我創造了一個親戚的業務的電子商務商店。他擁有用戶必須在網站上進行個性化的產品。我正在編寫一個單獨的頁面,允許用戶執行此操作,該操作將鏈接回商店,並在完成後將商品添加到購物車。 我找到了解決每個產品添加至購物車分開,而不是添加到數量(因爲添加的每個產品已被個性化的),和溶液以改變車中的項目的價格。Woocommerce-不同價格相同的產品在購物車
WooCommerce: Add product to cart with price override? https://businessbloomer.com/woocommerce-display-separate-cart-items-product-quantity-1/
的問題是,所有的項目都發生變化,不僅僅是最後一個加入。例如:用戶設計一個帶有她的名字的圓珠筆。價格是4美元。用戶然後用她的名字設計一支凝膠筆。購物車將顯示兩個單獨的筆,計算價格增加,但所有筆的價格都會增加,而不是最後一筆增加。我需要找到一種方法來根據產品構建頁面發送的價格設置最後添加項目的價格。到目前爲止,我所嘗試過的東西都沒有奏效。
可變產品不會對這個網站的工作。
//To display a product separately every time it is added
function bbloomer_split_product_individual_cart_items($cart_item_data, $product_id){
$unique_cart_item_key = uniqid();
$cart_item_data['unique_key'] = $unique_cart_item_key;
return $cart_item_data;
}
add_filter('woocommerce_add_cart_item_data','bbloomer_split_product_individual_cart_items', 10, 2);
add_filter('woocommerce_is_sold_individually', '__return_true');
//To change the item price programmatically. Changes all items with the same ID
//I need it to change only the last one added.
//Set to retrieve new price from the customizing page
add_action('woocommerce_before_calculate_totals', 'add_custom_price', 10, 1);
/**
* @param $cart_obj
*/
function add_custom_price($cart_obj) {
ob_start();
include 'createbb.php';
global $custom_price;
$target_product_id = 17;
// This is necessary for WC 3.0+
if (is_admin() && ! defined('DOING_AJAX'))
return;
foreach ($cart_obj->get_cart() as $key => $value) {
if ($value['product_id'] == $target_product_id) {
$value['data']->set_price($custom_price);
}
}
ob_end_clean();
}
請提供有關您的WooCommerce實施的更多詳細信息,並嘗試將產品集成爲Variable。 –
我添加了代碼。我不知道如何在用戶自定義每個單獨的「筆」時單獨定價,因爲它們都共享相同的ID。 –