Woocommerce 中特定运输类别、最低金额和特定国家/地区的免费送货购物车消息

时间:2021-03-30 20:10:20

标签: php woocommerce cart

我想在购物车中添加一条关于免费送货的消息,取决于特定送货类别和特定国家/地区的最低金额(我的地理位置处于活动状态)。 我已经修改了 Cart Message for a specific shipping class and a minimal cart total in Woocommerce 答案中的代码。

我的解决方案似乎有效,我想确保我在所有设备上正确编写。

add_action( 'woocommerce_before_calculate_totals', 'cart_items_shipping_class_message1aa', 20, 1 );
function cart_items_shipping_class_message1aa( $cart ){
    if ( ! ( is_cart() && WC_Geolocation::geolocate_ip()['country'] === 'IT' ) || ( is_admin() && ! defined('DOING_AJAX') ) )
        return;    
    $shipping_class = 'supplier';
    $min_amout      = 20;
    $free_shipping  = 50;
    $amout_incl_tax = 0;
    foreach( $cart->get_cart() as $cart_item ){        
        if( $cart_item['data']->get_shipping_class() === $shipping_class ){            
            $amout_incl_tax += $cart_item['line_subtotal'] + $cart_item['line_subtotal_tax'];
        }       
    } 
    if( $amout_incl_tax >= $min_amout && $amout_incl_tax < $free_shipping && is_cart() ){           
        wc_add_notice( sprintf( '<p><strong><font color="green">FREE SHIPPING on products SUPPLIER width at least 50 €</font></strong></p><a href="/brand/supplier-slug" class="button free">Add %s of products from Supplier</a>',
            wc_price($free_shipping - $amout_incl_tax) ), 'notice' );
    }
}

0 个答案:

没有答案