我有一款有不同变化的产品.要 Select 变体,必须填写两个 Select 字段.但是,只有在两个 Select 字段都 Select 了选项时,才会显示可用性消息.只要没有 Select 变体,我就会显示一条一般的可用性消息:" Select 位置和日期以查看可用性".

我知道如何使用"WooCommerce_Get_Availability_Text"-筛选器更改消息.但我不知道如何从页面加载直接显示这条消息,也不知道我必须更改WooCommerce的哪个部分才能使其正常工作.

我认为以下几点是必要的:

  1. <div class="woocommerce-variation-availability">Choose location and date to see availability</div>添加到Content-Single-Product.php中,以便在页面加载时显示常规消息.

  2. 添加一些JS,一旦 Select 了变体,就会将in-stockout-of-stock消息放到div中.JS还必须注意,一旦不再 Select 任何变体,常规消息就会再次显示.

推荐答案

要在未 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相同的行为.

Php相关问答推荐

带有打印和回声的PHP意外命令

在PHP中替换数组中的文本并同时使用其他函数

模型中的Laravel自定义查询字段

在ShipStation for WooCommerce中使用自定义订单项目元数据

如何删除WordPress用户的个人资料描述或其他个人资料字段中的链接?

如何在函数中定位WooCommerce产品循环

如何显示每个分类的当前术语的帖子中包含的前20个标签,不包括标签分类

WooCommerce数量减号和加号图标未显示

如何在php网站代码中清理浏览器缓存

是否使用联系人表单7 wpcf7_Feedback_Response输出答复中的html?

哪里的表现能更好?

在 WooCommerce 管理变量产品中启用其他变体图像

Symfony:指定 data_class 时,提交的表单获取初始化前不得访问

如何在自定义消息中包含 WooCommerce 选定的产品变体详细信息

使用 RSA 2048 公钥验证 RSA PKCS#1v1.5 SHA 256 签名

为什么 AJAX 请求没有通过 is_ajax_request() codeigniter 的条件?

如何从 PHP 中的字符串位置排除图像扩展名

构建 [Controller] 时目标 [Interface] 不可实例化

使用 --exclude-dir 选项从 PHP 执行 grep 时出现问题

复杂的mysql查询问题