如何在WooCommerce购物车中为不同的产品增加额外费用 [英] How to add additional fees to different products in WooCommerce cart

查看:107
本文介绍了如何在WooCommerce购物车中为不同的产品增加额外费用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码向特定产品ID添加额外费用。问题是我只能向不同产品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中为不同的产品添加不同的费用?

推荐答案

有几种方法。例如,您确实可以add a custom field to the admin product settings

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

多次添加相同的代码也不是一个好主意,因为那样的话,您将多次检查购物车。这不仅会降低您的网站速度,而且最终还会导致错误。

在我看来,添加一个数组是最简单有效的方法,在该数组中,您不仅可以确定产品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 );

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

这篇关于如何在WooCommerce购物车中为不同的产品增加额外费用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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