dpw63348 2018-01-24 07:10
浏览 38

限制Woocommerce中某些特定产品类别的交付日期

I've added DatePicker to my checkout page for customers to pick the delivery date. Also added a script to disable past dates (obviously) and delivery on some holidays.

Here is the script:

<script>
var disableddates = ["14-02-2018", "25-12-2018"];

function DisableSpecificDates(date) {
    var string = jQuery.datepicker.formatDate("dd-mm-yy", date);
    return [disableddates.indexOf(string) == -1];
}

jQuery(function() {
    jQuery("#billing_delivery_date").datepicker({
        dateFormat: "DD, d MM, yy",
        beforeShowDay: DisableSpecificDates,
        minDate: new Date()
    });
});
</script>

And it works perfectly until I try to put some restrictions on it. I need just disable holiday delivery for some categories, not for all. I did it like that:

function add_checkout_script() {
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {    
        // if a product is not in our cat, bail out since we know the cat is not alone
        if ( has_term( my_category, 'product_cat', $cart_item['data']->id ) ) {
            wp_enqueue_script( 'newscript', get_stylesheet_directory_uri() . '/restrict_day_script.js', array( 'jquery' ));
        }
    }   
} 
add_action( 'woocommerce_after_checkout_form', 'add_checkout_script' );

Also tried just paste the script:

function add_checkout_script() {
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {    
        // if a product is not in our cat, bail out since we know the cat is not alone
        if ( has_term( my_category, 'product_cat', $cart_item['data']->id ) ) {
            ?>
            <script>
            var disableddates = ["14-02-2018", "25-12-2018"];

            function DisableSpecificDates(date) {
                var string = jQuery.datepicker.formatDate("dd-mm-yy", date);
                return [disableddates.indexOf(string) == -1];
            }

            jQuery(function() {
                jQuery("#billing_delivery_date").datepicker({
                    dateFormat: "DD, d MM, yy",
                    beforeShowDay: DisableSpecificDates,
                    minDate: new Date()
                });
            });
            </script>
            <?php
        }
    }    
}
add_action( 'woocommerce_after_checkout_form', 'add_checkout_script' );

What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • dongpan8928 2018-01-24 12:56
    关注

    I have maid some changes to your code, where you will need to define your specific targeted product categories:

    add_action( 'woocommerce_after_checkout_form', 'add_checkout_script' );
    function add_checkout_script() {
        // HERE define your product categories in the array (Ids, slugs or names)
        $categories = array( 't-shirts', 'sweet-shirts' );
    
        $product_id = $cart_item['product_id'];
        $found = false;
    
        // Loop through cart items to find out specific product categories
        foreach( WC()->cart->get_cart() as $cart_item )
            if( has_term( $categories, 'product_cat', $product_id ) ) {
                $found = true;
                break;
            }
    
        // If product categories are not found in cart items, we exit
        if ( ! $found ) return
    
        ?>
        <script>
            jQuery(function($) {
                var disableddates = ["14-02-2018", "25-12-2018"];
    
                function DisableSpecificDates(date) {
                    var string = $.datepicker.formatDate("dd-mm-yy", date);
                    return [disableddates.indexOf(string) == -1];
                }
    
                $("#billing_delivery_date").datepicker({
                    dateFormat: "DD, d MM, yy",
                    beforeShowDay: DisableSpecificDates,
                    minDate: new Date()
                });
            });
        </script>
        <?php    
    }
    

    Code goes in function.php file of your active child theme (or active theme).

    It should work now.

    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答