php购物车不刷新,php – 动态购物车商品定价不适用于WooCommerce 3.0中的订单

使用get_price()方法更新…

您应该在此自定义附加功能,产品ID或产品ID数组中使用woocommerce_before_calculate_totals操作挂钩设置.然后,对于每个人,您可以进行自定义计算,以设置将在购物车,结帐时和订单提交后设置的自定义价格.

这是在WooCommerce 3.0版上测试的功能代码:

add_action( 'woocommerce_before_calculate_totals', 'adding_custom_price', 10, 1);

function adding_custom_price( $cart_obj ) {

if ( is_admin() && ! defined( 'DOING_AJAX' ) )

return;

// Set below your targeted individual products IDs or arrays of product IDs

$target_product_id = 53;

$target_product_ids_arr = array(22, 56, 81);

foreach ( $cart_obj->get_cart() as $cart_item ) {

// The corresponding product ID

$product_id = $cart_item['product_id'];

// For a single product ID

if($product_id == $target_product_id){

// Custom calculation

$price = $cart_item['data']->get_price() + 50;

$cart_item['data']->set_price( floatval($price) );

}

// For an array of product IDs

elseif( in_array( $product_id, $target_product_ids_arr ) ){

// Custom calculation

$price = $cart_item['data']->get_price() + 30;

$cart_item['data']->set_price( floatval($price) );

}

}

}

代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中.

Then you can easily replace the fixed values in my fake calculations by your product dynamic values with that with get_post_meta() function just like in your code as you have the $product_id for each cart item…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值