2015-09-02 129 views
0

我想購物車的折扣基於 超過600美元訂單的5%和10%超過1000美元? 我可以讓它工作超過600美元,但不是超過1000美元。我上線15WooCommerce - 多個購物車dlicounts

add_action('woocommerce_before_cart', 'apply_matched_coupons'); 

function apply_matched_coupons() { 
    global $woocommerce; 

    $coupon_code = 'over600'; // your coupon code here 
    $coupon_codeb = 'over1000'; // your coupon code here 

    if ($woocommerce->cart->has_discount($coupon_code)) return; 

    if ($woocommerce->cart->cart_contents_total >= 600) { 
     $woocommerce->cart->add_discount($coupon_code); 
     $woocommerce->show_messages(); 
    } 
    if ($woocommerce->cart->cart_contents_total >= 1000) { 
     $woocommerce->cart->add_discount($coupon_codeb); 
     $woocommerce->show_messages(); 
    } 

} 
+0

歡迎StackOverflow的@dhcrain!請包括您所看到的錯誤消息的全文。 – eebbesen

+1

什麼是錯誤?我們該如何知道15號線在哪裏? – helgatheviking

回答

1

得到一個錯誤,最後只是一個完全不同的方式

function nh_custom_coupon_filter() { 
    global $woocommerce; 
    $excluded_amount = $discount_percent = 0; 
    $working_total = $woocommerce->cart->cart_contents_total; 
    $excluded_categories = array(
    217, # Training 
    223, # Starter Kits 
); 

    # Only apply manual discount if no coupons are applied 
    if (!$woocommerce->cart->applied_coupons) { 

    # Find any items in cart that belong to the restricted categories 
    foreach ($woocommerce->cart->cart_contents as $item) { 
     $product_categories = get_the_terms($item['product_id'], 'product_cat'); 
     if (empty($product_categories) || is_wp_error($product_categories) || !$product_categories) { 
     if (is_wp_error($product_categories)) { 
      wp_die($product_categories->get_error_message()); 
     } 
     else { 
      $product_categories = new WP_Error('no_product_categories', "The product \"".$item->post_title."\" doesn't have any categories attached, thus no discounts can be calculated.", "Fatal Error"); 
      wp_die($product_categories); 
     } 
     } 
     foreach ($excluded_categories as $excluded_category) { 
     foreach ($product_categories as $category) { 
      if ($excluded_category == $category->term_id) { 
      $excluded_amount += $item['line_subtotal']; # Increase our exclusion amount 
      $working_total -= $item['line_subtotal']; # Decrease our discountable amount 
      } 
     } 
     } 
    } 

    # Logic to determine WHICH discount to apply based on subtotal 
    if ($working_total >= 600 && $working_total < 1000) { 
     $discount_percent = 5; 
    } 
    elseif ($working_total >= 1000) { 
     $discount_percent = 10; 
    } 
    else { 
     $discount_percent = 0; 
    } 

    # Make sure cart total is eligible for discount 
    if ($discount_percent > 0) { 
     $discount_amount = ((($discount_percent/100) * $working_total) * -1); 
     $woocommerce->cart->add_fee('Bulk Discount', $discount_amount); 
    } 
    } 
} 
add_action('woocommerce_cart_calculate_fees', 'nh_custom_coupon_filter');