繁体   English   中英

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

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

我想复制特定项目的促销。

如果您购买一件 (b10 plus) 产品,那么您有资格在所有塑光工具上享受总计 289 欧元的折扣,因此,如果您将 100 欧元的“类别小计”添加到购物车配件,您将获得 100 欧元的折扣,如果您购买 300 欧元的配件,您可以获得 289 欧元的折扣。

我尝试了另一种解决方案(插件和 php 代码),但它一直以 289 美元的价格为每个项目(对应于“配件”)打折。

如果“B10 plus”在购物车中,我还尝试自动包含该类别的折扣。 但此代码将折扣添加到单个产品

这是我当前的代码:

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();
    }
  
    }
  
}

要根据特定产品 ID 应用折扣,您可以使用woocommerce_cart_calculate_fees挂钩

  1. 首先,您必须确定具体的产品 ID,这对应于您问题中的 b10 产品
  2. 然后将通过find_product_in_cart()检查此特定产品是否在购物车中。 如果是这样,让我们​​继续
  3. 通过特定产品ID的价格,我们确定最大折扣
  4. 将属于特定类别的产品的价格添加到总折扣中(假设 b10 产品不属于此类别)
  5. 如果总折扣小于最大折扣,我们将使用此折扣。 如果没有,将应用最大折扣

所以你得到:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM