Search code examples
phpmysqldatabasewordpressserver

wc_get_orders isn't getting all of the db records


I'm having an issue, I think it might be site/server related than code related as the db for this site is massive and this code worked perfectly before and i've never changed it. But what I am doing is grabbing all of the orders from a woocommerce store that have an "affiliate" associated with it. I do that with this code:

$order = wc_get_orders( array( 
        'status'=> 'wc-completed',
        'meta_key'  => '_coupon_codes_CCAFF_Aff_Affiliate_ID',
        )
    );

I then have a user form where the user enters a start date and end date and some other info for their query. (I can add the code in here for that if needed but trying to keep it short)

I then run a foreach loop though the orders where I echo out a table with all the entries.

foreach ($order as $ordercommout) {
    $orderID = $ordercommout->ID;
    $orderDateComp = strtotime($ordercommout->get_date_completed());
   // echo $orderDateComp;
    if($starttimeSTR < $orderDateComp && $endtimeSTR > $orderDateComp){
    $affID = get_post_meta($orderID, "_coupon_codes_CCAFF_Aff_Affiliate_ID");
    $affInfo = get_userdata($affID[0]);
    $affName = $affInfo->user_login;  
    $order_subtotal = $ordercommout->total - $ordercommout->shipping_total - $ordercommout->total_tax;
    $affcompctind = get_user_meta($affID[0], '_CCAFF_vendor_comm_pct');
    $commissionamt = $order_subtotal*$affcompctind[0]*0.01;
    $commpaidstatus1 = get_post_meta($orderID, "_coupon_codes_CCAFF_Comm_Status");
    
    
        echo "<tr>";
        echo "<td>" . esc_html($affName) . "</td>";
        echo "<td>" . esc_html($orderID) . "</td>";
        echo "<td>" . wc_price($commissionamt) . "</td>";
        echo "<td>" . esc_html(date('m/d/Y', $orderDateComp)) . "</td>";
        echo "<td>" . esc_html($commpaidstatus1[0]) . "</td>";
        echo '<td><form class="ccformaff" method="post" action="" onsubmit="return confirm(' . "'" . 'Do you really want to delete Entry?' . "'" . ');">
        
        <input type="hidden" id="delstart" name="delstart" value="' . esc_html($testdatestart) . '">
        <input type="hidden" id="delend" name="delend" value="' . esc_html($testdateend) . '">
        <input type="hidden" id="delcommtype" name="delcommtype" value="' . esc_html($testcommtype) . '">
        <input type="hidden" id="delcommstatus" name="delcommstatus" value="' . esc_html($testcommstatus) . '">
        
        
        <button type="submit" class="button button-primary" name="deleteItemCOMM" value="' . esc_html($orderID) . '"/>Delete</button></form></td>';
        echo "</tr>";
        
    }  
} 

So the thing is this code works. My issue is I'm not getting any orders before early September, I can run it multiple times and sometimes I get data to September 12th. If I run it again I might get up to September 8th, etc. It seems like my query is timing out but I'm not waiting long at all for it to run, the output is almost instant. Is there a way to fix this?


Solution

  • wc_get_orders has a limit parameter, same as WP_Query does.

    https://github.com/woocommerce/woocommerce/wiki/wc_get_orders-and-WC_Order_Query#parameters:

    limit
    Accepts an integer: Maximum number of results to retrieve or -1 for unlimited.
    Default: Site 'posts_per_page' setting.

    So unless your posts_per_page was already set to -1 for unlimited (unlikely), you should explicitly specify limit => -1 in your query parameters here, to get all posts.