我使用这个代码,它给客户的折扣,基于产生折扣的具体金额范围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' );
}
}
1条答案
按热度按时间sqxo8psd1#
在WooCommerce中,产品类别和标签不是由产品变量处理的,而是由父变量产品处理的。因此,对于变体购物车项目,您需要获取父产品ID,以便能够定位特定的产品类别。还有其他一些错误。
下面是你重新访问的代码:
代码放在活动子主题(或活动主题)的functions.php文件中。测试和作品。
注意:我没有测试“vip”用户角色