php WooCommerce中的折扣规则,不包括具有用户角色条件的类别

huwehgph  于 2023-06-20  发布在  PHP
关注(0)|答案(1)|浏览(121)

我使用这个代码,它给客户的折扣,基于产生折扣的具体金额范围2% 4% 6%对于普通买家和3% 5% 7%对于客户的作用“贵宾”.
代码还必须排除我添加的类别。如果客户将属于这些产品的产品放入购物车中,则不应将其与它们一起计算。反正现在他们也算,我没法让它工作。
折扣步骤正常工作,但不排除我指定要排除的类别中的产品。当我有一个绿色的t恤在它计数,当我把一个红色的排除t恤在它仍然计数,无论如何,即使坚韧,它不应该。

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

    // excluding categories here
    $excluded_categories = array( 'redshirt1', 'redshirt2', 'redshirt3', 'redshirt4' );
    $discounted_total = 0;

    foreach ( $cart->get_cart() as $cart_item ) {
        $product = $cart_item['data'];
        $product_id = $product->get_id();
        $terms = get_the_terms( $product_id, 'product_cat' );

        $exclude = false;
        foreach ( $terms as $term ) {
            if ( in_array( $term->slug, $excluded_categories ) ) {
                $exclude = true;
                break;
            }
        }

        if ( ! $exclude ) {
            $discounted_total += $cart_item['line_subtotal'];
        }
    }

    $discount = 0;
    $discount_text = "";

    // check if the user is in the role 'vip'
    $user = wp_get_current_user();
    $is_customer = in_array( 'vip', (array) $user->roles );

    if ( $discounted_total >= 2000 && $discounted_total < 4000 ) {
        $discount = $discounted_total * ($is_customer ? 0.03 : 0.02); // 3% for vip, 2% for others
        $discount_text = 'Discount ' . ($is_customer ? '3%' : '2%');
    } else if ( $discounted_total >= 4000 && $discounted_total < 6000 ) {
        $discount = $discounted_total * ($is_customer ? 0.05 : 0.04); // 5% for vip, 4% for others
        $discount_text = 'Discount ' . ($is_customer ? '5%' : '4%');
    } else if ( $discounted_total >= 6000 ) {
        $discount = $discounted_total * ($is_customer ? 0.07 : 0.06); // 7% for vip, 6% for others
        $discount_text = 'Discount ' . ($is_customer ? '7%' : '6%');
    }

    if ( $discount > 0 ) {
        $cart->add_fee( __( $discount_text, 'woocommerce' ), -$discount );
    }
}

add_action( 'woocommerce_before_cart', 'show_discount_message' );
function show_discount_message() {
    $cart = WC()->cart;
    $total = $cart->subtotal;

    // excluding categories
    $excluded_categories = array( 'redshirt1', 'redshirt2', 'redshirt3', 'redshirt4' );
    $discounted_total = $total;

    foreach ( $cart->get_cart() as $cart_item ) {
        $product = $cart_item['data'];
        $product_cats = wp_get_post_terms( $product->get_id(), 'product_cat' );

        foreach ( $product_cats as $product_cat ) {
            if ( in_array( $product_cat->slug, $excluded_categories ) ) {
                $discounted_total -= $cart_item['line_subtotal'];
                break;
            }
        }
    }

    // check if the user is in role 'vip'
    $user = wp_get_current_user();
    $is_customer = in_array( 'vip', (array) $user->roles );

    $return_to = wc_get_page_permalink( 'shop' );

    if ( $discounted_total < 2000 ) {
        $difference = 2000 - $discounted_total;
        $added_text = 'you have ' . wc_price( $difference ) . ' untill you reach level 1 (' . ($is_customer ? '3%' : '2%') . ') on the discount!';
        $notice = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( $return_to ), 'Keep shopping', $added_text );
        wc_print_notice( $notice, 'notice' );
    } else if ( $discounted_total >= 2000 && $discounted_total < 4000 ) {
        $difference = 4000 - $discounted_total;
        $added_text = 'you have ' . wc_price( $difference ) . ' untill you reach level 2 (' . ($is_customer ? '5%' : '4%') . ') on the discount!';
        $notice = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( $return_to ), 'Keep shopping', $added_text );
        wc_print_notice( $notice, 'notice' );
    } else if ( $discounted_total >= 4000 && $discounted_total < 6000 ) {
        $difference = 6000 - $discounted_total;
        $added_text = 'you have ' . wc_price( $difference ) . ' untill you reach level 3 (' . ($is_customer ? '7%' : '6%') . ') on the discount!';
        $notice = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( $return_to ), 'Keep shopping', $added_text );
        wc_print_notice( $notice, 'notice' );
    }
}
sqxo8psd

sqxo8psd1#

在WooCommerce中,产品类别和标签不是由产品变量处理的,而是由父变量产品处理的。因此,对于变体购物车项目,您需要获取父产品ID,以便能够定位特定的产品类别。还有其他一些错误。
下面是你重新访问的代码:

// Custom conditional function for excluded product categories
function has_excluded_product_categories( $product ) {
    // Set HERE your Excluded product categories (slugs)
    $excl_terms = array( 'redshirt1', 'redshirt2', 'redshirt3', 'redshirt4', 'hoodies' );
    // IMPORTANT!!!: Handling Product variations 
    $product_id = $product->get_parent_id() ? $product->get_parent_id() : $product->get_id();
    // Simplifying: Get directly an array of product categories "slugs"
    $term_slugs = wp_get_post_terms( $product_id, 'product_cat', array( 'fields' => 'slugs' ) );
    // Using array_intersect() on 2 arrays instead of a heavier foreach loop
    return (bool) count( array_intersect($term_slugs, $excl_terms) ) > 0;
}

// Adding a discount conditionally
add_action( 'woocommerce_cart_calculate_fees', 'custom_discount_based_on_cart_total', 10, 1 );
function custom_discount_based_on_cart_total( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Initializing variables
    $discounted_total = 0; 
    $discount = 0; 
    $discount_text = __('Discount ', 'woocommerce');

    foreach ( $cart->get_cart() as $cart_item ) {
        if ( ! has_excluded_product_categories( $cart_item['data'] ) ) {
            $discounted_total += $cart_item['line_subtotal']; // add item line subtotal
        }
    }

    $user = wp_get_current_user();  
    $is_customer = in_array( 'vip', (array) $user->roles ); // check if the user is in the role 'vip'

    if ( $discounted_total >= 2000 && $discounted_total < 4000 ) {
        $discount = $discounted_total * ($is_customer ? 0.03 : 0.02); // 3% for vip, 2% for others
        $discount_text .=  $is_customer ? '3%' : '2%';
    } else if ( $discounted_total >= 4000 && $discounted_total < 6000 ) {
        $discount = $discounted_total * ($is_customer ? 0.05 : 0.04); // 5% for vip, 4% for others
        $discount_text .= $is_customer ? '5%' : '4%';
    } else if ( $discounted_total >= 6000 ) {
        $discount = $discounted_total * ($is_customer ? 0.07 : 0.06); // 7% for vip, 6% for others
        $discount_text .= $is_customer ? '7%' : '6%';
    }

    if ( $discount > 0 ) {
        $cart->add_fee( $discount_text, -$discount );
    }
}

// Display related discount message
add_action( 'woocommerce_before_cart', 'show_discount_message' );
function show_discount_message() {
    $discounted_total = 0; 

    foreach ( WC()->cart->get_cart() as $cart_item ) {
        if ( ! has_excluded_product_categories( $cart_item['data'] ) ) {
            $discounted_total = $cart_item['line_subtotal']; // add item line subtotal
        }
    }

    // check if the user is in role 'vip'
    $user = wp_get_current_user();
    $is_customer = in_array( 'vip', (array) $user->roles );
    $level = 0;
    $return_to = esc_url( wc_get_page_permalink( 'shop' ) );

    if ( $discounted_total < 2000 ) {
        $difference = 2000 - $discounted_total;
        $level = 1;
        $percentage = $is_customer ? '3%' : '2%';
    } else if ( $discounted_total >= 2000 && $discounted_total < 4000 ) {
        $difference = 4000 - $discounted_total;
        $level = 2;
        $percentage = $is_customer ? '5%' : '4%';
    } else if ( $discounted_total >= 4000 && $discounted_total < 6000 ) {
        $difference = 6000 - $discounted_total;
        $level = 3;
        $percentage = $is_customer ? '7%' : '6%';
    }

    if ( $level != 0 ) {
        $added_text = sprintf( __('You have %s untill you reach level %d (%s) on the discount!', 'woocommerce'), 
            wc_price( $difference ), $level, $percentage );

        wc_print_notice( sprintf(  '<a href="%s" class="button wc-forward">%s</a> %s',  $return_to,
            __('Keep shopping', 'woocommerce'), $added_text  ), 'notice' );
    }
}

代码放在活动子主题(或活动主题)的functions.php文件中。测试和作品。
注意:我没有测试“vip”用户角色

相关问题