WooCommerce:添加产品到价格覆盖的购物车?

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

上面的代码将产品256添加到购物车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; // for WooCommerce version 3+ use: // $value['data']->set_price($custom_price); } } 

希望它会有用…

您需要在上面的代码中引入if语句来检查产品ID:

 add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' ); function add_custom_price( $cart_object ) { $custom_price = 10; // This will be your custome price $target_product_id = 598; foreach ( $cart_object->cart_contents as $value ) { if ( $value['product_id'] == $target_product_id ) { $value['data']->price = $custom_price; } /* // If your target product is a variation if ( $value['variation_id'] == $target_product_id ) { $value['data']->price = $custom_price; } */ } } 

在任何地方添加此代码,并确保此代码始终可执行。

添加这个代码后,当你打电话给:

 global $woocommerce; $woocommerce->cart->add_to_cart(598); 

只有这个产品将会被加上价格的覆盖,其他产品添加到购物车将被忽略的压倒一切的价格。

希望这会有所帮助。

我已经尝试了以上所有代码示例,最新的woocommerce 3.0不支持上述任何示例。 使用下面的代码,并为我完美的工作。

 add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' ); function add_custom_price( $cart_object ) { $custom_price = 10; // This will be your custom price foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { $cart_item['data']->set_price($custom_price); } } 

woocommerce版本3.0.0发布后,产品价格更新使用set_price($ price)函数添加到购物车。 举例如下:

 add_action( 'woocommerce_before_calculate_totals', 'mj_custom_price' ); function mj_custom_price( $cart_object ) { $woo_ver = WC()->version; $custom_price = 10; foreach ( $cart->cart_contents as $key => $value ) { if($woo_ver < "3.0.0" && $woo_ver < "2.7.0") { $value['data']->price = $custom_price; } else { $value['data']->set_price($custom_price); } } } 

非常感谢

对于Wordpress和Woocommerce最新版本,请使用像这样

 add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' ); function add_custom_price( $cart_object ) { foreach ( $cart_object->cart_contents as $key => $value ) { $custom_price = 5; $value['data']->set_price($custom_price); } } 

为了使其成为dynamic的(分别替代购物车中的每个商品的价格),您需要使用woocommerce_add_to_cart挂钩将购物车商品关键字的会话中的覆盖商品价格保存为会话密钥。

通过使用这些会话值,您可以计算正确的购物车总额,并使更改后的价格也出现在订单项目中

举例来说,您可以参考以下指南获取完整的详细信息。

随着WooCommerce 2.5我发现这是一个两部分的过程。 第一步是通过woocommerce_add_cart_itemfilter将订单添加到购物车时更改定价的运行时间显示。 第二部分是通过woocommerce_get_cart_item_from_sessionfilter设置在结帐期间读取的持久会话数据。 这似乎比在WooCommerce中非常频繁地运行计算总计filter(比如woocommerce_before_calculate_totals)要快。

更多细节在这里: woocommerce更改价格,同时添加到购物车

从Google获得的eveeryone。 以上是现在不推荐使用,因为我发现更新到WooCommerce 3.0.1。

而不是上面你现在需要使用set_price而不是price

这里是一个例子:

 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']->set_price = $custom_price; } } 

我希望这有助于未来的人:)

这是我如何做到这一点,首先我添加我的自定义价格cart_item_data女巫可以保存自定义数据购物车项目,然后我使用woocommerce_before_calculate_totals,循环购物车,并添加以前增加的价格。

 function add_donation_to_cart() { $cart_item_data = array('price' => $_REQUEST['donate_amount']); $woocommerce->cart->add_to_cart( 5395, 1, '', array(), $cart_item_data); } add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' ); function add_custom_price( $cart ) { foreach ( $cart->cart_contents as $key => $value ) { $value['data']->price = $value['price']; } }