Woocommerce:基于用户输入的自定义价格 - 迷你购物车冲突

时间:2017-09-20 14:51:36

标签: wordpress woocommerce

我按照此post的解决方案从输入字段中获取带有Cookie的自定义价格,除了迷你购物车外,它的工作正常。

我的产品使用AJAX添加到购物车中,迷你购物车不会加载cookie的新值。它似乎缓存到输入字段的上一个值,直到您到达购物车页面并在硬重新加载后。

例如,我访问输入自定义价格的页面。我第一次投入20欧元,按下添加到购物车按钮,我的产品通过AJAX添加到购物车,效果很好。 如果我使用自定义价格删除此产品并尝试在此时以不同的价格再次添加,则迷你购物车会保留之前的价格(20欧元)。

所以,问题是,是否有办法让最新插入的价格更新迷你购物车?

1 个答案:

答案 0 :(得分:0)

无偿的自我推销,但我的Name Your Price应该自动使用迷你推车。

但我认为你的问题实际上是在问为什么这个项目不被认为是唯一的...因此又增加了第二次。答案是,不同的价格会呈现唯一的$cart_id see source。使用唯一ID时,购物车中找不到该商品,因此会再次添加该商品。

强行单独销售'不同价格的商品要单独销售,您需要通过过滤woocommerce_cart_id来更改购物车ID的生成方式。以下是我如何使用我的Name Your Price插件。您需要根据自己的代码进行调整。

<?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 );
相关问题