WooCommerce:即使在选择变体之前也总是显示添加到购物车按钮 [英] WooCommerce: always showing add-to-cart button even before variation is selected

查看:10
本文介绍了WooCommerce:即使在选择变体之前也总是显示添加到购物车按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在可变产品的单个产品页面上,WooCommerce 在选择一个变体之前不会生成添加到购物车"按钮.如果需要两个变体但只选择了一个,WC 仍会生成按钮,但单击它会触发一条错误消息,要求选择所有变体.

On the single product page of variable products, WooCommerce doesn't generate the Add-to-cart button until one variation gets selected. If two variations are required but only one is selected, WC still generates the button but clicking it triggers an error message asking to select all variations.

除了我不理解这个逻辑这一事实(为什么对第二个变体使用提交后错误消息,而对于第一个变体使用不同的解决方案 - 无提交按钮的解决方案?),是否有一个始终显示添加到购物车按钮的方法,如果未选择所有变体,则会显示提交后错误消息?

Apart from the fact that I don't understand this logic (why using a post-submit error message for the second variation and a different solution - the no-submit-button one - for the first?), is there a way to show the add-to-cart button at all times, with a post-submit error message if not all variations were selected ?

推荐答案

这应该可以:

add_action( 'woocommerce_before_add_to_cart_button', function(){
    // start output buffering
    ob_start();
} );

add_action( 'woocommerce_before_single_variation', function(){
    // end output buffering
    ob_end_clean();
    // output custom div
    echo '<div class="single_variation_wrap_custom">';
} );
';});

Practically I've intercepted (inside the template file single-product/add-to-cart/variable.php) the HTML tag <div class="single_variation_wrap" style="display:none;"> that is affected via javascript by the choices of the user, and replaced with <div class="single_variation_wrap_custom"> that is not.

这篇关于WooCommerce:即使在选择变体之前也总是显示添加到购物车按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆