【发布时间】:2021-02-02 15:49:33
【问题描述】:
我使用的代码在购物车中和结帐时根据Do not give discounts for on sale products when selecting “local pickup” in WooCommerce 选择“本地取货”时可享受 20% 的折扣
/**
* Discount for Local Pickup
*/
add_action( 'woocommerce_cart_calculate_fees', 'custom_discount_for_pickup_shipping_method', 10, 1 );
function custom_discount_for_pickup_shipping_method( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$percentage = 20; // Discount percentage
$chosen_shipping_method_id = WC()->session->get( 'chosen_shipping_methods' )[0];
$chosen_shipping_method = explode(':', $chosen_shipping_method_id)[0];
// Only for Local pickup chosen shipping method
if ( strpos( $chosen_shipping_method_id, 'local_pickup' ) !== false ) {
// Set variable
$new_subtotal = 0;
// Loop though each cart items and set prices in an array
foreach ( $cart->get_cart() as $cart_item ) {
// Get product
$product = wc_get_product( $cart_item['product_id'] );
// Product has no discount
if ( ! $product->is_on_sale() ) {
// line_subtotal
$line_subtotal = $cart_item['line_subtotal'];
// Add to new subtotal
$new_subtotal += $line_subtotal;
}
}
// Calculate the discount
$discount = $new_subtotal * $percentage / 100;
// Add the discount
$cart->add_fee( __('Discount') . ' (' . $percentage . '%)', -$discount );
}
}
告诉我,如果产品属于某些类别,如何确保不享受本地提货折扣?我必须手动指定类别名称,例如“steak”、“sauce”和“bread”。
以及如何通知客户“提货折扣不适用于”Category Name“类别”的产品?
我会很高兴得到您的帮助!
【问题讨论】:
标签: php wordpress woocommerce