我已经从2.6.14更新到WC 3.0.1.
我原来的代码如下:
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );
function add_custom_price( $cart_object ) {
$custom_price = 10; // This will be your custome price
foreach ( $cart_object->cart_contents as $key => $value ) {
$value['data']->price = $custom_price;
}
}
Run Code Online (Sandbox Code Playgroud)
它不再更新购物车或迷你车的价格.
对于最新版本的Woocommerce(3.0.1)中的购物车价格,请尝试在woocommerce中使用set_price($ price)功能,这将有所帮助.来源于此
add_action( 'woocommerce_before_calculate_totals', 'woocommerce_pj_update_price', 99 );
function woocommerce_pj_update_price() {
$custom_price = $_COOKIE["donation"]; // This will be your custom price
$target_product_id = 413; //Product ID
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if($cart_item['data']->get_id() == $target_product_id){
$cart_item['data']->set_price($custom_price);
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4962 次 |
| 最近记录: |