我正在我的个人网站上工作,而且我在重量单位方面苦苦挣扎。...可以帮我吗? 第一;我有这段代码,可以显示购物车和结帐页面中的商品重量小计。
// Display the cart item weight in cart and checkout pages
add_filter( 'woocommerce_get_item_data', 'display_custom_item_data', 10, 2 );
function display_custom_item_data( $cart_item_data, $cart_item ) {
if ( $cart_item['data']->get_weight() > 0 ){
$cart_item_data[] = array(
'name' => __( 'Weight subtotal', 'woocommerce' ),
'value' => ( $cart_item['quantity'] * $cart_item['data']->get_weight() ) . ' ' . get_option('woocommerce_weight_unit')
);
}
return $cart_item_data;
}
但是我想自动显示基于国家的分类汇总。例如,如果客户来自美国或加拿大,我必须将小计转换为磅和英寸,而不是公斤和厘米。 我已经使用此代码成功在“其他信息”区域的产品页面中进行了操作:
// For Weight
add_filter( 'woocommerce_format_weight', 'imperial_format_weight', 20, 2 );
function imperial_format_weight( $weight_string, $weight ) {
$country = WC()->customer->get_shipping_country(); // Customer country
$countries = array( 'US', 'LR', 'MM' ); // Imperial measurement countries
if ( ! in_array( $country, $countries ) ) return $weight_string; // Exit
$weight_unit = get_option( 'woocommerce_weight_unit' );
$weight_string = wc_format_localized_decimal( $weight );
if ( empty( $weight_string ) )
return __( 'N/A', 'woocommerce' ); // No values
if ( $weight_unit == 'kg' ) {
// conversion rate for 'kg' to 'lbs'
$rate = 2.20462;
$label = ' lbs';
} elseif ( $weight_unit == 'g' ) {
// conversion rate for 'g' to 'oz'
$rate = 0.035274;
$label = ' oz';
}
return round( $weight * $rate, 2 ) . $label;
}
,现在我想知道如何正确显示购物车中的此小计并检查每个国家/地区具有相应单位的页面。 谢谢您的帮助!