【问题标题】:How to use multiple filtering options that will affect the price in Woocommerce simple products?如何使用会影响 Woocommerce 简单产品价格的多个过滤选项?
【发布时间】:2019-05-14 07:57:34
【问题描述】:

我正在使用 "Add a select field that will change price in Woocommerce simple products" 答案代码。它仅使用单个选择字段进行过滤。我想使用 jQuery 扩展此代码,以便能够使用多个字段进行过滤。

示例:2 个选择字段、一个文本、一个复选框和一些单选按钮,它们都将确定最终计算的价格。

// Select field
woocommerce_form_field('materials_pack', array(
    'type'          => 'select',
    'class'         => array('material-field form-row-wide'),
    'label'         => __('Select Materials:', $domain),
    'required'      => true,
    'options'       => $options,
),'');   

<script>
jQuery(function($){
    var a = <?php echo json_encode($prices); ?>,
        b = 'p.price',
        c = 'select[name="materials_pack"]';

    $(c).on( 'change', function(){
        $.each( a, function( key, value ){
            if( $(c).val() == key )
                $(b).html(value);
        });
    });
});
</script>

如何将它用于多领域? jquery脚本是怎么做的?

【问题讨论】:

  • 感谢 Mena 的编辑。我们可以从这个帖子中邀请 LoicTheAztec 吗? stackoverflow.com/questions/49862061/…
  • 我刚刚对他的回答发表了评论。
  • 我的答案代码只适用于一个定义字段。将其扩展到不同类型的多个领域是一个真正的发展。最好使用允许这样做的existing related plugins
  • 拜托如果你能帮助我我不想使用插件我讨厌插件我创建一个没有插件的完整主题。

标签: javascript php jquery wordpress woocommerce


【解决方案1】:

好的,我找到了我的答案here 现在我只需要在这段代码中怎么做?

add_action('woocommerce_before_calculate_totals', 'set_cutom_cart_item_price', 20, 1);

foreach (  $cart->get_cart() as $cart_item ) {
    if ( isset( $cart_item['material_data']['new_price'] ) ){
        $cart_item['data']->set_price( $cart_item['material_data']['new_price'] );
    }
}

【讨论】:

  • 这里有来自 2 个选择文件的多个价格如何将其添加到购物车?两个价格?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-13
  • 1970-01-01
  • 2017-05-04
  • 1970-01-01
相关资源
最近更新 更多