简体   繁体   English

根据 WooCommerce 购物车小计自动添加可变数量的特定产品

[英]Auto add a variable quantity of specific product based on WooCommerce cart subtotal

I'm trying to add a gift product to a shopping cart based upon the total cost spent by a customer.我正在尝试根据客户花费的总成本将礼品产品添加到购物车。 It's for a restaurant that sells gift vouchers.这是一家出售礼券的餐厅。 The scheme runs as follows: for every £50 that a customer spends in a single transaction, they automatically receive a £10 gift voucher for free.该计划运行如下:客户在单次交易中每消费 50 英镑,他们将自动免费获得 10 英镑的礼券。 So, if they spend £100 on gift vouchers, they receive 2x £10 gift vouchers for free (and so on).因此,如果他们在礼券上花费 100 英镑,他们将免费获得 2 x 10 英镑的礼券(依此类推)。 To keep things as simple as possible, we're limiting denominations of gift vouchers (£25 - £250 increasing in £25 denominations).为了让事情尽可能简单,我们限制了礼券的面额(25 英镑 - 250 英镑,25 英镑面额增加)。 I don't particularly want to pay for/install another plugin that will need to be updated/managed, etc. If I'm honest, it's only for the Christmas period so a snippet of code to do this would be far better.我并不特别想支付/安装另一个需要更新/管理的插件等。老实说,这只是圣诞节期间,所以执行此操作的代码片段会好得多。 The way I see it working is that a customer adds the vouchers to the cart and when they go to the cart it shows they've also got the additional 'free' vouchers in there too.我认为它的工作方式是客户将代金券添加到购物车,当他们进入购物车时,它显示他们也有额外的“免费”代金券。

I've found some code online and made some changes to it.我在网上找到了一些代码并对其进行了一些更改。 The code is as follows:代码如下:

   
function auto_add_specific_product_to_cart() {
           
   // select product ID
   $product_id = 1428;
           
   // if cart empty, add it to cart
   if ( WC()->cart->get_cart_total() <= 99 ) {
WC()->cart->add_to_cart( $product_id, 1 ); }
 
else if ( WC()->cart->get_cart_total() <= 149 ) {
WC()->cart->add_to_cart( $product_id, 2 ); }
 
else if ( WC()->cart->get_cart_total() <= 199 ) {
WC()->cart->add_to_cart( $product_id, 3 ); }
 
else if ( WC()->cart->get_cart_total() <= 249 ) {
WC()->cart->add_to_cart( $product_id, 4 ); }
     
}

I'm having problems with it right, left and centre.我在右边、左边和中间都有问题。 It keeps adding the free vouchers to the shopping cart so eventually customers end up with 20 or 30 vouchers there;它不断地将免费代金券添加到购物车中,因此最终客户最终会在那里获得 20 或 30 份代金券; the PHP for recognising the different thresholds (50 to 99, 100 to 149, etc) aren't working properly.用于识别不同阈值(50 到 99、100 到 149 等)的 PHP 无法正常工作。 I'm probably making this far too complicated and not thinking it through but a bit of help would be great.我可能把这弄得太复杂了,没有考虑清楚,但有一点帮助会很好。 I've found plenty of code online for discounting products but nothing that gives something away for free.我在网上找到了很多打折产品的代码,但没有任何东西可以免费赠送。

Here a variable quantity of a "specific product voucher" will be auto added to cart based on cart subtotal.这里将根据购物车小计自动添加到购物车中的“特定产品凭证”的可变数量。

The code below is based on Add free gifted product for a minimal cart amount in WooCommerce answer code.下面的代码基于在 WooCommerce答案代码中以最少的购物车数量添加免费赠品 The code automate the generation of cart subtotal thresholds (min and max amounts) and the related quantity of the voucher product to be added to cart.该代码自动生成购物车小计阈值(最小和最大金额)以及要添加到购物车的凭证产品的相关数量。

The number set in $max_quantity variable, determines the number of iterations on the FOR loop (so the number of different generated thresholds and the max quantity of voucher product that can be added to cart) . $max_quantity变量中设置的数量决定了FOR循环上的迭代次数(因此不同生成的阈值的数量和可以添加到购物车的凭证产品的最大数量)

// Auto add a quantity of voucher product based on cart subtotal
add_action( 'woocommerce_before_calculate_totals', 'auto_add_voucher_product_based_on_subtotal' );
function auto_add_voucher_product_based_on_subtotal( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Settings
    $voucher_product_id = 37; // Voucher product Id
    $max_quantity       = 20; // Here set the max item quantity (for voucher product)
    $min_subtotal       = 50;  // Starting threshold min amount
    $cart_subtotal      = 0;  // Initializing

    // Loop through cart items (first loop)
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
        // When voucher product is is cart
        if ( $voucher_product_id == $cart_item['product_id'] ) {
            $voucher_key = $cart_item_key;
            $voucher_qty = $cart_item['quantity'];
            // $cart_item['data']->set_price(0); // (optional) set the price to zero
        } else {
            $cart_subtotal += $cart_item['line_total'] + $cart_item['line_tax']; // Get subtotal
        }
    }

    // If voucher product is not already in cart, add it
    if ( ! isset($voucher_key) && $cart_subtotal >= $min_subtotal ) {
        $cart->add_to_cart( $voucher_product_id );
    }
    // Check vouvher product quantity and adjust it depending on the subtotal amount.
    else {
        // Loop dynamically through subtotal threshold min and max amounts setting the right quantity
        // $i is the min amount, $j the max amount and $q the related quantity to check
        for ( $i = $min_subtotal, $j = 100, $q = 1; $q < $max_quantity; $i += 50, $j += 50, $q++ ) {
            if ( $cart_subtotal >= $i && $cart_subtotal < $j && $voucher_qty != $q ) {
                $cart->set_quantity( $voucher_key, $q );
            }
        }
        if ( $cart_subtotal >= $i && $voucher_qty != $q ) {
            $cart->set_quantity( $voucher_key, $q );
        }
    }
}

Code goes in functions.php file of the active child theme (or active theme).代码位于活动子主题(或活动主题)的 functions.php 文件中。 Tested and works.测试和工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据Woocommerce购物车小计自动为特定产品应用优惠券 - Auto apply a coupon for a specific product based on Woocommerce cart subtotal 基于 Woocommerce 中特定产品类别购物车项目数量的购物车费用 - Cart fee based on specific product category cart item quantity in Woocommerce 在 WooCommerce 产品页面中显示数量变化的产品总数和购物车小计 - Display product total and cart subtotal on quantity change in WooCommerce product page 根据 WooCommerce 产品类别禁用特定的购物车项目数量字段 - Disable specific cart item quantity fields based on WooCommerce product category 根据 Woocommerce 中的产品数量替换特定的购物车项目 - Replace specific cart item based on product quantity in Woocommerce 根据Woocommerce中的其他购物车项目自动将特定产品添加到购物车 - Auto add a specific product to cart based on other cart items count in Woocommerce 为 WooCommerce 中特定产品类别的购物车项目自动添加产品 - Auto add a product for cart item from specific product categories in WooCommerce Woocommerce将购物车中特定产品的数量相乘 - Woocommerce multiply the quantity of a specific product in cart 在Woocommerce 3中自动将产品添加到购物车 - Auto add a Product to Cart in Woocommerce 3 根据购物车页面中的属性值和产品数量,在 WooCommerce 中添加到购物车验证 - Add to cart validation in WooCommerce based on attribute value and product quantity from the cart page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM