$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);
}
}
希望它会有用...
您需要在上面的代码中引入一个用于检查产品 ID 的 if
语句:
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_object->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);
}
}
对于从谷歌来到这里的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'];
}
}
使用WooCommerce 2.5,我发现这是一个由两部分组成的过程。 第一步是通过woocommerce_add_cart_item筛选器添加到购物车时更改定价的运行时显示。 第二部分是设置在签出期间通过woocommerce_get_cart_item_from_session过滤器读取的持久会话数据。 这似乎比挂钩计算总计过滤器(例如woocommerce_before_calculate_totals(更快,因为它们在WooCommerce中运行非常频繁。
更多细节在这里:woocommerce在添加到购物车时更改价格
要使其动态化(分别覆盖购物车中每件商品的价格(,您需要使用woocommerce_add_to_cart
钩子将带有购物车项目键的会话中的覆盖产品价格保存为会话键。
通过使用这些会话值,您可以计算正确的购物车总数,并使更改后的价格也显示在订单项目中
您可以使用以下内容
add_filter( 'woocommerce_cart_item_price', 'kd_custom_price_message' );
function kd_custom_price_message( $price ) {
$textafter = ' USD';
return $price . $textafter;
}