php – 在Woocommerce 3中更改购物车商品价格

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 在Woocommerce 3中更改购物车商品价格脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用以下功能购物车中更改产品价格:
add_action( 'woocommerce_before_shipping_calculator','add_custom_PRice' 
     );
      function add_custom_price( $cart_object ) {
         foreach ( $cart_object->cart_contents as $key => $value ) {
         $value['data']->price = 400;
        } 
     }

它在WooCommerce版本2.6.x中正常工作,但在3.0版本中不再起作用@H_126_4@

何在WooCommerce 3.0版中使其工作?

谢谢.

更新(2018年9月)

使用WooCommerce 3.0版,您需要:

>改为使用woocommerce_before_calculate_totals钩子.
>改用WC_Cart get_cart()方法
>改用WC_product set_price()方法

这是代码

add_action( 'woocommerce_before_calculate_totals','add_custom_price',20,1);
function add_custom_price( $cart_obj ) {

    // This is necessary for WC 3.0+
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Avoiding hook rePEtITion (when using price calculations for example)
    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Loop through cart items
    foreach ( $cart_obj->get_cart() as $cart_item ) {
        $cart_item['data']->set_price( 40 );
    }
 }

代码在您的活动主题(或主题)的function.PHP文件中或在任何插件文件中.

代码经过测试并可以使用.

有关:

> Set cart item price from a hidden input field custom price in Woocommerce 3
> Change cart item price based on custom cart data in WooCommerce
> Set a specific product price conditionally on Woocommerce single product page & cart
> Add a select field that will change price in Woocommerce simple products

脚本宝典总结

以上是脚本宝典为你收集整理的php – 在Woocommerce 3中更改购物车商品价格全部内容,希望文章能够帮你解决php – 在Woocommerce 3中更改购物车商品价格所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。