【问题标题】:Set minimum allowed weight for a specific country in WooCommerce在 WooCommerce 中为特定国家/地区设置最小允许重量
【发布时间】:2018-08-28 21:49:19
【问题描述】:

我正在尝试为哥伦比亚国家/地区专门应用 20 公斤的强制性最低重量,如果购物车总重量低于此最低重量,则避免结账。

这是我的实际代码,可以让我确定最小重量:

add_action( 'woocommerce_check_cart_items', 'cldws_set_weight_requirements' );
function cldws_set_weight_requirements() {
    // Only run in the Cart or Checkout pages
    if( is_cart() || is_checkout() ) {
        global $woocommerce;
        // Set the minimum weight before checking out
        $minimum_weight = 30;
        // Get the Cart's content total weight
        $cart_contents_weight = WC()->cart->cart_contents_weight;
        // Compare values and add an error is Cart's total weight
        if( $cart_contents_weight < $minimum_weight  ) {
            // Display our error message
            wc_add_notice( sprintf('<strong>A Minimum Weight of %s%s is required before checking out.</strong>'
                . '<br />Current cart weight: %s%s',
                $minimum_weight,
                get_option( 'woocommerce_weight_unit' ),
                $cart_contents_weight,
                get_option( 'woocommerce_weight_unit' ),
                get_permalink( wc_get_page_id( 'shop' ) )
                ),
            'error' );
        }
    }
}

如何使它仅适用于哥伦比亚国家/地区?

【问题讨论】:

    标签: php woocommerce cart checkout country


    【解决方案1】:

    更新(针对阿根廷和哥伦比亚航运国家)

    我重新审视了您的代码,使其仅适用于最小重量为 20 公斤的哥伦比亚。您需要检查重量单位,因为它应该是“Kg”(单位为 Kilos)。

    代码:

    add_action( 'woocommerce_check_cart_items', 'checkout_required_min_weight_country_based' );
    function checkout_required_min_weight_country_based() {
        // Only on Cart or Checkout pages
        if( ! ( is_cart() || is_checkout() ) ) return;
    
        // Get the shipping country
        $country = WC()->session->get('customer')['shipping_country'];
        if( empty($country) ){
            $country = WC()->session->get('customer')['billing_country'];
        }
    
        // For Colombia and Argentina shipping countries
        if( in_array( $country, array('CO', 'AR') ) ){
    
            // HERE Set the minimum weight
            $minimum_weight = 20; // 20 kg
    
            // Get the Cart's content total weight
            $total_weight = WC()->cart->get_cart_contents_weight();
    
            // If total weight is lower than minimum, we avoid checkout and display an error notice
            if( $total_weight < $minimum_weight  ) {
                // Display an dynamic error notice
                wc_add_notice( sprintf(
                    '<strong>A Minimum Weight of %s is required before checking out.</strong>'
                    . '<br />Current cart weight: %s',
                    wc_format_weight($minimum_weight),
                    wc_format_weight($total_weight)
                ), 'error' );
            }
        }
    }
    

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

    当哥伦比亚是检测到的国家(或定义的国家)时,您将得到如下信息:


    所有国家/地区的代码相同:

    add_action( 'woocommerce_check_cart_items', 'checkout_required_min_weight' );
    function checkout_required_min_weight() {
        // Only on Cart or Checkout pages
        if( ! ( is_cart() || is_checkout() ) ) return;
    
        // HERE Set the minimum weight
        $minimum_weight = 20; // 20 kg
    
        // Get the Cart's content total weight
        $total_weight = WC()->cart->get_cart_contents_weight();
    
        // If total weight is lower than minimum, we avoid checkout and display an error notice
        if( $total_weight < $minimum_weight  ) {
            // Display an dynamic error notice
            wc_add_notice( sprintf(
                '<strong>A Minimum Weight of %s is required before checking out.</strong>'
                . '<br />Current cart weight: %s',
                wc_format_weight($minimum_weight),
                wc_format_weight($total_weight)
            ), 'error' );
        }
    }
    

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

    【讨论】:

    • 完美运行!!如果客户选择送货地址哥伦比亚,是否也可以实施?谢谢
    • 你好!,看看我是否解释,你发给我的代码是完美的,但我也想扩展和应用任何不能从哥伦比亚访问并将哥伦比亚应用为国家的客户订单中,如果订单未达到最低 20 公斤,请勿让您下订单。非常感谢!对不起我的翻译。来自西班牙的问候
    • Buenas Loic, a ver sí me puedes ayudar, el código qué me pasastes funciona perfectamente, lo que quiero ampliar es que por ejemlo el cliente de otro país que no es Colombia, a la hora de finalizar pedido y ponga como país de envío Colombia le aparezca también la restrictción (ahora mismo no aparece),aparte quiero poner también Argentina。 Muchas gracias por tu ayuda。谢谢!!!
    猜你喜欢
    • 2018-07-23
    • 1970-01-01
    • 2018-05-05
    • 2021-03-01
    • 2021-06-10
    • 1970-01-01
    • 2019-07-29
    • 1970-01-01
    • 2016-09-08
    相关资源
    最近更新 更多