【发布时间】:2020-11-19 22:10:29
【问题描述】:
我正在尝试使用名为“Cost_price”的自定义产品属性“值”为每个产品(简单和可变)更新元“销售价格”。 此自定义产品属性是与其他站点连接的 API,它会每周更改一次值(价格),因此当产品自定义属性更新时,代码应该能够更改“销售价格”中的价格。
add_filter( 'woocommerce_product_variation_get_price', 'conditional_product_sale_price', 10, 2 );
add_filter( 'woocommerce_product_get_sale_price', 'conditional_product_sale_price', 10, 2 );
add_filter( 'woocommerce_product_variation_get_sale_price', 'conditional_product_sale_price', 10, 2 );
add_filter( 'woocommerce_variation_prices_sale_price', 'conditional_product_sale_price', 10, 2 );
global $product;
$new_price = array_shift( wc_get_product_terms( $product->id, 'pa_cost_price', array( 'fields' => 'names' ) ) );
function conditional_product_sale_price( $price, $product ) {
if( is_admin() ) return $price;
$price = $new_price;
}
if( !empty($sale_price) ){
update_post_meta( $product_id, '_sale_price', $new_price );
}
return $price;
我在这里寻找不同的变体,但找不到任何有效的方法。有谁知道我做错了什么? P.S 我完全是个菜鸟。
【问题讨论】:
标签: php wordpress api woocommerce getcustomattributes