我正在为我的网站设置加州赎回价值费.我在这里找到的代码是有效的,但我希望不同的费用合并在同一个名称下,只需"C.R.V.".但仍会根据购物车中的不同产品计算适当的费用.例如:购物车中的一个5美分CRV项目和一个10美分CRV项目目前将显示在两个单独的行上.我希望结果只是"C.R.V.-0.15c"
Using code from the answer to this page个
我正在使用的代码:
function action_woocommerce_cart_calculate_fees( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Settings (multiple settings arrays can be added/removed if desired)
$settings = array(
array(
'product_id' => array( 11891, 11893 ),
'amount' => 0.05,
'name' => __( '5 Cent C.R.V.', 'woocommerce' ),
'total_amount' => 0,
),
array(
'product_id' => array( 11892 ),
'amount' => 0.10,
'name' => __( '10 Cent C.R.V.', 'woocommerce' ),
'total_amount' => 0,
),
array(
'product_id' => array( 825 ),
'amount' => 0.20,
'name' => __( '20 Cent CRV', 'woocommerce' ),
'total_amount' => 0,
),
);
// Loop through cart contents
foreach ( $cart->get_cart_contents() as $cart_item ) {
// Get product id
$product_id = $cart_item['product_id'];
// Get quantity
$quantity = $cart_item['quantity'];
// Loop trough settings array (determine total amount)
foreach ( $settings as $key => $setting ) {
// Search for the product ID
if ( in_array( $product_id, $settings[$key]['product_id'] ) ) {
// Addition
$settings[$key]['total_amount'] += $setting['amount'] * $quantity;
}
}
}
// Loop trough settings array (output)
foreach ( $settings as $setting ) {
// Greater than 0
if ( $setting['total_amount'] > 0 ) {
// Add fee
$cart->add_fee( $setting['name'], $setting['total_amount'], false );
}
}
}
add_action( 'woocommerce_cart_calculate_fees', 'action_woocommerce_cart_calculate_fees', 10, 1 );
我试着把所有的费用都改成"C.R.V.".但随后购物车将只显示并计算一笔费用.我很抱歉,如果这是非常简单的或盯着我的脸,但我对任何类型的编码都是非常新的.如有任何帮助,我们不胜感激!