2017-09-20 67 views
-3

我遵循此post的解決方案從輸入字段使用cookie獲得自定義價格,並且除迷你購物車之外它可以正常工作。Woocommerce:基於用戶輸入的自定義價格 - 迷你購物車衝突

我的產品通過AJAX添加到購物車中,迷你購物車不會加載cookie的新值。它似乎被緩存到輸入字段的前一個值,直到您到達購物車頁面並重新加載之後。

例如,我訪問我輸入自定義價格的頁面。我第一次投入20歐元時,按下加入購物車按鈕,我的產品通過AJAX添加到購物車中,並且效果很好。 如果我使用自定義價格移除此產品,並嘗試在此時以不同的價格再次添加此產品,則迷你推車會保留之前的價格(20歐元)。

所以,問題是如果有一種方法來保持迷你購物車更新最後插入的價格?

回答

0

無償自我推銷,但我的Name Your Price應自動與迷你車。

但我認爲你的問題實際上是問爲什麼該項目不被視爲獨特的...因此,第二次添加。答案是,不同的價格將呈現獨特的$cart_idsee source。使用唯一的ID,購物車中找不到該商品,因此會再次添加該商品。

要強制使用不同價格的「單獨銷售」商品單獨銷售,您需要通過過濾woocommerce_cart_id來更改購物車ID的生成方式。以下是我如何使用我的姓名價格插件進行操作。你需要適應你自己的代碼。

<?php 
/** 
* Plugin Name: WooCommerce Name Your Price Sold Individually 
* Plugin URI: https://gist.github.com/helgatheviking/a8802255167751a5dd746f83cdfc8716 
* Description: Double check enforcement of "Sold Individually" for NYP items 
* Version: 1.1.0 
* WC requires at least: 2.6.3 
* Author: Kathy Darling 
* Author URI: http://kathyisawesome.com/ 
* 
* Copyright: © 2016 Kathy Darling 
* License: GNU General Public License v3.0 
* License URI: http://www.gnu.org/licenses/gpl-3.0.html 
*/ 

function wc_nyp_force_sold_individually($cart_id, $product_id, $variation_id, $variation, $cart_item_data) { 
    // Get the product 
    $product = wc_get_product($variation_id ? $variation_id : $product_id); 
    if ($product->is_sold_individually() && WC_Name_Your_Price_Helpers::is_nyp($product)){ 
     $id_parts = array($product_id); 
     if ($variation_id && 0 != $variation_id) { 
      $id_parts[] = $variation_id; 
     } 
     if (is_array($variation) && ! empty($variation)) { 
      $variation_key = ''; 
      foreach ($variation as $key => $value) { 
       $variation_key .= trim($key) . trim($value); 
      } 
      $id_parts[] = $variation_key; 
     } 
     $cart_id = md5(implode('_', $id_parts)); 
    } 
    return $cart_id; 
} 
add_filter('woocommerce_cart_id', 'wc_nyp_force_sold_individually', 10, 5); 
相關問題