WooCommerce에서 장바구니 수량 및 제품 속성에 따라 변수 및 개별 제품에 할인을 적용하는 방법은 무엇입니까?
P粉592085423
P粉592085423 2023-07-23 13:26:53
0
1
577

특정 제품 속성으로 장바구니에 담긴 제품 수를 기준으로 백분율 할인을 적용하려고 합니다.

더 정확하게는 '플라스크' 속성이 있는 상품을 6개 이상 구매 시 15% 할인을 적용하는 것이 목표입니다.

가변 속성이 설정된 제품에서는 이 작업을 성공적으로 수행했지만 단일/단순 제품에서는 수행할 수 없는 것 같습니다.

지금까지 제가 작성한 코드입니다. (수량 및 가격 조건은 WooCommerce에서 빌려왔습니다.)

// 根据购物车中的产品数量和属性进行折扣。
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;

    // 初始化变量。
    $min_item_amount = 6; // 最小数量。
    $discount = $items_count = $percent = $items_subtotal = 0;
    $taxonomy   = 'pa_variant'; // 分类
    $term_slugs = array('flaske'); // 术语
    // 遍历购物车中的物品
    foreach( $cart->get_cart() as $cart_item_key => $cart_item ) {
      // 遍历变体
      foreach( $cart_item['variation'] as $attribute => $term_slug ) {
        // 只计算具有属性且数量超过6的物品。
        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'];
        }
      }
    }
    // 条件百分比。
    if ($items_count >= 6 ) {
        $percent = 15;
    }
    // 折扣(应税)。
    if( $items_count > 0 ) {
        // 计算
        $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" 속성을 가진 단일/간단한 제품에도 작동하도록 일반적으로 작동하게 만드는 방법은 무엇입니까?

여러분의 도움과 제안에 진심으로 감사드립니다.

P粉592085423
P粉592085423

모든 응답(1)
P粉323374878

수량에 따라 각 항목에 대한 WooCommerce 백분율 할인

WooCommerce에서 2개의 특정 속성 용어가 있는 변형을 쿠폰 사용에서 제외

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;

    // 初始化变量。
    $min_item_amount = 6; // 最小数量
    $discount = $items_count = $percent = $items_subtotal = 0;
    $taxonomy   = 'pa_variant'; // 分类
    $term_slugs = array('flaske'); 
    // 遍历购物车中的物品
    foreach( $cart->get_cart() as $cart_item_key => $cart_item ) {
        $product = $cart_item['data'];
        $price   = $product->get_price();

        // 产品变种
        if( $product->is_type('variation') ) {
            // 遍历变种属性
            foreach ($cart_item['variation'] as $attribute => $term_slug) {
                // 只计算具有属性且数量大于6的物品。
                if ($price >= $min_item_amount && $attribute === 'attribute_' . $taxonomy && in_array($term_slug, $term_slugs)) {
                    $items_count += $cart_item['quantity'];
                    $items_subtotal += $cart_item['line_subtotal'];
                }
            }
        } 
        // 简单产品
        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 ($price >= $min_item_amount && count( array_intersect($slugs, $term_slugs) ) > 0 ) {
                    $items_count += $cart_item['quantity'];
                    $items_subtotal += $cart_item['line_subtotal'];
                }
            }
        }
    }
    // 条件百分比
    if ($items_count >= 6) {
        $percent = 15;
    }
    // 折扣(应税)。
    if ($items_count > 0) {
        // 计算
        $discount -= ($items_subtotal / 100) * $percent;
        $cart->add_fee(__("Mix & Match rabat - $percent%", "woocommerce"), $discount, true);
    }
}

이렇게 하면 작동합니다

최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!