【问题标题】:Woocommerce: Custom price based on user input - Mini cart conflictWoocommerce:基于用户输入的自定义价格 - 迷你购物车冲突
【发布时间】:2017-09-20 14:51:35
【问题描述】:

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

我的产品通过 AJAX 添加到购物车,而迷你购物车未加载 cookie 的新值。它似乎被缓存到输入字段的先前值,直到您到达购物车页面并在硬重新加载之后。

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

那么,问题是是否有办法让迷你购物车更新为最后插入的价格?

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

    无偿自我推销,但我的Name Your Price 应该会自动与迷你购物车一起使用。

    但我认为您的问题实际上是在问为什么该项目不被视为独特......因此第二次添加。答案是不同的价格会呈现出独一无二的$cart_idsee 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 );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-16
      • 1970-01-01
      • 1970-01-01
      • 2019-12-21
      • 1970-01-01
      • 2014-03-11
      • 1970-01-01
      相关资源
      最近更新 更多