WooCommerce:将产品添加到价格优先的购物车? [英] WooCommerce: Add product to cart with price override?

查看:156
本文介绍了WooCommerce:将产品添加到价格优先的购物车?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $ replace_order = new WC_Cart(); 
$ replace_order-> empty_cart(true);
$ replace_order-> add_to_cart(256,1);

上述代码将 256 添加到购物车 1 时间。但我遇到的问题是,我想要能够完全覆盖产品价格...据我所知,我可以做的唯一的事情,它适用于购物车的优惠券。

$ b

解决方案

这里是在购物车中覆写产品价格的代码

  add_action('woocommerce_before_calculate_totals','add_custom_price'); 

function add_custom_price($ cart_object){
$ custom_price = 10; //这将是您的客户价格
foreach($ cart_object-> cart_contents as $ key => $ value){
$ value ['data'] - > price = $ custom_price;
}
}

希望它会有用...


$replace_order = new WC_Cart();
$replace_order->empty_cart( true );
$replace_order->add_to_cart( "256", "1");

The above code add product 256 to the Cart 1 time. But the issue I'm having is that I want to be able to completely override the product price... as far as I can tell, the only thing I can do it apply a coupon to the Cart.

Is there a way to completely override the price to something totally custom?

解决方案

Here is the code for overriding price of product in cart

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;
    }
}

Hope it will be useful...

这篇关于WooCommerce:将产品添加到价格优先的购物车?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆