2017-03-04 7 views
0

私はwoocommerceを使用しており、1つのページに5つの変数の商品があります。それらはすべて高さ、幅、色の属性を使用します。私がしたいことは、製品1が赤の色を選択した場合、他の4つの製品でも選択されることです。高さと幅は同じです。woocommerce製品の変数を強制的にドロップダウン

これは、jQueryを使用してドロップダウン選択を強制するだけの簡単な方法だと思いました。だから私は見始めて、私はいくつかを見つけた。彼らはすべて、htmlを製品2の分野にクローン化しているようだった。したがって、product-1でredを選択した場合、product-2のドロップダウンには、選択されたクローンが2つあります。

最終的に私が見つけたものを.cloneではなく.changeに変更しました。選択IDはすべての製品で同じなので、私はその名前を使用しました。

jQuery("select[name='wccp_attribute_pa_length[1488195634]'").change(function() { 
    alert('working'); 
    if (jQuery(this).data('options') == undefined) { 
    /*Taking an array of all options-2 and kind of embedding it on the select1*/ 
    jQuery(this).data('options', jQuery('select[name="component_1486761669_bundle_attribute_pa_length_186695"] option').change()); 
    } 
    var id = jQuery(this).val(); 
    var options = jQuery(this).data('options').filter('[value=' + id + ']'); 
    jQuery('select[name="component_1486761669_bundle_attribute_pa_length_186695"]').html(options); 
}); 

一見、それはトリックをしているようでした。それは視覚的に2番目の製品のドロップダウンを変更しました。しかし、異なるバリエーションオプション(色など)を手動で選択すると、ドロップダウンの最初のオプションに戻ります。第二に、これは製品価格を更新していない。まるでwoocommerceがバリエーションが選択されているかのように。

誰かがより良い方法を知っている場合、私は上記のコードを使用すると結婚していません。私は何らかの製品フィルタを探してみましたが、私は検索で空になりました。私はwoocommerceのノブです。

私は本当に立ち往生しています。いつものように、どんな助けもありがとうございます。

乾杯

答えて

0

ADD_FILTER( 'woocommerce_variation_option_name'、 'display_price_in_variation_option_name')。

function display_price_in_variation_option_name($ term){ グローバル$ wpdb、$ product;

$result = $wpdb->get_col("SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'"); 

$term_slug = (!empty($result)) ? $result[0] : $term; 


$query = "SELECT postmeta.post_id AS product_id 
      FROM {$wpdb->prefix}postmeta AS postmeta 
       LEFT JOIN {$wpdb->prefix}posts AS products ON (products.ID = postmeta.post_id) 
      WHERE postmeta.meta_key LIKE 'attribute_%' 
       AND postmeta.meta_value = '$term_slug' 
       AND products.post_parent = $product->id"; 

$variation_id = $wpdb->get_col($query); 

$parent = wp_get_post_parent_id($variation_id[0]); 

if ($parent > 0) { 
    $_product = new WC_Product_Variation($variation_id[0]); 
    return $term . ' (' . woocommerce_price($_product->get_price()) . ')'; 
} 
return $term; 

}

関連する問題