Search code examples
phpwordpresswoocommercemetadataorders

Check for an order item meta data value in Woocommerce 3


I am using Woocommerce latest version 3.4.2. Plugin: "WC Fields Factory" or "Woocommerce Custom Product Addons". How to do a value check in a meta data? I read the official documentation for a long time and could not find a solution.

Example: I have custom values in the array. And I want to make a check - if there is a value of "sugar", then...

Meta $key - 'Optionally select'

$custom_meta = $item->get_meta('Optionally select'); // Show all value

foreach( $order->get_items() as $item_id => $item){
    $skus[] = $product->get_sku();

    // Here need add check and formate meta value
}

I want to achieve this: $skus[] = //if $custom_meta have value 'sugar', I give cusom value for $skus[] = '50000'


Solution

  • As the order item metadata value is a comma separated string, you can use strpos() this way:

    $ops = $item->get_meta('Optionally select');
    if( strpos( $ops, 'Sugar' ) !== false ) $skus[] = '50000';