[英]Local Pickup fixed discount by product for specific products in WooCommerce
在我们的商店中,我们希望为客户提供提货的选择。 对于 X 类产品,如果客户选择提货选项,我们希望为客户提供每种产品 6 欧元的折扣。 如果他们不选择接机选项,情况会更加复杂。 5.95 的运费不适用于 X 类产品。
我尝试使用这段代码,但无法弄清楚。 如果我们可以通过它们的 id 而不是它们的类别来识别这些产品,那将是最好的
/**
* 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;
$discount_amount = 6; // Discount
$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;
// Set discount excluded categories list
$arr_discount_excluded_category = ['merch', 'rum'];
// Set variable for matched excluded category from the cart list
$arr_discount_excluded_category_matched = [];
// 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'] );
// Get product category
$category_list = wp_get_post_terms($cart_item['product_id'],'product_cat',array('fields'=>'names'));
// Product has no discount
$arr_discount_excluded_category_matched_by_item = array_intersect($category_list, $arr_discount_excluded_category);
if ( ! $product->is_on_sale() && empty($arr_discount_excluded_category_matched_by_item)) {
// line_subtotal
$line_subtotal = $cart_item['line_subtotal'];
// Add to new subtotal
$new_subtotal += $line_subtotal;
}
else{
$arr_discount_excluded_category_matched = array_merge($arr_discount_excluded_category_matched, $arr_discount_excluded_category_matched_by_item);
}
}
// Calculate the discount
$discount = 0;
if($new_subtotal > 0){
$discount = $new_subtotal - $discount_amount;
}
//Add notification
if(!empty($arr_discount_excluded_category_matched)){
$str_message = 'Pickup discount does not apply to products from the Category "' . implode('", "', array_unique($arr_discount_excluded_category_matched)) . '"';
wc_add_notice($str_message, 'notice');
}
// Add the discount
$cart->add_fee( __('Discount') . ' (' . $discount_amount . '€)', -$discount );
}
}
The following will add a fixed discount by product only to specific defined products when Local Pickup shipping method is selected.
在下面的代码中定义所需的产品 ID 和产品折扣金额:
add_action( 'woocommerce_cart_calculate_fees', 'fixed_discount_by_product_for_pickup_shipping_method', 10, 1 );
function fixed_discount_by_product_for_pickup_shipping_method( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$discount_by_product = 6; // Here set the discount amount by product
$targeted_product_ids = array(37,40); // Here set your excluded product ids
$chosen_shipping_id = WC()->session->get('chosen_shipping_methods')[0];
$total_discount = 0; // Initializing
// Only for "Local pickup" shipping method
if ( strpos( $chosen_shipping_id, 'local_pickup' ) !== false ) {
// Loop though each cart items and set prices in an array
foreach ( $cart->get_cart() as $item ) {
// Get matching targeted product ids from the current cart item
$matched_product_ids = array_intersect( array($item['product_id'], $item['variation_id']), $targeted_product_ids);
// If product matches with targeted product Ids
if ( ! empty($matched_product_ids) ) {
// Add the discount by product
$total_discount += $discount_by_product;
}
}
if ( $total_discount > 0 ) {
$cart->add_fee( __('Local pickup discount'), -$total_discount ); // Add the discount
}
}
}
代码进入活动子主题(或活动主题)的functions.php文件。 测试和工作。
要在折扣计算中处理产品数量,请替换:
$total_discount += $discount_by_product;
和:
$total_discount += $discount_by_product * $item['quantity'];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.