[英]WooCommerce: Add product to cart with price override?
$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_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);
}
}
對於從 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;
}
}
我希望這對未來的人們有所幫助:)
為了使其動態化(分別覆蓋購物車中每個項目的價格),您需要使用woocommerce_add_to_cart
鈎子將購物車項目鍵作為會話鍵保存在會話中的覆蓋產品價格。
通過使用這些會話值,您可以計算正確的購物車總數並使更改后的價格也出現在訂單項中
在 WooCommerce 2.5 中,我發現這是一個由兩部分組成的過程。 第一步是在通過 woocommerce_add_cart_item 過濾器添加到購物車時更改定價的運行時顯示。 第二部分是通過 woocommerce_get_cart_item_from_session 過濾器設置在結賬期間讀取的持久會話數據。 這似乎比掛鈎計算總計過濾器(例如 woocommerce_before_calculate_totals)更快,因為它們在 WooCommerce 中運行非常頻繁。
此處有更多詳細信息: woocommerce 在添加到購物車時更改價格
我就是這樣做的,首先我將我的自定義價格添加到 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'];
}
}
您可以使用以下
add_filter( 'woocommerce_cart_item_price', 'kd_custom_price_message' );
function kd_custom_price_message( $price ) {
$textafter = ' USD';
return $price . $textafter;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.