Shinox
Shinox

Reputation: 107

How to loop through order item quantity in WooCommerce

The following problem: I would like to have a PDF button output for any number of orders. For this PDF button, the order ID and the item ID should be given for each order so that the PDF can be generated.

In the following code section I can also see what I mean with a picture from the frontend:

foreach ( $order->get_items() as $item_id => $item ) {
$order = new WC_Order( $_POST['oid'] );
$cid = get_user_meta( get_current_user_id(), 'company', true );
$result = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . "center_codes WHERE order_id = '" . $order_id . "'", OBJECT );
        foreach ($result as $key => $value) {
            echo '<button><a style="color: #fff;" href="' . PLUGIN_URL . '..generate-pdf.php?oid=' . $order_id . '&item_id=' . $value->item_id . '" target="_blank">' .  pll_translate_string( 'PDF-Download', pll_current_language() ) . '</a></button><br/><br/>';
        }
}

The order ID of the order is saved in the Order ID column. The product ID is stored in the Item ID column for each order

Upvotes: 2

Views: 1549

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 254378

Updated - There are some mistakes in your code…

  • Get the WC_Order Object before your 1st foreach loop (loop through order items)
  • for item quantity use a FOR loop instead of a FOREACH loop
  • You should directly use in your SQL query the 'item_id' included as key from the order items foreach loop. This is much better than embedding another foreach loop inside the order items loop.
  • and some other things…

So your code could be (commented):

global $wpdb;

// Be sure that we get an order id from $_POST
if ( isset($_POST['oid']) && $_POST['oid'] > 0 ) {
    $order_id = (int) $_POST['oid'];
    $order    = new WC_Order( $_POST['oid'] ); // <=== Get the order object before your 1st foreach loop
}
// Be sure that we get the WC_Order object (valid order id)
if ( is_a($order, 'WC_Order') ) {

    $cid = get_user_meta( get_current_user_id(), 'company', true );

    if ( ! empty($cid) ) {
        
        // Loop through order items
        foreach( $order->get_items() as $item_id => $item ) {
            
            $result = $wpdb->get_results( $wpdb->prepare( "
                SELECT * 
                FROM {$wpdb->prefix}coupon_codes
                WHERE order_id = %d
                    AND item_id = %d
            ", $order_id, $item_id ) );
            
            // Quantity Loop (where variable $i will be increased one by one depending on quantity)
            for ( $i = 1; $i <= $item->get_quantity(); $i++ ) { // <=== A FOR loop (instead of a foreach)
                
                // You may need to use the variable $i somewhere below

                echo '<button><a style="color: #fff;" href="' . TS_SHOP_PLUGIN_URL . '..generate-pdf.php?oid=' . $order_id . '&item_id=' . $item_id . '" target="_blank">' .  pll_translate_string( 'PDF-Download', pll_current_language() ) . '</a></button><br/><br/>';
            }
        }
    }
}

Now I can't really test the code… But this should help you in solving your issue.

Last thing, if needed, to get the user Id from the order, use $user_id = $order->get_user_id();. Then could use it (may be) in:

$user_id = $order->get_user_id(); // <=== Get the user Id from the order
$cid     = get_user_meta( $user_id, 'company', true );

Upvotes: 2

Related Questions