2017-07-07 252 views
2

我正在創建一個WooCommerce加載項,以根據訪問者選定的選項自定義產品,並自定義價格計算並存儲到會話表中。
我能夠在購物車頁面中獲得該值。更改WooCommerce購物車中的產品價格並結帳

我的問題:
我想改變產品的默認價格,並在WooCommerce過程購物車,結賬,付款,郵件通知,爲了新的計算值替換它...

有什麼建議嗎?

感謝

回答

2

策右勾拳得到它的工作是woocommerce_before_calculate_totals。但是,你必須完成(替換)代碼來獲得在下面的掛鉤函數的新價格:

add_filter('woocommerce_before_calculate_totals', 'custom_cart_items_prices', 10, 1); 
function custom_cart_items_prices($cart_object) { 

    if (is_admin() && ! defined('DOING_AJAX')) 
     return; 

    foreach ($cart_object->get_cart() as $cart_item) { 

     // get the product id (or the variation id) 
     $id = $cart_item['data']->get_id(); 

     // GET THE NEW PRICE (code to be replace by yours) 
     $new_price = 500; // <== Add your code HERE 

     // Updated cart item price 
     $cart_item['data']->set_price($new_price); 
    } 
} 

代碼放在您的活動子主題(或主題)的function.php文件或也以任何插件文件。

此代碼已經過測試,適用於WooCommerce版本3+。但是,由於您不提供任何代碼,我無法測試它是否真正從會話中獲得新的價格...

相關問題