基于WooCommerce中购物车小计和运输国家/地区的自定义费用 [英] Custom fee based on cart subtotal and shipping country in WooCommerce

查看:86
本文介绍了基于WooCommerce中购物车小计和运输国家/地区的自定义费用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我根据在这里找到的一些代码编写了以下代码.但不幸的是,它不起作用.我需要根据国家/地区清单和超过1000`的购物车价值收取额外的出口费.

I made the following code based on some codes I found here. But unfortunately, it doesn't work. I need to charge extra export fee based on country list and cart value more than 1000`

add_action( 'woocommerce_cart_calculate_fees','add_fee_weight_based', 10 , 1 );
function add_fee_weight_based( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$fee       = 45; //
$targeted_country == array('AF','AL','DZ','AS','AD','AO','AI','AQ','AG','AR','AM','AW','AU','AZ','BS','BH','BD','BB','BY','BZ','BJ','BM','BT','BO','BQ','BA','BW','BV','BR','IO','BN','BF','BI','KH','CM','CA','CV','KY','CF','TD','CL','CN','CX','CC','CO','KM','CG','CD','CK','CR','CI','CU','CW','DJ','DM','DO','EC','EG','SV','GQ','ER','ET','FK','FO','FJ','GF','PF','TF','GA','GM','GE','GH','GI','GL','GD','GP','GU','GT','GG','GN','GY','HT','HM','VA','HN','HK','IS','IN','ID','IR','IQ','IM','IL','JM','JP','JE','JO','KZ','KE','KI','KP','KR','KW','KG','LA','LB','LS','LR','LY','LI','MO','MK','MG','MW','MY','MV','ML','MH','MQ','MR','MU','YT','MX','FM','MD','MC','MN','ME','MS','MA','MZ','MM','NA','NR','NP','NC','NZ','NI','NE','NG','NU','NF','MP','NO','OM','PK','PW','PS','PA','PG','PY','PE','PH','PN','PR','QA','RE','RU','RW','BL','SH','KN','LC','MF','PM','VC','WS','SM','ST','SA','SN','RS','SC','SL','SG','SX','SB','SO','ZA','GS','SS','LK','SD','SR','SJ','SZ','CH','SY','TW','TJ','TZ','TH','TL','TG','TK','TO','TT','TN','TR','TM','TC','TV','UG','UA','AE','GB','US','UM','UY','UZ','VU','VE','VN','VG','VI','WF','EH','YE','ZM','ZW');
$cart_value      = $cart->get_cart_contents_total(); // Total value
if ( $cart_value > 1000 && WC()->customer->get_shipping_country() == $targeted_country ) {
$cart->add_fee(_('Export Document'), ($fee), true );
}
}

推荐答案

您的代码中存在一些错误,例如 $ targeted_country == 应该是 $ targeted_country = ,并且您应该在最终的 IF 国家/地区声明中使用 in_array().

There are some mistakes in your code like $targeted_country == should be $targeted_country = and also you should use in_array() in your final IF statement for countries.

改为使用以下内容:

add_action( 'woocommerce_cart_calculate_fees', 'add_fee_country_and_min_amount_based' );
function add_fee_country_and_min_amount_based( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;
        
    $fee_ammount = 45; // Fee amount
    $min_ammount = 1000; // Cart min amount
    $countries   = array( 'AF', 'AL', 'DZ', 'AS', 'AD', 'AO', 'AI', 'AQ', 'AG', 'AR', 'AM', 
        'AW', 'AU', 'AZ', 'BS', 'BH', 'BD', 'BB', 'BY', 'BZ', 'BJ', 'BM', 'BT', 'BO', 'BQ', 
        'BA', 'BW', 'BV', 'BR', 'IO', 'BN', 'BF', 'BI', 'KH', 'CM', 'CA', 'CV', 'KY', 'CF', 
        'TD', 'CL', 'CN', 'CX', 'CC', 'CO', 'KM', 'CG', 'CD', 'CK', 'CR', 'CI', 'CU', 'CW', 
        'DJ', 'DM', 'DO', 'EC', 'EG', 'SV', 'GQ', 'ER', 'ET', 'FK', 'FO', 'FJ', 'GF', 'PF', 
        'TF', 'GA', 'GM', 'GE', 'GH', 'GI', 'GL', 'GD', 'GP', 'GU', 'GT', 'GG', 'GN', 'GY', 
        'HT', 'HM', 'VA', 'HN', 'HK', 'IS', 'IN', 'ID', 'IR', 'IQ', 'IM', 'IL', 'JM', 'JP', 
        'JE', 'JO', 'KZ', 'KE', 'KI', 'KP', 'KR', 'KW', 'KG', 'LA', 'LB', 'LS', 'LR', 'LY', 
        'LI', 'MO', 'MK', 'MG', 'MW', 'MY', 'MV', 'ML', 'MH', 'MQ', 'MR', 'MU', 'YT', 'MX', 
        'FM', 'MD', 'MC', 'MN', 'ME', 'MS', 'MA', 'MZ', 'MM', 'NA', 'NR', 'NP', 'NC', 'NZ', 
        'NI', 'NE', 'NG', 'NU', 'NF', 'MP', 'NO', 'OM', 'PK', 'PW', 'PS', 'PA', 'PG', 'PY', 
        'PE', 'PH', 'PN', 'PR', 'QA', 'RE', 'RU', 'RW', 'BL', 'SH', 'KN', 'LC', 'MF', 'PM', 
        'VC', 'WS', 'SM', 'ST', 'SA', 'SN', 'RS', 'SC', 'SL', 'SG', 'SX', 'SB', 'SO', 'ZA', 
        'GS', 'SS', 'LK', 'SD', 'SR', 'SJ', 'SZ', 'CH', 'SY', 'TW', 'TJ', 'TZ', 'TH', 'TL', 
        'TG', 'TK', 'TO', 'TT', 'TN', 'TR', 'TM', 'TC', 'TV', 'UG', 'UA', 'AE', 'GB', 'US', 
        'UM', 'UY', 'UZ', 'VU', 'VE', 'VN', 'VG', 'VI', 'WF', 'EH', 'YE', 'ZM', 'ZW' );
    
    $cart_total = $cart->get_cart_contents_total(); // Cart content subtotal
    
    if ( $cart_value >= $min_ammount && in_array( WC()->customer->get_shipping_country(), $countries ) ) {
        $cart->add_fee(_('Export Document', 'woocommerce'), $fee_ammount, true );
    }
}

代码进入您的活动子主题(或活动主题)的functions.php文件中.应该可以.

Code goes in functions.php file of your active child theme (or active theme). It should works.

这篇关于基于WooCommerce中购物车小计和运输国家/地区的自定义费用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆