Woocommerce中基于时间和日期的有条件交付通知

时间:2018-10-30 10:29:50

标签: php wordpress date time woocommerce

我正在尝试将一条消息汇总在一起,该消息将显示运输通知,具体取决于星期一至星期四和星期五至星期日的时间。

如果客户在白天(上午12点)之前下订单,则该订单将在同一天(周一至周四)发货并在第二天发货。

如果客户在周一至周四的12点后订购,则将在周一至周四的次日准备并发货。

所有周五至周日下达的订单都将在下一个工作日(星期一)准备好并发货。

我正在使用的代码无法做到这一点,我正在尝试了解如何使其工作。非常感谢您的帮助。

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

1 个答案:

答案 0 :(得分:0)

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

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文件中。经过测试,可以正常工作。

enter image description here