要在未 Select 变体的情况下显示可变产品上的自定义文本,请使用以下命令:
add_action( 'woocommerce_before_variations_form', 'display_variation_availability_message_js' );
function display_variation_availability_message_js() {
global $product;
$custom_text_html = sprintf('<div class="wc-availability" style="display:none">%s</div>',
__('Choose location and date to see availability', 'woocommerce')
);
wc_enqueue_js("const a = '.wc-availability';
$('.single_variation_wrap').prepend('{$custom_text_html}');
$('form.cart').on('show_variation hide_variation', function(e) {
e.type === 'show_variation' ? $(a).hide() : $(a).show();
});");
}
代码位于您的子主题的unctions.php文件中(或在插件中).经过测试,效果良好.
要仅以特定variable products为目标,请在函数中替换:
global $product;
有:
global $product;
// Here below define the targeted variable product IDs in the array
$specific_products_ids = array( 15, 37, 108 );
if ( ! in_array( $product->get_id(), $specific_products_ids ) ) return;
编辑:
作为现有的parent <div class="woocommerce-variation single_variation">
for <div class="woocommerce-variation-availability">
子元素,在没有 Select 任何变体you can not use it to display your custom div inside <div class="woocommerce-variation-availability">
时隐藏(WooCommerce向下滑动使用).
如果不编辑WooCommerce的JS文件,就不可能更改WooCommerce在<div class="woocommerce-variation single_variation">
上所做的行为向上/向下滑动.
以下是使用<div>
容器和slideUp()
/slideDown()
方法而不是show()
/hide()
方法的重新访问的代码版本:
add_action( 'woocommerce_before_variations_form', 'display_variation_availability_message_js' );
function display_variation_availability_message_js() {
global $product;
$custom_text_html = sprintf('<div class="vcontainer" style="display:none">'.
'<div class="wc-availability">%s</div></div>',
__('Choose location and date to see availability', 'woocommerce')
);
wc_enqueue_js("$('.single_variation_wrap').prepend('{$custom_text_html}');
$('form.cart').on('show_variation hide_variation', function(e) {
e.type === 'show_variation' ? $('.vcontainer').slideUp(200) : $('.vcontainer').slideDown(200);
});");
}
代码位于您的子主题的unctions.php文件中(或在插件中).经过测试,效果良好.
这并不是你在 comments 中想要的那样唯一的问题,但它使用了与WooCommerce相同的行为.