【问题标题】:Force woocommerce product variable dropdowns强制 woocommerce 产品变量下拉菜单
【发布时间】:2017-03-04 01:39:52
【问题描述】:

我正在使用 woocommerce,并且在一页上有 5 个可变产品。它们都使用高度、宽度和颜色的属性。我想做的是,如果产品一选择红色,我希望它也被选择在其他 4 个产品中。高度和宽度相同。

我认为这就像使用 jQuery 强制下拉选择一样简单。所以我开始寻找,我发现了一些东西。他们似乎都将 html 克隆到 product-2 领域。因此,如果我们在 product-1 中选择红色,则 product-2 将在下拉列表中选择 2 个红色作为选择克隆。

我最终将我发现的一些东西调整为 .change 而不是 .clone。我使用这个名称是因为所有产品的选择 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);
});

乍一看,它似乎在起作用。它在视觉上改变了第二个产品中的下拉菜单。但是,如果我手动选择不同的变体选项(例如颜色),它会恢复为下拉菜单的第一个选项。其次,这不是更新产品价格。好像 woocommerce 不知道选择了变体。

如果有人知道更好的方法,我不会使用上面的代码。我尝试寻找某种产品过滤器,但我的搜索结果是空的。我是 woocommerce 的菜鸟。

我真的被困住了。一如既往地感谢任何帮助。

干杯

【问题讨论】:

    标签: php jquery wordpress woocommerce


    【解决方案1】:

    add_filter('woocommerce_variation_option_name', 'display_price_in_variation_option_name');

    函数 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;
    

    }

    【讨论】:

      猜你喜欢
      • 2017-06-11
      • 2019-05-16
      • 2019-08-25
      • 1970-01-01
      • 2019-09-30
      • 1970-01-01
      • 1970-01-01
      • 2019-03-13
      • 2016-08-14
      相关资源
      最近更新 更多