Home > Enterprise >  How to return product meta value when multiple products are in an order (Woocommerce)
How to return product meta value when multiple products are in an order (Woocommerce)

Time:02-01

I have a product item meta field called 'Start Date' which populates a date in checkout as "m-d-y" when product A is added to the cart.

I have a custom function setup to copy this meta field to an order custom field by getting the product meta field mentioned above. Everything is working fine when the order contains ONLY product A. However, as soon as there is a second product (product B) added to the order (where the 'Start Date' meta is not used in product B), the function breaks and does not see the 'Start Date' meta in the order item meta of product A.

I believe this is happening because my function is returning the meta from only the most recent item found on the order, as opposed to searching the order for that particular meta key. There will only ever be one of that meta key on a given order in product A, regardless of the number of products/items added, so I would like to search for the 'Start Date' meta key across all items contained in an order in order to return the result every time.

TLDR: What would I need to change in order search across all products in the order and get the meta 'Start Date'?

Here is my current code below:


   foreach ($order->get_items() as $item_id => $item ) {
        $item_data  = $item->get_data();
        $startdate =  $item->get_meta('Start Date'); // The Product ID
}

CodePudding user response:

I could understand that, you need to get meta value of 'product A' from your order items, even there are multiple products in a single order.

You can do this 2 ways.

1: Whenever $startdate has a value, then break from foreach loop.

foreach ($order->get_items() as $item_id => $item ) {
    $item_data  = $item->get_data();
    $startdate =  $item->get_meta('Start Date'); 
    if(!empty($startdate)){
        break;
    }
}

2: Assume that you know Id of product A. So check product A is exist in the order items. If exist then get meta value

$productA = 'x'; // let x is id of product A
foreach ($order->get_items() as $item_id => $item ) {
    // get product id from order items
    $product = $item->get_product();
    $product_id = $product->get_id();
    if($product_id == $productA){
        $startdate =  $item->get_meta('Start Date');
    }
}

CodePudding user response:

Try modifying your code to search for the Start Date meta key across all products in the order by using a loop to iterate through all items in the order, and checking if the meta key Start Date exists for each item.

Then, store the meta value in a variable and break the loop once it is found.

Try this:

$startdate = '';
foreach ($order->get_items() as $item_id => $item ) {
    $item_data  = $item->get_data();
    $meta =  $item->get_meta('Start Date'); // The meta value
    if (!empty($meta)) {
        $startdate = $meta;
        break;
    }
}

This code will loop through all items in the order, looking for the Start Date meta key.

When it's found, the loop will break and the meta value will be stored in the $startdate variable.

  • Related