检查 Woocommerce 3 中的订单项元数据值

Check for an order item meta data value in Woocommerce 3

我正在使用 Woocommerce 最新版本 3.4.2。插件:"WC Fields Factory" 或 "Woocommerce Custom Product Addons"。 如何在元数据中进行值检查? 看了半天官方文档也没找到解决方法

示例: 我在数组中有自定义值。我想检查一下 - 如果有 "sugar" 的值,那么...

$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
}

我想实现这个: $skus[] = //如果 $custom_meta 有值 'sugar',我给 $skus[] = '50000'

自定义值

您可以使用array_search

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

$sku = (array_search('Sugar', $custom_meta) !== false)
  ? '50000'
  : '0'; // default value

由于订单项元数据值是一个逗号分隔的字符串,您可以这样使用strpos()

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