隐藏送货消息,直到将产品添加到WooCommerce中的购物车中

时间:2018-11-03 15:28:00

标签: woocommerce

我有这段代码,告诉客户在免费送货之前还剩下多少钱。但是,无论您是否将产品添加到购物车,它都会在您访问产品页面时立即显示。

我希望仅在将产品添加到购物车后才显示(请参阅各个挂钩)。有什么想法吗?

代码如下:

    add_action( 'woocommerce_before_single_product', 'free_shipping_cart_notice_zones' );
    add_action( 'woocommerce_checkout_before_order_review', 'free_shipping_cart_notice_zones' );
    add_action( 'woocommerce_cart_totals_after_order_total', 'free_shipping_cart_notice_zones' );

    function free_shipping_cart_notice_zones() {
        global $woocommerce;
if ( WC()->cart->get_cart_contents_count() != 0 ) {

        $default_zone = new WC_Shipping_Zone(0);
        $default_methods = $default_zone->get_shipping_methods();
        foreach( $default_methods as $key => $value ) {
            if ( $value->id === "free_shipping" ) {
              if ( $value->min_amount > 0 ) $min_amounts[] = $value->min_amount;
            } 
        }

        $delivery_zones = WC_Shipping_Zones::get_zones();
        foreach ( $delivery_zones as $key => $delivery_zone ) {
            foreach ( $delivery_zone['shipping_methods'] as $key => $value ) {
                if ( $value->id === "free_shipping" ) {
                    if ( $value->min_amount > 0 ) $min_amounts[] = $value->min_amount;
                } 
            } 
        }

        if ( is_array($min_amounts) ) {
            $min_amount = min($min_amounts);
            $current = WC()->cart->subtotal;

            if ( $current < $min_amount ) {
                $shipping_text = esc_html__('You are ', 'woocommerce' ) . wc_price( $min_amount - $current ) . esc_html__(' away from FREE SHIPPING!', 'woocommerce' );
                echo '<p class="delivery-message">' . $shipping_text . '</p>';
            } 
        }  
    }
}

任何建议都将受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

只需在挂钩中显示消息,即可检查购物车是否空了

SELECT count(1)
from [dbo].[EmployeeMaster]
where [UserName]=EncryptByKey (Key_GUID('Symmetrickey'),'Username')