2016-08-12 6 views
0

クライアントには非常に奇妙なリクエストがあります:クーポンコードのセットをに追加したい場合は、を注文に追加します。これは可能ですか?WooCommerce - 注文に固定料金を追加するクーポンのセットを作成する

、彼らが解決しようとしている問題は、このです:

現在、店は私が出荷クラスと適切に構成されたテーブル率を持っている下48人の米国には送料無料を提供しています。ただし、顧客が1日の取引サイトで販売したクーポンは、送料無料の特典を削除し、一定の送料を加算することになっています。

どのようにこれを達成できますか?

答えて

4

はい、あなたは(クーポンのセットのために)そのスニペットコードでそれを行うことができます。

function coupon_add_cart_fee($bookable_total = 0) { 
    $has_coupon = false; 

    // Set here your specials coupons slugs (one by line - last one have no coma) 
    $coupon_codes = array (
     'your_coupon_code_slug1', 
     'your_coupon_code_slug2', 
     'your_coupon_code_slug3' 
    ); 

    // Set here your fee amount or make fees calculation (see the links in reference) 
    $fee = 20; 

    if (is_admin() && ! defined('DOING_AJAX')) 
     return; 

    // checking if that "special" coupon is applied to customer cart 
    foreach ($coupon_codes as $coupon_code) { 
     if (WC()->cart->has_discount($coupon_code)) { 

      // If yes apply the fee to the cart 
      if (!$has_coupon) { 
       $has_coupon = true; 
       break; 
      } 
     } 
    } 

    if ($has_coupon) { 
     WC()->cart->add_fee('Fees: ', $fee, false); 
    } 
} 
add_action('woocommerce_cart_calculate_fees','coupon_add_cart_fee'); 

このコードがテストされ、それが動作します。あなたのアクティブな子テーマまたはテーマのfunction.phpファイルに移動します。


add_fee() method

重要とTAXオプション: TAX add_fee() methodを働いたりされていないという事実は、woocommerceであなたの税金設定が最初に依存します。

クラスWC_Cart add_fee()メソッドは、カートに追加料金を追加します。

add_fee(string $name, float $amount, boolean $taxable = false, string $tax_class = '' )> <!-- language: lang-css --> 

Parameters: 
    $name  Unique name for the fee. Multiple fees of the same name cannot be added. 
    $amount Fee amount. 
    $taxable (default: false) Is the fee taxable? 
    $tax_class (default: '') The tax class for the fee if taxable. A blank string is standard tax class. 

参考:

+0

これは私が必要とする、幻想的で正確なものです@LoicTheAztec。これはもっと基本的なPHPの問題かもしれませんが、 'coupon_code'変数にクーポンコードの配列を格納することはできますか?多くのコードのいずれかが利用されている場合は追加する必要がありますか? – Cmagb

+0

単一クーポン値で完全にテストされています。アレイのおかげで、ありがとう。あなたの助けが大いにありがとう!間違いなくここでたくさんのことを学ぶ。 – Cmagb

関連する問題