在woocommerce中从元数据中过滤值的功能

问题描述 投票:0回答:1

据我们所知,元数据存储在一列中。我找到了一种方法来过滤它并仅获取值。

add_filter('woe_get_order_product_value__alg_wc_pif_global', function ($value,$order, $item, $product,$item_meta) {
 $t = unserialize($value);
 if( is_array($t) )
  $value = $t[0]['_value'];
 return $value;
}, 10, 5);

我在一些WP论坛上发现了这一点。现在我正在尝试更改它,以便它不仅可以过滤一个字段,而且可以过滤更多字段,例如“ alg_wc_pif_global_1”“ alg_wc_pif_global_2”“ alg_wc_pif_global_3”“ alg_wc_pif_global_4”

有什么想法吗?

php wordpress woocommerce hook-woocommerce
1个回答
0
投票

首先,我们在数组中定义所需的所有钩子前缀。然后通过foreach循环,我们将以这种方式启用每个定义的带前缀过滤器挂钩:

function woe_get_order_product_val_filter_callback( $value, $order, $item, $product, $item_meta ) {
    $value = maybe_unserialize( $value );
    return is_array( $value ) ? reset($value) : $value;
}

function woe_multiple_filter_hooks(){
    $prefixes    = array( '', '_1', '_2', '_3', '_4' ); // <=== HERE you define all the different hook prefixes

    $main_string = 'woe_get_order_product_value__alg_wc_pif_global';

    foreach( $prefixes as $prefix ) {
        add_filter( $main_string . $prefix, 'woe_get_order_product_val_filter_callback', 10, 5);
    }
}

woe_add_multiple_filter_hooks(); // We run the function, to enable each composite hook

经过测试和工作……

© www.soinside.com 2019 - 2024. All rights reserved.