我正在为我的网站设置加州赎回价值费.我在这里找到的代码是有效的,但我希望不同的费用合并在同一个名称下,只需"C.R.V.".但仍会根据购物车中的不同产品计算适当的费用.例如:购物车中的一个5美分CRV项目和一个10美分CRV项目目前将显示在两个单独的行上.我希望结果只是"C.R.V.-0.15c"

Currently Displayed as:

Using code from the answer to this page

我正在使用的代码:

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( 11891, 11893 ),
            'amount'        => 0.05,
            'name'          => __( '5 Cent C.R.V.', 'woocommerce' ),
            'total_amount'  => 0,
        ),
        array(
            'product_id'    => array( 11892 ),
            'amount'        => 0.10,
            'name'          => __( '10 Cent C.R.V.', 'woocommerce' ),
            'total_amount'  => 0,
        ),
        array(
            'product_id'    => array( 825 ),
            'amount'        => 0.20,
            'name'          => __( '20 Cent CRV', '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 );

我试着把所有的费用都改成"C.R.V.".但随后购物车将只显示并计算一笔费用.我很抱歉,如果这是非常简单的或盯着我的脸,但我对任何类型的编码都是非常新的.如有任何帮助,我们不胜感激!

推荐答案

如果我已经很好地理解了,以下将适用于定义产品的合并‘CRV’费用,基于您的设置.

我已经简化了您的代码,请try :

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: amount / product Ids
    $settings = array(
        '0.05' => array( 11891, 11893 ),
        '0.1'  => array( 11892 ),
        '0.2'  => array( 825 ),
    );

    $total_amount = 0; // Initializing
    
    // Loop through cart items
    foreach ( $cart->get_cart() as $item ) {
        // Loop trough settings array
        foreach ( $settings as $amount => $product_ids ) {
            // Search for the product ID
            if ( in_array( $item['product_id'], $product_ids) ) {
                $total_amount += floatval($amount) * intval($item['quantity']); // Add to total amount
            }
        }
    }

    if ( $total_amount > 0 ) {
        $cart->add_fee( __( 'CRV', 'woocommerce' ), $total_amount, false ); 
    }
}

应该能行得通.

Php相关问答推荐

将一个数字分配到一系列因素中(就像贪婪算法)

有条件的运费率基于购物车小计与WooPayments货币转换

邮箱打开跟踪器不工作时发送邮件使用laravel调度程序

如何使用属性#[Embedded]嵌入带有规则的对象集合?

以编程方式更改新订单、已取消订单和失败订单的邮箱WooCommerce通知

Symfony Api平台:按自定义实体方式排序结果

如果WooCommerce中存在特定的运输方式,则隐藏其他运输方式

在Google云存储对象上从PHP设置缓存控制TTL会更改错误的元数据

在WooCommerce中禁用特定日期之前的免费送货

如何删除WordPress用户的个人资料描述或其他个人资料字段中的链接?

如果为布尔值,则 for each 循环重新启动

使用php ZipArhive类将Zip压缩文件分成多个部分

在 Woocommerce 邮箱订单中显示产品 GTIN

调用未定义的方法 mysqli::execute_query()

使用自定义插件中的 WooCommerce 模板发送自定义邮箱

如何在PHP中对多个脚本调用进行排队?

在 Symfony 测试中何时使用 TestCase 而不是 KernelTestCase

Laravel 响应 html 默认页面忽略控制器

Symfony 在将它传递给 Twig 时是否从数据库中获取整个对象?

如何将 2021-04-08T22:25:09.335541Z 作为时间戳保存到 Laravel 中的数据库中?