Woocommerce中基于时间和日期的有条件交货通知 [英] Conditional Delivery Notice based on Time and Date in Woocommerce

查看:145
本文介绍了Woocommerce中基于时间和日期的有条件交货通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一条消息汇总在一起,该消息将根据周一至周四以及周五至周日的时间显示运输通知。

I am trying to get a message together that will display a shipping notice depending on what time it is monday to thursday and friday to sunday.

如果客户订购

如果客户在星期一的12点以后订购,则该订单在同一天(星期一至星期四)发货,并在第二天交付。

If the customer orders after 12 o'clock monday to thursday the order will be prepared and shipped the day after, monday to thursday.

所有周五至周日下达的订单都将在下一个工作日(周一)进行准备和运输。

All orders made friday to sunday will be prepared and shipped on the following weekday (monday).

我正在使用的代码无法做到这一点,我试图了解如何使其工作。

The code I am using does not do this and I am trying to understand how to make it work. Any help is very much appreciated.

    add_action( 'woocommerce_before_customer_login_form', 'next_day_delivery' );
    add_action( 'woocommerce_before_customer_login_form', 'next_day_delivery' );
    add_action( 'woocommerce_before_checkout_form', 'next_day_delivery' );
    add_action( 'woocommerce_before_shop_loop', 'next_day_delivery' );
    add_action( 'woocommerce_before_single_product_summary', 'next_day_delivery' );
    add_action( 'woocommerce_before_cart', 'next_day_delivery' );

    function next_day_delivery() {

        date_default_timezone_set('Europe/Stockholm');
        $end_time         = mktime('12', '00', '00', '2018');
        $now_time         = strtotime("now");

        if ( WC()->cart->get_cart_contents_count() > 0 ) && $now_time < $end_time {

        // print the information notice
        wc_print_notice( __( 'Order within $end_time - $now_time and get your order delivered tomorrow!', 'woocommerce' ), 'success' );
    }

else if wc_print_notice( __( 'Your order will be prepared and shipped on Monday.', 'woocommerce' ), 'success' );
    }


推荐答案

存在一些错误和遗漏代码中的内容。以下将有条件地显示您的问题中定义的自定义发货通知:

There is some mistakes and missing things in your code. The following will display conditionally a custom shipping notice as defined in your question:

add_action( 'woocommerce_before_customer_login_form', 'next_day_delivery' );
add_action( 'woocommerce_before_customer_login_form', 'next_day_delivery' );
add_action( 'woocommerce_before_checkout_form', 'next_day_delivery' );
add_action( 'woocommerce_before_shop_loop', 'next_day_delivery' );
add_action( 'woocommerce_before_single_product_summary', 'next_day_delivery' );
add_action( 'woocommerce_before_cart', 'next_day_delivery' );

function next_day_delivery() {
    if( WC()->cart->is_empty() )
        return; // Exit

    // Set the time zone
    date_default_timezone_set('Europe/Stockholm');

    // From Monday to Thursday
    $is_week_days  = in_array( date('w'), array( 1, 2, 3, 4 ) ) ? true : false;
    $end_time      = mktime('12', '00', '00', date('m'), date('d'), date('Y'));
    $now_time      = time();
    $after_tomorow = date('l', strtotime('+2 days'));

    $dateDiff      = intval(($end_time - $now_time)/60);
    $diff_hours    = intval($dateDiff/60);
    $diff_minutes  = $dateDiff%60;
    $hours_label   = _n( 'hour', 'hours', $diff_hours, 'wooocommerce' );
    $minutes_label = _n( 'minute', 'minutes', $diff_minutes, 'wooocommerce' );


    if ( $end_time > $now_time && $is_week_days ) {
        // print the information notice
        $message = sprintf( __( '%s left to be delivered tomorrow!', 'woocommerce' ),
        $diff_hours.' '.$hours_label.' and '.$diff_minutes.' '.$minutes_label);
    }
    elseif ( $end_time <= $now_time && $is_week_days ) {
        $message = sprintf( __( 'Your order will be delivered this %s.', 'woocommerce' ), $after_tomorow );
    } else {
        $message = __( 'Your order will be prepared and shipped next upcoming monday and delivered on tuesday.', 'woocommerce' );
    }
    wc_print_notice( $message, 'success' );
}

代码进入活动子主题(或活动主题)的function.php文件)。经过测试并有效。

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

这篇关于Woocommerce中基于时间和日期的有条件交货通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆