2017-05-15 9 views
0

woocommerce用に作成したカスタムコードは、woocommerce 3に更新した後に正しく動作しません。この特定のケースでは、woocommerce_available_variationフィルタです。私はそれを使用して、製品属性に基づいて製品バリエーションの説明を動的に挿入しました。今、私はこのエラーメッセージを取得していますのようwoocommerce_available_variationフィルタは、Woocommerce 3アップデート後には動作しません

キャッチできる致命的なエラー:クラスWC_Product_Downloadのオブジェクトは/ホーム/ silent48/public_htmlの/ WP/WP-コンテンツ/プラグイン/ woocommerceに文字列に変換することができませんでした/includes/wc-product-functions.php on line 376

3.0に準拠させるには、以下のコードをどうすればいいですか?

add_filter('woocommerce_available_variation', 'change_variation_descriptions'); 
function change_variation_descriptions($descriptions) { 


global $post, $woocommerce; 


    $basicmp3des = '<div class="licensedetails"> 
        <li>-Delivered in mp3 format instantly after purchase</li> 
        <li>-Synchronization rights are granted</li> 
        <li>-One commercial use is permitted (ie: mixtape, album, etc)</li> 
        <li>-You may distribute up to 5000 profitable units</li> 
        <li>-Includes non-exclusive contract agreement (e-signed at checkout)</li> 
        <li>-Send me a quote to inquire about Exclusive License purchase!</li> 
        </div>';  





    foreach ($descriptions as $description) { 
     $variation = wc_get_formatted_variation($description, true); 


      if (strpos($variation, 'Basic License') !== false && strpos($variation, 'mp3') !== false) { 
        $descriptions['variation_description'] = $basicmp3des; 
      } 

    } 

      return $descriptions;  
} 
+1

私は、 'woocommerce_available_variation'は常に配列であり、バリエーションオブジェクトの配列ではないので、' foreach() 'を実行してバリエーションを期待しているとエラーが発生する可能性が高いです。 「基本ライセンス」と「mp3」は何を指していますか?それらの製品の属性ですか?私は説明を直接フィルタリングする方が簡単かもしれませんが、条件付きで目的のバリエーションにテキストを追加する方法を知る必要があります。 – helgatheviking

+0

こんにちは。それは事です、それはwoocommerce 2.xでうまくいきました。そして、ええ、音楽ファイルをファイル形式とライセンスタイプで販売しています。オリジナルのコードはかなり長くなりましたが、1つの説明と1つのバリエーションを表示するように短縮しました。私は5つのライセンス・タイプと2つのファイル・フォーマットを属性として持っています。私はこれを行うためのより適切な方法があると確信していましたが、私が働いている最初の方法だったので、私はそれを変更する気にはなりませんでした。 – isk

答えて

1

ここには2つのアプローチがあります。私は自宅にいないので、今すぐテストすることはできません。まず、渡されるデータ配列の構造に合ったものを更新することができます。どちらの場合も、バリエーションに正しい属性が割り当てられているかどうかを確認しようとしていますが、それは完全にテストできない部分なので、条件をカット/ペーストできない可能性があります。うまくいかない場合は、var_dump($attributes);をご覧になるか、またはエラーログを有効にしてerror_log(json_encode($attributes))を有効にして、属性のキーと値のペアが本当に何かを確認してください。

add_filter('woocommerce_available_variation', 'change_variation_descriptions', 10, 3); 
function change_variation_descriptions($data, $product, $variation) { 

    // Returns array of attribute name value pairs. Keys are prefixed with attribute_, as stored. 
    $attributes = $variation->get_attributes(); 

    if(isset($attributes['pa_license-options'] && 'basic-license' == $attributes['pa_license-options'] && isset($attributes['pa_delivery-format']) && 'mp3' == $attributes['pa_delivery-format'])) { 
     $data['variation_description'] = '<div class="licensedetails"> 
        <li>-Delivered in mp3 format instantly after purchase</li> 
        <li>-Synchronization rights are granted</li> 
        <li>-One commercial use is permitted (ie: mixtape, album, etc)</li> 
        <li>-You may distribute up to 5000 profitable units</li> 
        <li>-Includes non-exclusive contract agreement (e-signed at checkout)</li> 
        <li>-Send me a quote to inquire about Exclusive License purchase!</li> 
        </div>';  

    } 

    return $data;  
} 

そして次は、私たちはただ変動の説明のすべての時間をフィルタリングすることができると思う:

add_filter('woocommerce_product_get_description', 'kia_filter_description', 10, 2); 
function kia_filter_description($desc, $product) { 
    if($product->is_type('variation')) { 
     // Returns array of attribute name value pairs. Keys are prefixed with attribute_, as stored. 
     $attributes = $product->get_attributes(); 

     if(isset($attributes['pa_license-options'] && 'basic-license' == $attributes['pa_license-options'] && isset($attributes['pa_delivery-format']) && 'mp3' == $attributes['pa_delivery-format'])) { 
      $desc .= '<div class="licensedetails"> 
        <li>-Delivered in mp3 format instantly after purchase</li> 
        <li>-Synchronization rights are granted</li> 
        <li>-One commercial use is permitted (ie: mixtape, album, etc)</li> 
        <li>-You may distribute up to 5000 profitable units</li> 
        <li>-Includes non-exclusive contract agreement (e-signed at checkout)</li> 
        <li>-Send me a quote to inquire about Exclusive License purchase!</li> 
        </div>';  
     } 

    } 
    return $desc; 
} 

EDIT OPのセットアップに一致する属性と用語ナメクジを使用するように更新コード。

+0

ありがとう、私は最初のオプションをつけていた。バリエーションに属性スラッグ名を修正して属性が設定されている場合は、その説明を正常に表示できるようになりました。 ** $ attributes ['pa_license-options'] **、** $ attributes ['attribute_format'] **〜** $ attributes ['pa_delivery-format' ] **。残っているのは、特定の属性をチェックする方法を見つけることだけです。 ** 'Basic License' == $ attributes ['pa_delivery-format'] **メソッドはうまく動作していないようです。 – isk

+0

あなたの属性はタクソノミーですか? – helgatheviking

+0

ビンゴ!私は基本的なライセンス属性のタイトルをスラッグ形式に変更しました(スラグの名前を「基本ライセンス」の代わりに「基本ライセンス」とすることがあります)。私はあなたの答えを適切なナメクジで更新し、それを選択された答えとしてマークしました。 – isk

関連する問題