wordpress 如何在WooCommerce中根据购物车数量和产品属性对可变和单个产品应用折扣?

1bqhqjot  于 2023-08-03  发布在  WordPress
关注(0)|答案(1)|浏览(97)

我试图应用一个百分比折扣的基础上的产品与特定的产品属性的数量,在购物车.
更确切地说,我的目标是应用15%折扣的订单最少6产品与属性flaske.
我已经成功地实现了变量属性集的可变产品,但我似乎不能针对单一/简单的产品。
到目前为止我的代码(借用自Condition for Quantity and Price for Woocommerce):

// Discount based on product quantity and attribute in cart
add_action( 'woocommerce_cart_calculate_fees','wc_cart_item_quantity_discount' );
function wc_cart_item_quantity_discount( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // INITIALIZING VARIABLES
    $min_item_amount = 6; // Min quantity
    $discount = $items_count = $percent = $items_subtotal = 0;
    $taxonomy   = 'pa_variant'; // Taxonomy
    $term_slugs = array('flaske'); // Term/terms
    // Loop through cart items
    foreach( $cart->get_cart() as $cart_item_key => $cart_item ) {
      // Loop through variation
      foreach( $cart_item['variation'] as $attribute => $term_slug ) {
        // Only counting items that are above 6 and has attribute
        if( $cart_item['data']->get_price() >= $min_item_amount && $attribute === 'attribute_'.$taxonomy && in_array( $term_slug, $term_slugs ) ) {
            $items_count += $cart_item['quantity'];
            $items_subtotal += $cart_item['line_subtotal'];
        }
      }
    }
    // CONDITIONAL PERCENTAGE
    if ($items_count >= 6 ) {
        $percent = 15;
    }
    // DISCOUNT (TAXABLE)
    if( $items_count > 0 ) {
        // Calculation
        $discount -= ($items_subtotal / 100) * $percent;
        $cart->add_fee( __( "Mix & Match rabat - $percent%", "woocommerce" ), $discount, true);
    }
}

字符串
我当前的代码对于变量产品(变体)非常有效,但它似乎不会影响单个产品,即使我给单个产品赋予与变量产品相同的属性。
我怀疑这与foreach循环foreach( $cart_item['variation'] as $attribute => $term_slug )有关
我如何才能使它在一般情况下工作,所以它也适用于具有相同属性flaske的单一/简单产品?
任何帮助和建议将不胜感激。

其他有用的参考资料:

Woocommerce percentage discount per item based on quantity
Exclude variations with 2 specific attribute terms from coupon usage in Woocommerce

gxwragnw

gxwragnw1#

在简单产品或可变产品中,您无法获得产品属性集,这与产品变体相同。
以下重新访问的代码也应该处理简单的产品(未测试):

add_action( 'woocommerce_cart_calculate_fees','wc_cart_item_quantity_discount' );
function wc_cart_item_quantity_discount( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // INITIALIZING VARIABLES
    $discount = $items_count = $percent = $items_subtotal = 0;
    $taxonomy   = 'pa_variant'; // Taxonomy
    $term_slugs = array('flaske'); // Term/terms
    // Loop through cart items
    foreach( $cart->get_cart() as $cart_item_key => $cart_item ) {
        $product = $cart_item['data'];

        // Product variations
        if( $product->is_type('variation') ) {
            // Loop through variation attributes
            foreach ($cart_item['variation'] as $attribute => $term_slug) {
                // Only counting items that are above 6 and has attribute
                if ($attribute === 'attribute_' . $taxonomy && in_array($term_slug, $term_slugs)) {
                    $items_count += $cart_item['quantity'];
                    $items_subtotal += $cart_item['line_subtotal'];
                }
            }
        } 
        // Simple products
        elseif ( $product->is_type('simple') ) {
            $attributes = $product->get_attributes();

            if( ! empty($attributes) && array_key_exists($taxonomy, $attributes) ) {
                $terms = (array) $attributes[$taxonomy]->get_terms(); // array of WP_Term objects
                $slugs = array_map(function($term) { return $term->slug; }, $terms); // Extract only the term slugs

                if (count( array_intersect($slugs, $term_slugs) ) > 0 ) {
                    $items_count += $cart_item['quantity'];
                    $items_subtotal += $cart_item['line_subtotal'];
                }
            }
        }
    }
    // CONDITIONAL PERCENTAGE
    if ($items_count >= 6) {
        $percent = 15;
    }
    // DISCOUNT (TAXABLE)
    if ($items_count > 0) {
        // Calculation
        $discount -= ($items_subtotal / 100) * $percent;
        $cart->add_fee(__("Mix & Match rabat - $percent%", "woocommerce"), $discount, true);
    }
}

字符串
应该能用

相关问题