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

时间:2018-01-24 07:10:14

标签: php woocommerce cart categories checkout

我已将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' );

我做错了什么?

1 个答案:

答案 0 :(得分:0)

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

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    
}

代码进入活动子主题(或活动主题)的function.php文件。

现在应该可以了。