伍德姆:加产品到购物车与价格优先?

时间:2022-10-25 10:15:04
$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.

上述代码将产品256添加到购物车1次。但我的问题是,我想要完全凌驾于产品价格之上……据我所知,我能做的唯一的事情就是把优惠券应用到购物车上。

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

有没有一种方法可以完全凌驾于价格之上,使其完全定制?

9 个解决方案

#1


48  

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...

希望它有用……

#2


41  

You need to introduce an if statement 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.

只有这个产品会被加上重写的价格,其他添加到cart的产品会被忽略,因为重写的价格。

Hope this will be helpful.

希望这能有所帮助。

#3


8  

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

#4


6  

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

非常感谢

#5


3  

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

#6


1  

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_cart hook.

要使其动态(分别为cart中的每一项重写价格),需要使用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

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

You can refer the following guide for complete details with an example.

您可以参考下面的指南以获得完整的详细信息。

#7


1  

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_total)要快,因为它们在WooCommerce中经常运行。

More details here: woocommerce change price while add to cart

更多细节在这里:woocommerce改变价格的同时增加购物车

#8


1  

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

从谷歌到这里的每个人。当我发现更新到WooCommerce 3.0.1时,上面的内容已经过时了。

Instead of the above you now need to use set_price instead 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 :)

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

#9


0  

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 witch可以将定制数据保存到购物车中,然后我使用woocommerce_before_calculate_total,循环购物车并添加先前添加的价格。

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

#1


48  

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...

希望它有用……

#2


41  

You need to introduce an if statement 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.

只有这个产品会被加上重写的价格,其他添加到cart的产品会被忽略,因为重写的价格。

Hope this will be helpful.

希望这能有所帮助。

#3


8  

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

#4


6  

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

非常感谢

#5


3  

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

#6


1  

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_cart hook.

要使其动态(分别为cart中的每一项重写价格),需要使用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

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

You can refer the following guide for complete details with an example.

您可以参考下面的指南以获得完整的详细信息。

#7


1  

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_total)要快,因为它们在WooCommerce中经常运行。

More details here: woocommerce change price while add to cart

更多细节在这里:woocommerce改变价格的同时增加购物车

#8


1  

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

从谷歌到这里的每个人。当我发现更新到WooCommerce 3.0.1时,上面的内容已经过时了。

Instead of the above you now need to use set_price instead 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 :)

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

#9


0  

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 witch可以将定制数据保存到购物车中,然后我使用woocommerce_before_calculate_total,循环购物车并添加先前添加的价格。

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