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

上面的代码将产品添加256到购物车1时间。但我遇到的问题是我希望能够完全覆盖产品价格......据我所知,我唯一能做的就是将优惠券应用于购物车。

有没有办法完全覆盖完全定制的价格?

4

10 回答 10

57

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

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

希望它会有用...

于 2012-09-08T11:31:23.017 回答
45

您需要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);

只有该产品会以覆盖价格添加,其他添加到购物车的产品将被忽略以覆盖价格。

希望这会有所帮助。

于 2014-01-20T06:50:18.880 回答
14

我已经尝试了上述所有代码示例,最新的 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);   
    }
}
于 2017-04-10T12:06:43.310 回答
7

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

非常感谢

于 2017-05-08T12:04:46.930 回答
4

对于 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); 
    }
}
于 2017-07-11T09:05:00.447 回答
3

对于从 Google 来到这里的每个人。由于我发现更新到 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;
    }
}

我希望这对未来的人们有所帮助:)

于 2017-04-07T09:24:57.317 回答
1

要使其动态化(分别覆盖购物车中每个项目的价格),您需要使用woocommerce_add_to_cart钩子将覆盖产品价格保存在会话中,并将购物车项目键作为会话键。

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

于 2014-12-14T19:09:30.830 回答
1

使用 WooCommerce 2.5,我发现这是一个两部分的过程。第一步是在通过 woocommerce_add_cart_item 过滤器添加到购物车时更改定价的运行时显示。第二部分是设置通过 woocommerce_get_cart_item_from_session 过滤器在结帐期间读取的持久会话数据。这似乎比挂钩计算总计过滤器(例如 woocommerce_before_calculate_totals)更快,因为它们在 WooCommerce 中运行得非常频繁。

更多详细信息: woocommerce 在添加到购物车时更改价格

于 2016-02-25T16:13:46.900 回答
1

我就是这样做的,首先我将我的自定义价格添加到 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'];
    }
}
于 2016-09-28T14:25:35.263 回答
1

您可以使用以下

add_filter( 'woocommerce_cart_item_price', 'kd_custom_price_message' );

function kd_custom_price_message( $price ) {

        $textafter = ' USD'; 
        return $price . $textafter;
}
于 2018-10-10T09:42:12.990 回答