问题:如何在 WooCommerce 购物车中为不同产品添加额外费用

我使用此代码向特定产品添加额外费用。问题是我只能向不同的产品 ID 添加一笔费用。

add_action('woocommerce_cart_calculate_fees', 'add_fees_on_ids'); 
function add_fees_on_ids() { 
   if (is_admin() && !defined

('DOING_AJAX')) {return;} 
   foreach( WC()->cart->get_cart() as $item_keys => $item ) { 
     if( in_array( $item['product_id'], 

fee_ids() )) { 
     WC()->cart->add_fee(__('ADDITIONAL FEE:'), 5); 
     } 
   } 
} 
function fee_ids() { 
   return array( 2179 ); 
}

我需要为不同的产品添加不同的费用 - 例如:

  • 产品 ID 为 1234 的产品 1 将收取“xx”额外费用。

  • 产品 ID 为 5678 的产品 2 将收取“xx”额外费用。

使用此代码,我只能为不同的产品设置一项费用。如何在 WooCommerce 中为不同的产品添加不同的费用?

解答

有几种方法。例如,您确实可以将自定义字段添加到管理产品设置。

但这并不一定很复杂,为此安装一个额外的插件太牵强了!

多次添加相同的代码也是一个坏主意,因为这样您将多次浏览购物车。这不仅会减慢您的网站速度,而且最终还会导致错误。

添加一个数组,您不仅可以确定产品 ID,还可以根据数组键立即确定额外费用,这在我看来是最简单有效的方法。

所以你得到:

function action_woocommerce_cart_calculate_fees( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Add in the following way: Additional fee => Product ID
    $settings = array(
        10  => 1234,
        20  => 5678,
        5   => 30,
        2   => 815,
    );
    
    // Initialize
    $additional_fee = 0;
    
    // Loop through cart contents
    foreach ( $cart->get_cart_contents() as $cart_item ) {
        // Get product id
        $product_id = $cart_item['product_id'];
        
        // In array, get the key as well
        if ( false !== $key = array_search( $product_id, $settings ) ) {
            $additional_fee += $key;
        }
    }

    // If greater than 0, so a matching product ID was found in the cart
    if ( $additional_fee > 0 ) {
        // Add additional fee (total)
        $cart->add_fee( __( 'Additional fee', 'woocommerce' ), $additional_fee, false );
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'action_woocommerce_cart_calculate_fees', 10, 1 );

可选的:

要单独显示每笔费用的添加,以便客户知道哪个费用是指哪个产品,您可以使用多维数组。

将必要的信息添加到设置数组中,其他一切都会自动发生。

所以你得到:

function action_woocommerce_cart_calculate_fees( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // 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' ),
        ),
    );
    
    // Loop through cart contents
    foreach ( $cart->get_cart_contents() as $cart_item ) {      
        // Get product id
        $product_id = $cart_item['product_id'];
        
        // Loop trough settings array
        foreach ( $settings as $setting ) {
            // Search for the product ID
            if ( $setting['product_id'] == $product_id ) {
                // Add fee
                $cart->add_fee( $setting['name'], $setting['amount'], false );
            }
        }       
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'action_woocommerce_cart_calculate_fees', 10, 1 );

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

Logo

WooCommerce社区为您提供最前沿的新闻资讯和知识内容

更多推荐