【问题标题】:WooCommerce - Buy one Get one FreeWooCommerce - 买一送一
【发布时间】:2017-11-04 19:48:44
【问题描述】:

我在 WooCommerce 优惠券代码中创建了一个名为“2FOR1WOW”的代码,并将此代码添加到 functions.php,但它不起作用。票证被应用,确认消息说没问题,但总数没有减少。

任何帮助将不胜感激!

// Hook before calculate fees - "Buy 2 get cheapest free" coupon
add_action('woocommerce_cart_calculate_fees' , 'buy2_coupon');

/**
 * Add discount for "Buy 2 get cheapest free" coupon
 * @param WC_Cart $cart
 */

function buy2_coupon( WC_Cart $cart ){

    // add the coupons here
    $buy2_coupons = array('2FOR1WOW', 'anothercouponcode');

    // return if cart has less than 2 items
    if( $cart->cart_contents_count < 2 ){
        return;
    }

    $applied_coupons = $cart->get_applied_coupons();    
    $matches = array_intersect($buy2_coupons, $applied_coupons);

    // return if no coupon matches
    if (empty($matches)) return;

    // loop through the items in cart to find the cheapest
    foreach ( $cart->get_cart() as $cart_item_key => $values ) {
        $_product = $values['data'];
        $product_price[] = $_product->get_price_including_tax();
    }

    $cheapest = min($product_price);

    $cart->add_fee( 'Coupon 2FOR1WOW', -$cheapest, true, 'standard' );
}

【问题讨论】:

  • 问题的标题是不是有点像woocommerce产品的营销口号?

标签: php wordpress woocommerce cart coupon


【解决方案1】:

自 WooCommerce 3+ 起,WC_Product get_price_including_tax() 已被弃用,并已被函数 wc_get_price_excluding_tax() 取代(不再是方法)。

您应该不需要任何优惠券代码来使这种代码工作。当您使用它们来触发“买一送一”的折扣时,我将其保留在下面的功能上。

您在代码中忘记的是:

  • 在您在价格数组中添加产品价格时注意数量。
  • 当有多个不同数量的商品时,获取所有必要的最便宜的价格数组。

min() php 函数仅适用于一项,因此并不适用。

所以正确的代码应该是这样的:

add_action('woocommerce_cart_calculate_fees', 'buy_one_get_one_free', 10, 1 );
function buy_one_get_one_free( $wc_cart ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

    $cart_item_count = $wc_cart->get_cart_contents_count();
    if ( $cart_item_count < 2 ) return;

    // Set HERE your coupon codes
    $coupons_codes = array('2for1wow', 'anothercouponcode');
    $discount = 0; // initializing

    $matches = array_intersect( $coupons_codes, $wc_cart->get_applied_coupons() );
    if( count($matches) == 0 ) return;

    // Iterating through cart items
    foreach ( $wc_cart->get_cart() as $key => $cart_item ) {
        $qty = intval( $cart_item['quantity'] );
        // Iterating through item quantities
        for( $i = 0; $i < $qty; $i++ )
            $items_prices[] = floatval( wc_get_price_excluding_tax( $cart_item['data'] ) );
    }
    asort($items_prices); // Sorting cheapest prices first

    // Get the number of free items (detecting odd or even number of prices)
    if( $cart_item_count % 2 == 0 ) $free_item_count = $cart_item_count / 2;
    else  $free_item_count = ($cart_item_count - 1) / 2;

    // keeping only the cheapest free items prices in the array
    $free_item_prices = array_slice($items_prices, 0, $free_item_count);

    // summing prices for this free items
    foreach( $free_item_prices as $key => $item_price )
        $discount -= $item_price;

    if( $discount != 0 ){
        // The discount
        $label = '"'.reset($matches).'" '.__("discount");
        $wc_cart->add_fee( $label, number_format( $discount, 2 ), true, 'standard' );
        # Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false)
    }
}

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

此代码在 Woocommerce 3+ 上进行了测试并且可以正常工作。你会得到类似的东西:

需要以零价格折扣创建优惠券代码(此处为 2for1wow)。将优惠券应用于购物车时,将启用负费用(折扣),并将一件物品免费两件。


类似答案:WooCommerce discount: buy one get one 50% off

【讨论】:

  • 谢谢 LoicTheAztec!但它不起作用:) 我已经升级到最新版本的 WP 和 Woo,认为这可能是问题所在,但无济于事。同样的事情发生了:没有任何计算。你可以在这里看到它的实际效果(即使它是法语的):boutique.maison4tiers.com 你至少需要添加 2 张 DVD 来测试它。 DVD 是这样标记的。不知道为什么自升级以来所有产品都标记为红色。 p.s.:这里如何给 cmets 添加行尾?
  • 嗨 Loic,关于您的说明,我不需要优惠券代码:在这种情况下是的,因为我希望只有拥有优惠券代码的客户才能从此促销中受益。
  • 这是我到达你的页面时得到的 --> screencast.com/t/Otechua5B
  • 对我来说也一样,没有什么适用的。它应该可以在不手动创建优惠券的情况下工作,对吗?
猜你喜欢
  • 2018-02-26
  • 2018-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-12
  • 1970-01-01
  • 1970-01-01
  • 2020-12-31
相关资源
最近更新 更多