Php 为商业中的特定国家/地区设置允许的最小重量
我试图明确规定哥伦比亚国家的最低强制重量为20公斤,如果购物车总重量低于此最低重量,则避免结帐 这是我的实际代码,允许我确定最小重量:Php 为商业中的特定国家/地区设置允许的最小重量,php,woocommerce,cart,checkout,country,Php,Woocommerce,Cart,Checkout,Country,我试图明确规定哥伦比亚国家的最低强制重量为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_car
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' );
}
}
}
add_action('woocommerce_check_cart_items'、'cldws_set_weight_requirements');
功能cldws\u设置\u重量\u要求(){
//仅在购物车或结帐页面中运行
if(is_cart()| | is_checkout()){
全球商业;
//退房前设置最小重量
$最小重量=30;
//获取购物车的内容总重量
$cart\u contents\u weight=WC()->cart->cart\u contents\u weight;
//比较值并添加一个错误是购物车的总重量
如果($cart\u contents\u weight<$minimum\u weight){
//显示我们的错误消息
wc_添加通知(sprintf(“退房前要求最小重量为%s%s)。”
“
当前购物车重量:%s%s”,
$最小重量,
获取选项(“重量单位”),
$cart\u内容物\u重量,
获取选项(“重量单位”),
获取永久链接(wc获取页面id('shop'))
),
“错误”);
}
}
}
如何使其仅适用于哥伦比亚国家?更新(适用于阿根廷和哥伦比亚航运国家)
我重新访问了您的代码,使其仅适用于哥伦比亚,最低重量为20公斤。您需要检查重量单位,因为它应该是“Kg”(以千克为单位)
守则:
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' );
}
}
}
add_action('woocommerce_check_cart_items'、'checkout_required_minu weight_country_based');
功能检查\所需\最小\重量\基于国家的(){
//仅在购物车或结账页面上
如果(!(is_cart()| is_checkout())返回;
//到达航运国
$country=WC()->session->get('customer')['shipping_country'];
如果(空($国家)){
$country=WC()->session->get('customer')['billing_country'];
}
//哥伦比亚和阿根廷航运国
if(在数组中($country,数组('CO','AR')){
//这里设置最小重量
$最小重量=20;//20千克
//获取购物车的内容总重量
$total_weight=WC()->cart->get_cart_contents_weight();
//如果总重量低于最小值,我们将避免签出并显示错误通知
如果($总重量<$最小重量){
//显示动态错误通知
wc_添加_通知(sprintf(
“退房前要求最小重量为%s。”
“
当前购物车重量:%s”,
wc_格式_重量($minimum_重量),
wc_格式_重量($total_重量)
)“错误”);
}
}
}
代码进入活动子主题(或活动主题)的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' );
}
}
add_动作('woocommerce_check_cart_items'、'checkout_required_minu weight');
功能检查所需的最小重量(){
//仅在购物车或结账页面上
如果(!(is_cart()| is_checkout())返回;
//这里设置最小重量
$最小重量=20;//20千克
//获取购物车的内容总重量
$total_weight=WC()->cart->get_cart_contents_weight();
//如果总重量低于最小值,我们将避免签出并显示错误通知
如果($总重量<$最小重量){
//显示动态错误通知
wc_添加_通知(sprintf(
“退房前要求最小重量为%s。”
“
当前购物车重量:%s”,
wc_格式_重量($minimum_重量),
wc_格式_重量($total_重量)
)“错误”);
}
}
代码进入活动子主题(或活动主题)的function.php文件。经过测试,工作正常。它工作正常!!在客户选择发货地址的情况下是否也可以实施?谢谢你好!,为了了解我是否解释,您发送给我的代码是完美的,但我还想扩展并应用,任何不从哥伦比亚访问并将哥伦比亚作为订单所在国的客户,如果订单未达到最低20公斤,请不要让您下订单。非常感谢你!对不起我的翻译。来自西班牙卢瓦茨、阿尤达尔、卡迪戈·奎梅斯·帕斯特的问候,来自完美的功能,来自哥伦比亚的客户的问候,来自哥伦比亚的客户的问候,来自哥伦比亚环境保护协会的问候,阿根廷塔姆比公寓。谢谢你。谢谢!!!