在 WooCommerce 中为特定国家/地区设置最小允许重量

时间:2023-03-19
本文介绍了在 WooCommerce 中为特定国家/地区设置最小允许重量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to specifically apply a mandatory minimum weight of 20 kilos for the country of Colombia avoiding checkout if the total cart weight is under this minimal weight.

这是我的实际代码,它允许我确定最小权重:

Here is my actual code, that allow me to fix a minimum weight:

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

我怎样才能让它只适用于哥伦比亚国家?

How can I make it work for only for Colombia country?

推荐答案

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

Update (for Argentina and Colombia shipping counties)

我已重新访问您的代码,使其仅适用于最小重量为 20 公斤的哥伦比亚.您需要检查重量单位,因为它应该是Kg"(以公斤为单位).

I have revisited your code and make it work just for colombia with a minimal weight of 20 kilos. You will need to check the weight unit as it should be "Kg" (in 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 文件中.经过测试和工作.

Code goes in function.php file of your active child theme (or active theme). Tested and work.

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

When Colombia is the detected country (or the defined country) you will get something like:

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

The same code for all countries:

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 文件中.经过测试和工作.

Code goes in function.php file of your active child theme (or active theme). Tested and work.

这篇关于在 WooCommerce 中为特定国家/地区设置最小允许重量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:在 Woocommerce 结帐页面中显示一个添加费用的复选 下一篇:在woocommerce中取消特定产品类别的产品标签

相关文章

最新文章