简体   繁体   中英

Force woocommerce product variable dropdowns

I am using woocommerce and have 5 variable products on one page. They all use a attributes of height, width and color. What I would like to do is, if product one selects color of red, I want it also to be selected in the other 4 products. Same for height and width.

I thought this would be as simple as just forcing the dropdown selection with jQuery. So I started looking and I found a few things. They all seemed to be cloning the html into the field of product-2. So if we selected red in product-1, product-2 would have 2 red as options in the dropdown with the clone selected.

I ultimately tweaked something I had found to .change instead of .clone. I used the name because the select id's are the same on all products.

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);
});

At first glance it seemed to be doing the trick. It visually changed the dropdown in the second product. However if I select a different variation option manually(such as color) it reverts to the first option of the dropdown. And secondly this is not updating the product price. Its as if woocommerce doesnt know the variation is selected.

I am not married to using the above code if anyone knows of a better way. I tried looking for some kind of product filter but I came up empty in my search. I am a noob with woocommerce.

I am really stuck. any help is appreciated, as always.

Cheers

add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );

function display_price_in_variation_option_name( $term ) { global $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;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM