我已经编写了一些代码来显示产品页面上的 Select 输入,我现在的问题是,如果它是一个没有变化的简单产品,那么客户可以添加到购物车中,而无需从输入字段中进行 Select .尽管如此,我还是希望输入字段是必需的.

以下是我到目前为止所做的:

add_action('woocommerce_before_add_to_cart_button','wdm_add_custom_fields');
/**
 * Adds a custom field for Product
 * @return [type] [description]
 */
function wdm_add_custom_fields()
{

    global $product;

    ob_start();

    ?>
        <div class="wdm-custom-fields>
        <label for="fabric">Fabric:</label>

        <select id="id_select2_example" name="Fabric" style="width: 150px;">
                <option data-img_src="" value="">Choose a Fabric</option>

        <option data-img_src="http://localhost/shopneyah/wp-content/uploads/2022/08/19-organic-cotton-satin-fabric-1-250x250-1.webp" value="ash">Ash</option>
        
        <option data-img_src="http://localhost/shopneyah/wp-content/uploads/2022/08/superior-quality-poly-cotton-denim-blue-white-blue-plain-poly-cotton-fabric-close-up-fabric-photo.jpg" value="Light Blue">Light Blue</option>
        <option data-img_src="http://localhost/shopneyah/wp-content/uploads/2022/08/poly-cotton-fabric.jpg" value="Dark Blue">Dark Blue</option>
        <option data-img_src="http://localhost/shopneyah/wp-content/uploads/2022/08/images-2.jpg" value="light purple">Light Purple</option>
    </select>
    </div>
    
    <!-- scripts -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.js"></script>
<script type="text/javascript">
    function custom_template(obj){
            var data = $(obj.element).data();
            var text = $(obj.element).text();
            if(data && data['img_src']){
                img_src = data['img_src'];
                template = $("<div><img src=\"" + img_src + "\" style=\"width:35%;height:200px;\"/><p style=\"font-weight: 700;font-size:14pt;text-align:left;\">" + text + "</p></div>");
                return template;
            }
        }
    var options = {
        'templateSelection': custom_template,
        'templateResult': custom_template,
    }
    $('#id_select2_example').select2(options);
    $('.select2-container--default .select2-selection--single').css({'height': '225px'});

</script>

    <?php

    $content = ob_get_contents();
    ob_end_flush();

    return $content;
}

add_filter( 'woocommerce_checkout_fields' , 'wdm_add_custom_fields' );

enter image description here

从上面的图像中,尚未 Select 布料输入,但可以单击添加到购物车,请在 Select 布料之前如何禁用它或停止它工作?谢谢.

推荐答案

您可以将以下JavaScript代码添加到主题的自定义JavaScript文件中,或添加到wdm_add_Custom_field()函数中的脚本标记中:

jQuery(document).ready(function($){
    // Disable the "Add to Cart" button
    $("button[name='add-to-cart']").prop("disabled", true);

    // Listen for changes in the fabric select field
    $("select[name='Fabric']").change(function(){
        // If a fabric has been selected, enable the "Add to Cart" button
        if($(this).val() != ''){
            $("button[name='add-to-cart']").prop("disabled", false);
        } else {
            // Otherwise, disable the "Add to Cart" button
            $("button[name='add-to-cart']").prop("disabled", true);
        }
    });
});

它将在页面加载时禁用"Add to Cart"按钮,然后监听Fabric Select 字段中的更改.如果 Select 了织物,则会启用"Add to Cart"按钮,如果没有 Select ,则会禁用该按钮.

Php相关问答推荐

一列必须指定多个变量的Laravel查询

PHP中冒号的奇怪用例

如何修复条带元素中的错误&&客户端_机密&

由于某种原因,FILE_GET_CONTENTS从一天到另一天停止工作,并在PHP 7.2.34中返回一条&http/1.1 406不能接受&的错误

添加登录:需要app.yaml仍然允许所有拥有Google帐户的人访问

我有一个显示记录的表格,每行在最右边有两个按钮.如何才能以正确的顺序获得表的值?

使用简码在 WooCommerce 我的帐户中显示一些用户数据

Laravel 在数据库上显示加密列

Symfony:从控制器内部调用自定义命令

当我激活插件时出现错误注意:未定义的偏移量:1

使用用户元数据填写 woocommerce checkout 字段

从 Laravel 机制中排除目录并直接访问该目录

Laravel路由中的符号在slug中会创建额外的斜杠

使用其ID查询和捕获与订单相关的其他详细信息的shopware 6方法

仅允许减少 WooCommerce 中已完成订单的库存数量

在 Laravel、Vuejs 和 Inertia 中配置 TypeScript 的问题

正则表达式 (php) 匹配单个 [ 或单个 ] 但忽略 [[ ]] 之间的任何内容?

在 GA4 Data API V1 Beta 中使用 inListFilter 过滤器时出错

Docker: unixodbc.h 没有这样的文件或目录.已安装 unixodbc-dev 时出现pecl install sqlsrv错误

我想更新表格中的结果并将它们发布到浏览器,但它不起作用