如何在WooCommerce购物车中合计增加的产品ID的额外费用 [英] How to sum the additional fees of added product ID's in WooCommerce cart

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

问题描述

我使用How to add additional fees to different products 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' ),
    ),
);

目前,如果我添加两个属于同一&q;附加费&q;类别的产品,则&q;‘附加服务费’&q;只会显示一次金额,不会计算在内。

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

wordpress

推荐答案函数检索翻译,因此与添加多个ProductID无关。相反,您可以将多个产品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 );

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

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