我尝试使用 WooCommerce 中的 WC_Query 通过帖子作者 id 获取产品,因此我尝试包含一个新的自定义元密钥 "_author",其中包含以下内容:
add_filter( 'woocommerce_product_data_store_cpt_get_products_query', 'handling_custom_meta_query_keys', 10, 3 );
function handling_custom_meta_query_keys( $wp_query_args, $query_vars ) {
$meta_key = '_author';
if ( ! empty( $query_vars[$meta_key] ) ) {
$wp_query_args['meta_query'][] = array(
'key' => $meta_key,
'value' => esc_attr( $query_vars[$meta_key] ),
'operator' => '==',
);
}
return $wp_query_args;
}
Run Code Online (Sandbox Code Playgroud)
然后我尝试将它与以下内容一起使用wc_get_products():
$product_list = wc_get_products( array('_author' => get_current_user_id()) );
Run Code Online (Sandbox Code Playgroud)
但它返回空数组。任何想法?
我想显示 woocommerce 中不同角色的产品价格。为了这个目标,我通过输入字段在产品的后元中设置了一个带有价格元值的元键。然后我使用以下代码来过滤该产品的显示价格以获得所需的角色。角色是customer_2
例如:产品_A 的正常价格为 200 美元,客户_2 显示为 150 美元,产品_B 的正常价格为 350 美元,客户_2 显示为 200 美元,依此类推...
function custom_price($price, $product){
$product_co_price = get_post_meta($product->ID, '_co_price');
$user = wp_get_current_user();
if($product_co_price){
if(in_array('customer_2', (array) $user->roles)){
$price = $product_co_price;
}
}
return $price;}
add_filter('woocommerce_product_get_price', 'custom_price', 10, 2);
add_filter('woocommerce_product_variation_get_price', 'custom_price', 10, 2 );
add_filter( 'woocommerce_product_get_regular_price', 'custom_price', 10, 2 );
add_filter( 'woocommerce_product_get_sale_price', 'custom_price', 10, 2 );
Run Code Online (Sandbox Code Playgroud)
元键中的“_co_price”存储价格的元值。
但什么也没发生!