【问题标题】:Display how much is left before free shipping and Delivery Information in WooCommerce在 WooCommerce 中显示免费送货和送货信息前还剩多少
【发布时间】:2018-10-31 10:16:59
【问题描述】:

根据这个答案:Conditional Delivery Notice based on Time and Date in Woocommerce,我收到了一条基于特定运输条件的运输信息。

现在我意识到我需要在 WooCommerce 中设置免费送货方式,并且这样做,我需要通知客户在他们获得免费送货之前还剩多少。

所以,我找到了这段代码:

$total_cart = WC()->cart->total;
$limit_for_free_shipping = 99;

    if ($total_cart != 0 && $total_cart < $limit_free_shipping ) { 
        $difference = wc_price( round($limit_for_free_shipping - $total_cart, 2) );
        ?>

    <p class="spend_message" >
        <img src="<?php echo get_stylesheet_directory_uri(); ?>/images/shipping-icon.png"> <?php _e('If you spend another '); ?> <?php echo $diff; ?> <?php _e(', you will get '); ?> <strong><?php _e('FREE SHIPPING!'); ?></strong>
    </p>

<?php
    }

但我无法将其与另一条消息合并并将其转换为&lt;p&gt; 标记而不是wc-notice

非常感谢任何帮助。

【问题讨论】:

    标签: php wordpress woocommerce shipping notice


    【解决方案1】:

    以下将两个功能合二为一:

    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' );
        $minutes_displ = $diff_minutes.' '.$minutes_label;
        $hours_display = $diff_hours.' '.$hours_label.' and ';
        $remain_time   = $diff_hours > 0 ? $hours_display . $minutes_displ : $minutes_displ;
    
        $cart_total    = WC()->cart->total;
        $limit_free    = 100; // Starting freee shipping amount
        $free_shipping = '';  // Initialising
    
        if ( $cart_total < $limit_free ) {
            $free_shipping =  sprintf(
                __('%s If you spend another %s you will get %s', 'woocommerce' ),
                '', // ' <img src="' . get_stylesheet_directory_uri() . '/images/shipping-icon.png"> ',
                strip_tags( wc_price( round( $limit_free - $cart_total, 2 ) ) ),
                '<strong>' . __( 'FREE SHIPPING!', 'woocommerce' ) . '</strong>'
            );
        } elseif ( $cart_total >= $limit_free ) {
            $free_shipping = '<strong>' . __( ' You get FREE SHIPPING!', 'woocommerce' ) . '</strong>';
        }
    
        if ( $end_time > $now_time && $is_week_days ) {
            // print the information notice
            $message = sprintf( __( '%s left to be delivered tomorrow! %s', 'woocommerce' ), $remain_time, $free_shipping );
        }
        elseif ( $end_time <= $now_time && $is_week_days ) {
            $message = sprintf( __( 'Your order will be delivered this %s. %s', 'woocommerce' ), $after_tomorow, $free_shipping );
        } else {
            $message = __( 'Your order will be prepared and shipped next upcoming monday and delivered on tuesday.', 'woocommerce' ) . $free_shipping;
        }
        wc_print_notice( $message, 'success' );
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

    更新:删除了等于零的小时数:

    要使用&lt;p class="my-custom&gt; html 标记,而不是使用 Woocommerce 通知结构,请替换:

    wc_print_notice( $message, 'success' );
    

    作者:

    echo '<p class="my-custom>' . $message . '</p>';
    

    你已经完成了……

    【讨论】:

    • 非常感谢您的帮助!由于它仍然是一个通知而不是

      标签,我想知道如何向它添加一个类以便我可以设置它的样式。

    • 酷,我会用

      看看它的外观,并尝试将它从通知(如我上面的问题中提到的)合并到

      。再次感谢!如果您愿意,我可以提出一个新问题,但我只是好奇如果只剩下几分钟,如何从通知中删除“0 小时”?你知道怎么做吗?

    • 谢谢。非常好。只是想办法把它变成一个

      :)

    • @WooDevil 如果您使用 wc_print_notice(),则不能。您需要构建自己的通知自定义功能。
    • 我知道。这就是为什么我在这个问题中询问(在你编辑它之后仍然存在)如何合并它们,但将它变成一个

      标签而不是通知。我会考虑制作一个自定义通知。

    猜你喜欢
    • 1970-01-01
    • 2018-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-06
    • 2020-06-22
    • 2020-11-26
    相关资源
    最近更新 更多