【问题标题】:Cart item discount based on quantity in Woocommerce 3基于 Woocommerce 3 中数量的购物车商品折扣
【发布时间】:2019-01-31 06:59:43
【问题描述】:

我的 WP 网站销售定制的 T 恤。定制插件使每件定制衬衫成为 woocommerce 购物车中的订单项。如果订购了 2 件一件设计的衬衫(该行项目的数量为 2 件),我想打折。但是,如果每行只有 1 件商品,我不想打折。

我找到了这个解决方案

Adding a discount by cart items conditionally based on the item quantity

但这似乎改变了数据库中的产品价格,所以在打折后说 2 件蓝色衬衫,因为在 1 行订购了 2 件,如果我在单独的行上添加第 3 件衬衫,它也会获得折扣,我不想要。

【问题讨论】:

  • 抱歉,链接代码不会改变数据库中的商品价格…… 它只是根据商品价格中的购物车商品进行计算,设置计算折扣(负费用)全球购物车。
  • 但是我在运行代码时发现,如果我在第 2 件商品行适当打折后添加了第 3 件蓝色衬衫作为另一行商品,那么新添加的衬衫也会打折,这是我不想要的.
  • 此代码不会在购物车中添加任何商品…… 它只是使用购物车费用 API 打折……您的问题肯定是由于其他代码 sn-p 或一个插件,就是这样。
  • 你是对的,我引用了错误的代码sn-p,这是我尝试的代码sn-pstackoverflow.com/questions/42135852/…
  • 这个新的链接代码也是一样的...... 它不会改变数据库中的产品价格,它只是改变购物车项目的价格,不会也不要添加任何购物车项目。现在自从 woocommerce 版本 3.1+ 此代码不再工作,这需要以不同的方式完成。

标签: php wordpress woocommerce cart discount


【解决方案1】:

由于 woocommerce 版本 3+ 链接的答案代码不起作用。它需要不同的东西,甚至可以以更好的方式

代码将根据购物车商品数量应用购物车商品折扣。在此代码示例中,当数量等于或大于 2(两个)时,它将对购物车商品应用 5% 的折扣。

显示的购物车商品单价始终是实际产品价格。折扣将生效并显示在购物车商品小计中。

此外,产品名称将附加折扣说明。

代码:

add_filter('woocommerce_add_cart_item_data', 'add_items_default_price_as_custom_data', 20, 3 );
function add_items_default_price_as_custom_data( $cart_item_data, $product_id, $variation_id ){
    $product_id = $variation_id > 0 ? $variation_id : $product_id;

    ## ----- YOUR SETTING ----- ##
    $discount_percentage = 5; // Discount (5%)

    // The WC_Product Object
    $product = wc_get_product($product_id);

    // Only for non on sale products
    if( ! $product->is_on_sale() ){
        $price = (float) $product->get_price();

        // Set the Product default base price as custom cart item data
        $cart_item_data['base_price'] = $price;

        // Set the Product discounted price as custom cart item data
        $cart_item_data['new_price'] = $price * (100 - $discount_percentage) / 100;

        // Set the percentage as custom cart item data
        $cart_item_data['percentage'] = $discount_percentage;
    }

    return $cart_item_data;
}

// Display the product original price
add_filter('woocommerce_cart_item_price', 'display_cart_items_default_price', 20, 3 );
function display_cart_items_default_price( $product_price, $cart_item, $cart_item_key ){
    if( isset($cart_item['base_price']) ) {
        $product        = $cart_item['data'];
        $product_price  = wc_price( wc_get_price_to_display( $product, array( 'price' => $cart_item['base_price'] ) ) );
    }
    return $product_price;
}

// Display the product name with the discount percentage
add_filter( 'woocommerce_cart_item_name', 'append_percetage_to_item_name', 20, 3 );
function append_percetage_to_item_name( $product_name, $cart_item, $cart_item_key ){
    if( isset($cart_item['percentage']) && isset($cart_item['base_price']) ) {
        if( $cart_item['data']->get_price() != $cart_item['base_price'] )
            $product_name .= ' <em>(' . $cart_item['percentage'] . '% discounted)</em>';
    }
    return $product_name;
}

add_action( 'woocommerce_before_calculate_totals', 'custom_discounted_cart_item_price', 20, 1 );
function custom_discounted_cart_item_price( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    ## ----- YOUR SETTING ----- ##
    $targeted_qty = 2; // Targeted quantity

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ) {

        // For item quantity of 2 or more
        if( $cart_item['quantity'] >= $targeted_qty && isset($cart_item['new_price']) ){

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

代码在您的活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。


要显示折扣产品价格而不是原始产品价格,只需删除 woocommerce_cart_item_price() 函数(和挂钩)...

最新相似: Cart item quantity progressive percentage discount in Woocommerce 3

【讨论】:

  • 这完全按照描述解决了问题,非常感谢。我在测试时发现,定制应用程序会修改购物车中的基本价格,例如每行超过 16 个字符的文本会增加 1 美元,因此购物车中的价格为 20 美元+1.50 美元=21.50 美元。我需要修改购物车的基本价格。我想我可以弄清楚如何做到这一点。感谢您的帮助。
  • 想我明白了,我改变了 $product_price = wc_price( wc_get_price_to_display( $product, array( 'price' => $cart_item['base_price'] ) ) ); to $product_price = wc_price(wc_get_price_to_display($product,$cart_item['base_price']));
  • @LoicTheAztec - 是 WC 神。伙计,非常感谢这个决定。我将它与另一个解决方案(你也分享了)结合起来,添加了一些我自己的解决方案,结果,我为 WC 商店获得了一个惊人的功能。
  • @LoicTheAztec 为什么 did_action( 'woocommerce_before_calculate_totals' ) >= 2 条件?它解决了什么问题?
猜你喜欢
  • 2017-06-05
  • 2019-02-06
  • 2019-02-21
  • 2017-09-27
  • 1970-01-01
  • 2020-11-24
  • 1970-01-01
  • 2018-01-30
  • 2020-08-18
相关资源
最近更新 更多