【问题标题】:How to sum the additional fees of added product ID's in WooCommerce cart如何汇总 WooCommerce 购物车中添加的产品 ID 的额外费用
【发布时间】:2021-11-07 10:53:36
【问题描述】:

我正在使用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' ),
    ),
);

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

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

【问题讨论】:

    标签: php wordpress woocommerce cart fee


    【解决方案1】:

    __() 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 );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-16
      • 1970-01-01
      • 2018-06-03
      • 2021-10-26
      相关资源
      最近更新 更多