php WooCommerce:将产品添加到购物车并覆盖价格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12327251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:21:00  来源:igfitidea点击:

WooCommerce: Add product to cart with price override?

phpwordpressclasswoocommerce

提问by dcolumbus

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

The above code add product 256to the Cart 1time. 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.

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

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

有没有办法将价格完全覆盖为完全定制的东西?

回答by Ratnakar - StoreApps

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;
        // for WooCommerce version 3+ use: 
        // $value['data']->set_price($custom_price);
    }
}

Hope it will be useful...

希望它会很有用...

回答by Ratnakar - StoreApps

You need to introduce an ifstatement for checking product id, in above code:

你需要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;
        }
        */
    }
}

Add this code anywhere and make sure that this code is always executable.

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

After adding this code, when you'll call:

添加此代码后,何时调用:

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

Only this product will be added with overridden price, other products added to cart will be ignored for overriding prices.

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

Hope this will be helpful.

希望这会有所帮助。

回答by Saran

I have tried all above code samples and latest woocommerce 3.0 is not support any of the above example. Use below code and working perfectly for me.

我已经尝试了上述所有代码示例,但最新的 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);   
    }
}

回答by Mohd Jafar

After release of woocommerce version 3.0.0 product price is update on add to cart using set_price($price) function. The example is given as below :

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

Many Thanks

非常感谢

回答by sumon cse-sust

For the Wordpress and Woocommerce latest version,Please use like this

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

回答by danyo

For eveeryone that got here from Google. The above is now deprecated as i found out updating to WooCommerce 3.0.1.

对于从 Google 来到这里的每个人。当我发现更新到 WooCommerce 3.0.1 时,上述内容现已弃用。

Instead of the above you now need to use set_priceinstead of price

您现在需要使用set_price代替上面的代替price

Here is an example:

下面是一个例子:

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

I hope this helps people in the future :)

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

回答by Sark

To make it dynamic ( override price for each item in cart separately ), you need to save the override product price in session with cart item key as session key using woocommerce_add_to_carthook.

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

by using these session values you can calculate correct Cart Total and make the altered price appear in the Order Item as well

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

回答by Lance Cleveland

With WooCommerce 2.5 I found this to be a 2-part process. The first step is to change the run-time display of pricing when added to the cart via the woocommerce_add_cart_item filter. The second part is to set the persistent session data which is read during checkout via the woocommerce_get_cart_item_from_session filter. This seems to be faster than hooking the calculate totals filters (such as woocommerce_before_calculate_totals) as they are run very frequently in WooCommerce.

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

More details here: woocommerce change price while add to cart

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

回答by tobros91

This is how i did it, first i add my custom price to cart_item_data witch can save custom data to cart items, then i use woocommerce_before_calculate_totals, loop the cart and add the previously added 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'];
    }
}

回答by shrddha patel

You can use the following

您可以使用以下

add_filter( 'woocommerce_cart_item_price', 'kd_custom_price_message' );

function kd_custom_price_message( $price ) {

        $textafter = ' USD'; 
        return $price . $textafter;
}