简体   繁体   English

仅当特定产品在 WooCommerce 购物车中时,才根据特定产品类别应用/确定折扣

[英]Apply/determine discount based on a particular product category only when a specific product is in WooCommerce cart

I would like to replicate promo for a specific item.我想复制特定项目的促销。

If you buy one (b10 plus) product then you're eligible for a total 289€ discount on all light shaping tools, so if you add to cart accessories for a "category subtotal" of 100€ you get 100€ discount, if you take 300€ accessories you get 289€ discount.如果您购买一件 (b10 plus) 产品,那么您有资格在所有塑光工具上享受总计 289 欧元的折扣,因此,如果您将 100 欧元的“类别小计”添加到购物车配件,您将获得 100 欧元的折扣,如果您购买 300 欧元的配件,您可以获得 289 欧元的折扣。

I tried with another solution (plugins and php code) but it keeps discounting each item (that correspond to "accessories") for $289.我尝试了另一种解决方案(插件和 php 代码),但它一直以 289 美元的价格为每个项目(对应于“配件”)打折。

I also tried to include automatically a discount for that category if the "B10 plus" is in the cart.如果“B10 plus”在购物车中,我还尝试自动包含该类别的折扣。 but this code add the discount to single products但此代码将折扣添加到单个产品

This is my current code:这是我当前的代码:

add_action( 'woocommerce_before_cart', 'bbloomer_apply_matched_coupons' );
  
function bbloomer_apply_matched_coupons() {
  
    $coupon_code = 'promob10'; 
  
    if ( WC()->cart->has_discount( $coupon_code ) ) return;
  
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
  
    // this is your product ID
    $autocoupon = array( 373 );
  
    if ( in_array( $cart_item['product_id'], $autocoupon ) ) {   
        WC()->cart->apply_coupon( $coupon_code );
        wc_print_notices();
    }
  
    }
  
}

To apply a discount based on a specific product ID you can use the woocommerce_cart_calculate_fees hook要根据特定产品 ID 应用折扣,您可以使用woocommerce_cart_calculate_fees挂钩

  1. First of all you will have to determine the specific product ID, this corresponds to the b10 product from your question首先,您必须确定具体的产品 ID,这对应于您问题中的 b10 产品
  2. Then it will be checked whether this specific product is in the cart via find_product_in_cart() .然后将通过find_product_in_cart()检查此特定产品是否在购物车中。 If that's the case, let's move on如果是这样,让我们​​继续
  3. Through the price of the specific product ID, we determine the maximum discount通过特定产品ID的价格,我们确定最大折扣
  4. The price of the products belonging to the particular category is added to total discount (assuming that the b10 product does not belong to this category)将属于特定类别的产品的价格添加到总折扣中(假设 b10 产品不属于此类别)
  5. If the total discount is less than the maximum discount, we will use this discount.如果总折扣小于最大折扣,我们将使用此折扣。 If not, the maximum discount will be applied如果没有,将应用最大折扣

So you get:所以你得到:

function action_woocommerce_cart_calculate_fees( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Product ID of the specific product
    $specific_product_id = 817;
    
    // The term name/term_id/slug to check for.
    $category = 'accessories';
    
    // Initialize
    $total_discount = 0;
    $maximum_discount = 0;

    // Cart id
    $product_cart_id = $cart->generate_cart_id( $specific_product_id );

    // Find product in cart
    $in_cart = $cart->find_product_in_cart( $product_cart_id );
    
    // In cart
    if ( $in_cart ) {       
        // Gets cart contents
        foreach ( $cart->get_cart_contents() as $cart_item ) {
            // Get product id
            $product_id = $cart_item['product_id'];
            
            // Compare
            if ( $product_id == $specific_product_id ) {
                // Get price = maximum discount
                $maximum_discount = $cart_item['data']->get_price();
            }

            // Has certain category     
            if ( has_term( $category, 'product_cat', $product_id ) ) {
                // Get price
                $price = $cart_item['data']->get_price();
                
                // Get quantity
                $quantity = $cart_item['quantity'];
                
                // Addition to the total discount
                $total_discount += $price * $quantity;
            }
        }

        // Less than
        if ( $total_discount < $maximum_discount ) {
            // Add total discount
            $cart->add_fee( __( 'Discount applied', 'woocommerce' ), -$total_discount, false );
        } else {
            // Add maximum discount         
            $cart->add_fee( __( 'Discount applied', 'woocommerce' ), -$maximum_discount, false );
        }
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'action_woocommerce_cart_calculate_fees', 10, 1 );

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

相关问题 WooCommerce 特定优惠券折扣基于产品类别 - WooCommerce specific coupon discount based on product category 基于Woocommerce中特定产品变化的购物车项目折扣 - Cart item discount based on specific product variation in Woocommerce "基于数量计算的产品类别的购物车折扣" - Cart discount for a product category based on quantity calculations 基于 Woocommerce 中特定产品类别购物车项目数量的购物车费用 - Cart fee based on specific product category cart item quantity in Woocommerce 根据 WooCommerce 产品类别禁用特定的购物车项目数量字段 - Disable specific cart item quantity fields based on WooCommerce product category 根据Woocommerce购物车小计自动为特定产品应用优惠券 - Auto apply a coupon for a specific product based on Woocommerce cart subtotal 当来自特定产品类别的商品在 Woocommerce 的购物车中时禁用购物 - Disable shopping when an item from a specific product category is in cart in Woocommerce WooCommerce 中特定产品标签 ID 的购物车批量折扣 - Cart bulk quantity discount for specific product tag IDs in WooCommerce Woocommerce中基于产品类别的第二项的数量折扣 - Quantity Discount for 2nd Item Based on Product Category in Woocommerce 如果购物车包含特定的 Woocommerce 产品类别,则阻止添加到购物车 - Prevent add to cart if cart contains a specific Woocommerce product category
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM