【问题标题】:Restrict delivery dates for some specific product categories in Woocommerce限制 Woocommerce 中某些特定产品类别的交货日期
【发布时间】:2018-07-03 03:48:05
【问题描述】:

我已将 DatePicker 添加到我的结帐页面,以便客户选择交货日期。还添加了一个脚本来禁用过去的日期(显然)和某些假期的交付。

这是脚本:

<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>

在我尝试对其施加一些限制之前,它可以完美运行。我只需要禁用某些类别的假日递送,而不是全部。我是这样做的:

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' );

也试过只是粘贴脚本:

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' );

我做错了什么?

【问题讨论】:

    标签: php woocommerce cart categories checkout


    【解决方案1】:

    我对您的代码进行了一些更改,您需要在其中定义特定的目标产品类别:

    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    
    }
    

    代码在您的活动子主题(或活动主题)的functions.php 文件中。

    它现在应该可以工作了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-22
      • 1970-01-01
      • 2016-08-23
      相关资源
      最近更新 更多