du0923
2018-08-28 21:49
浏览 87
已采纳

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

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?

图片转代码服务由CSDN问答提供 功能建议

我正在尝试为哥伦比亚国家特别申请20公斤的强制性最小重量,以避免结帐时的结帐 权重低于这个最小权重。

这是我的实际代码,允许我修复最小权重:

  add_action('  woocommerce_check_cart_items','cldws_set_weight_requirements'); 
 
函数cldws_set_weight_requirements(){
 //仅在购物车或结帐页面中运行
 if(is_cart()|| is_checkout()){
 global $ woocommerce; 
 // 在签出之前设置最小权重
 $ minimum_weight = 30; 
 //获取购物车内容总重量
 $ cart_contents_weight = WC() - &gt; cart-&gt; cart_contents_weight; 
 //比较值并添加 错误是购物车的总重量
 if($ cart_contents_weight&lt; $ minimum_weight){
 //显示我们的错误消息
 wc_add_notice(sprintf('&lt; strong&gt;最小重量) 在退房之前需要%s%s。&lt; / strong&gt;'
。  '&lt; br /&gt;当前购物车权重:%s%s',
 $ minimum_weight,
 get_option('woocommerce_weight_unit'),
 $ cart_contents_weight,
 get_option('woocommerce_weight_unit'),
 get_permalink(wc_get_page_id  ('shop'))
),
'错误'); 
} 
} 
} 
   
 
 

如何使其工作? 仅适用于哥伦比亚国家?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douyu1656 2018-08-28 23:27
    已采纳

    Update (for Argentina and Colombia shipping counties)

    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).

    The code:

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

    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:

    enter image description here


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

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

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题