2017-02-20 6 views
4

はシンプルかつ変数製品の製品価格の後にカスタムラベルを追加するには、以下のコードを使用しましょう:可変製品セレクタ:WooCommerceに住んで選択した値

add_filter('woocommerce_variation_price_html','prices_custom_labels', 10, 2 ); 
add_filter('woocommerce_price_html','prices_custom_labels', 10, 2); 
function prices_custom_labels($price, $product){ 

    // Set HERE your custom labels names 
    $per_dozen = ' '. __('per dozen', 'woocommerce'); 
    $per_case = ' '. __('per case (20 dozens)', 'woocommerce'); 


    // 1) Variable products 
    if ($product->product_type != 'simple' && $product->variation_id) { 

     // Getting the array of existing attributes values for a variation 
     $variation_attribute_value = $product->variation_data; 
     // Here we keep only the last value in this array 
     $last_variation_attribute_slug_value = ' ' . end($variation_attribute_value); 

     // Finding the word 'case' in the attribute value slug 
     $has_case = strstr($last_variation_attribute_slug_value, 'case'); 

     // Setting the right displayed label depending on attribute value slug 
     if($has_case) 
      $attribute_quantity_name_value = $per_case; 
     else 
      $attribute_quantity_name_value = $per_dozen; 

     // Here the output price + custom label 
     $price = '<ins class="highlight">'.woocommerce_price($product- >regular_price).$attribute_quantity_name_value.'</ins>'; 
    } 
    // 2) Simple products 
    else 
    { 
     // Here the output price + custom default label 
     $price = '<ins class="highlight">'.woocommerce_price($product- >regular_price).$per_dozen.'</ins>'; 
    } 
    return $price; 
} 

しかし、変数の製品で、私は問題を持っていますライブ表示価格で追加されたカスタムラベル。私が使用するコードは、 "ダースンごとの"ライブ価格の後にのみ表示されます。

私は価格の後に右のラベルを追加するためのカスタム「数量」セレクタで選択した値を取得する必要があります。

  • 選択した値が「ダース」であるならば、私は生き価格の後に表示する必要があります」
  • 選択された値が「ケース(20ダース)」の場合、「ケースごと(20ダース)」のライブ価格の後に表示する必要があります。

このスクリーンショットは、私はすべてのケースのために実際に持っているものです。

enter image description here

チェックこの問題を私のウェブサイト上でspecific product page

は、だから私は属性を取得する必要があります「数量ライブ値段に右のラベルを追加するために選択された値。

助けが必要ですか?それを働かせるにはどうすればいいですか?

私は多くのコードを試しましたが、私はそれを働かせることはできません。

+0

リンクを開くことができません! – Smit

+0

問題なしで開かれただけです...どのようなエラーが表示されますか?再試行する。 – Dora

答えて

6

この作業を取得する唯一の方法では、JavaScript/jQueryのを使用することですが、それは複雑ですWooCommerceはすでにJavascript/Ajaxコードを実行しています。

まず、はWooCommerceは<option> htmlタグから"selected"属性を削除するようセレクタで選択した顧客の選択を検出することはできません。顧客は完全な選択をしたら

は、Woocommerceは隠さ<imput> HTMLフィールドに対応する変動ID値を追加し、対応する価格を表示(そう、この変数製品から一つのバリエーションを選びました)。「数量」属性、それらの各々のためのの対応する値と

ジャバスクリプトに我々PHPコードパス変動IDをこの変数製品ののアレイ、

その後、我々は右の「ラベル」で価格を追加、その隠されたvariation ID値を取得し、ためにJavaScriptのイベント<select> htmlタグに「ぼかしの」を使用することができます。ここで

顧客の選択(そのように選択された製品バリエーション)に応じて、ライブの価格にカスタムラベルを追加します、機能コード、は次のとおりです。

add_action('woocommerce_after_add_to_cart_form', 'custom_get_variations_js'); 
function custom_get_variations_js() { 
    global $product; 

    // Set HERE your "quantity" attribute slug 
    $attribute_qty_slug = 'pa_quantity'; 
    $attribute_qty_slug_key = 'attribute_'.$attribute_qty_slug; 

    foreach($product->get_available_variations() as $values){ 
     $attribute_qty_slug_value = $values['attributes'][$attribute_qty_slug_key]; 
     $attribute_qty_name_value = get_term_by('slug', $attribute_qty_slug_value, $attribute_qty_slug); 
     $variations_id_arr[$values['variation_id']] = __(' per ', 'woocommerce') . strtolower($attribute_qty_name_value->name); 
    } 

    ## THE JQUERY SCRIPT ## 
    ?> 
    <script> 
     (function($){ 
      var $attributes; 
      <?php 
       // Passing the product variations attributes array to javascript 
       $js_array = json_encode($variations_id_arr); 
       echo 'var $variationsIdsAttr = '. $js_array ; 
      ?> 
      $('td.value select').blur(function() { 
       var $variationId = $('input[name="variation_id"]').val(); 
       if (typeof $variationId !== 'undefined'){ 
        for(key in $variationsIdsAttr){ 
         if(key == $variationId){ 
          $attributes = $variationsIdsAttr[key]; 
          break; 
         } 
        } 
       } 
       if (typeof $attributes !== 'undefined'){ 
        $('.woocommerce-variation-price .woocommerce-Price-amount.amount').append($attributes); 
       } 
      }); 
     })(jQuery); 
    </script> 
    <?php 
} 

その後、我々はあなたを変更する必要がありますコードは(最初のフック関数内で)この仕様変数の製品上の第2のカスタムラベルを表示するようなときには、既存:

add_filter('woocommerce_variation_price_html','prices_custom_labels', 10, 2); 
add_filter('woocommerce_price_html','prices_custom_labels', 10, 2); 
function prices_custom_labels($price, $product){ 

    // Custom label name 
    $per_dozen = ' '. __('per dozen', 'woocommerce'); 

    // Set HERE your "quantity" attribute slug 
    $attribute_qty_slug = 'pa_quantity'; 

    $attribute_qty_slug_key = 'attribute_'.$attribute_qty_slug; 
    $append_label = ''; 

    // 1) Variable products 
    if ($product->product_type != 'simple' && $product->variation_id) { 

     // Getting the attribute "quantity" value 
     $attribute_qty_is_set = $product->variation_data[$attribute_qty_slug_key]; 
     echo '<pre>'; print_r($product->variation_data[$attribute_qty_slug_key]); echo '</pre>'; 

     // if "quantity" not set we display " per dozen" 
     if(! $attribute_qty_is_set) 
      $append_label = $per_dozen; 


     // Outputed price + custom label 
     $price = '<ins class="highlight">'.woocommerce_price($product->regular_price).$append_label.'</ins>'; 
    } 
    // 2) Simple products 
    else 
    { 
     // Here the output price + custom default label 
     $price = '<ins class="highlight">'.woocommerce_price($product->regular_price).$per_dozen.'</ins>'; 
    } 
    return $price; 
} 

add_filter('woocommerce_variable_price_html', 'prices_custom_labels_min_max', 20, 2); 
function prices_custom_labels_min_max($price, $product) { 

    // Custom label name 
    $per_dozen = ' '. __('per dozen', 'woocommerce'); 
    $per_case = ' '. __('per case', 'woocommerce'); 

    // Set HERE your quantity attribute slug 
    $attribute_qty_slug = 'pa_quantity'; 


    // Getting the min and max variations prices 
    $variation_min_reg_price = $product->get_variation_regular_price('min', true); 
    $variation_max_reg_price = $product->get_variation_regular_price('max', true); 
    $variation_reg_price = $product->get_variation_regular_price(); 


    if($variation_min_reg_price == $variation_max_reg_price) 
    { 
     $price = '<ins class="highlight">'.woocommerce_price($variation_reg_price) . $per_dozen . '</ins>'; 
    } 
    else 
    { 
     if(!in_array($attribute_qty_slug, array_keys($product->get_attributes()))) 
     { 
      $price = '<ins class="highlight">' . woocommerce_price($variation_min_reg_price) . $per_dozen . ' - ' . woocommerce_price($variation_max_reg_price) . $per_dozen . '</ins>'; 
     } 
     else 
     { 
      $price = '<ins class="highlight">' . woocommerce_price($variation_min_reg_price) . $per_dozen . ' - ' . woocommerce_price($variation_max_reg_price) . $per_case . '</ins>'; 
     } 
    } 
    // print_r($product->get_attributes()); 
    return $price; 
} 

コードはfunctiに行きますアクティブな子テーマ(またはテーマ)のon.phpファイル、またはプラグインファイル内の。


関連の答え:

1

これは 'ケース'であるべきではありません。strstrは大文字と小文字を区別する関数であり、ブール値であるため、以下のステートメントは常にfalseを返します。 urセレクタの値はCase (20 dozens)ではないので、case (20 dozens)を参照してください。

このように、以下の行を変更します。

$has_case = strstr($last_variation_attribute_slug_value, 'case'); 

へ:

$has_case = strstr($last_variation_attribute_slug_value, 'Case'); 

REF:https://www.w3schools.com/php/func_string_strstr.asp

+0

それは魔法を作ります。とても感謝しています ! – Dora

+0

私はあなたの指示に従いましたが、一部の製品では "ケースごとに"表示されますが、その製品では "ケース"という名前のバリエーションはありませんか?どのように私は製品のバリエーションを持っていないときに価格で "ケースごとに"私を表示することは可能ですか? 例:http://www.wholesaleunderwear.co/product/rose-ladys-lace-boyshort-4/ しかし、その製品には「ケース」がありません。 http://i.imgur.com/WiuNnyZ.png?1 – Dora

+0

現在、1ダースごとに表示されています。右?ケースごとに表示したいですか? – Smit

関連する問題