如何计算 WooCommerce 购物车中添加的产品 ID 的额外费用

How to sum the additional fees of added product ID's in WooCommerce cart

我正在使用 答案代码(第 2 部分)。此代码用于向使用其 ID 描述的产品添加额外费用。

我已替换的现有答案中:

// Settings
$settings = array(
    array(
        'product_id' => 30,
        'amount'     => 5,
        'name'       => __( 'Additional service fee', 'woocommerce' ),
    ),
    array(
        'product_id' => 813,
        'amount'     => 10,
        'name'       => __( 'Packing fee', 'woocommerce' ),
    ),
    array(
        'product_id' => 815,
        'amount'     => 15,
        'name'       => __( 'Another fee', 'woocommerce' ),
    ),
);

// Settings
$settings = array(
    array(
        'product_id' => __(123, 456, 789),
        'amount'     => 10,
        'name'       => __( 'Additional service fee', 'woocommerce' ),
    ),
    array(
        'product_id' => __(987, 654, 321),
        'amount'     => 20,
        'name'       => __( 'Additiona packing fee', 'woocommerce' ),
    ),
    array(
        'product_id' => __(445, 556, 555),
        'amount'     => 30,
        'name'       => __( 'Additinal specific fee', 'woocommerce' ),
    ),
);

目前,如果我添加两个属于同一“附加费”类别的产品,例如,“'Additional service fee'”只显示一次金额,不计算在内。

我的问题是如何更改代码,以便通过添加属于同一“附加费用”类别的多个产品来考虑该类别的费用总和。

__() WordPress 函数检索翻译,因此与添加多个产品 ID 无关。相反,您可以将多个产品 ID 添加为一个数组。

注意: total_amount 设置用作跟踪总量的计数器,因此不应更改!

注意:我添加了额外的代码,该代码也考虑了产品数量。如果不适用。只需删除 $quantity = $cart_item['quantity']; 并将 $setting['amount'] * $quantity; 更改为 $setting['amount'];

所以你得到:

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( 30, 813, 815 ),
            'amount'        => 5,
            'name'          => __( 'Additional service fee', 'woocommerce' ),
            'total_amount'  => 0,
        ),
        array(
            'product_id'    => array( 817, 819, 820 ),
            'amount'        => 25,
            'name'          => __( 'Packing fee', 'woocommerce' ),
            'total_amount'  => 0,
        ),
        array(
            'product_id'    => array( 825 ),
            'amount'        => 100,
            'name'          => __( 'Another fee', '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 );