Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress Woocommerce-基于产品数量的额外费用_Wordpress_Woocommerce - Fatal编程技术网

Wordpress Woocommerce-基于产品数量的额外费用

Wordpress Woocommerce-基于产品数量的额外费用,wordpress,woocommerce,Wordpress,Woocommerce,我在前面的帖子(下面)中找到了这个答案,但我想知道,如果产品数量发生变化,有没有办法根据数量增加额外费用? 假设一个包裹里有100件物品。(问题还在于,所有包装中的物品数量并不相同,有些可能是100件,有些可能是150件、200件或500件) 例子: 1-99 = 1$. 100=不收费。 101 - 199 1$ 200=不收费 201-299=1美元,依此类推。。 每种产品的总价格始终为1美元,但如果他们订购了多个有这些折扣的产品,总价格可能会更高。如果有4种产品具有中断成本,则总计可为4

我在前面的帖子(下面)中找到了这个答案,但我想知道,如果产品数量发生变化,有没有办法根据数量增加额外费用? 假设一个包裹里有100件物品。(问题还在于,所有包装中的物品数量并不相同,有些可能是100件,有些可能是150件、200件或500件) 例子: 1-99 = 1$. 100=不收费。 101 - 199 1$ 200=不收费 201-299=1美元,依此类推。。 每种产品的总价格始终为1美元,但如果他们订购了多个有这些折扣的产品,总价格可能会更高。如果有4种产品具有中断成本,则总计可为4美元

(另外,不确定代码放在哪里)

谢谢大家!

我在这里找到的代码:


更新数量后立即触发
woocommerce\u-after\u-cart\u-item\u-quantity\u-update
。如果稍微修改一下函数(使用
WC()->cart
访问cart对象),可以在两个钩子上运行相同的函数。我认为它可能会继续增加额外的费用,但在我的测试中,它似乎只是重新计算相同费用的正确成本

add_action('woocommerce_cart_calculate_fees' , 'add_custom_fees');
add_action( 'woocommerce_after_cart_item_quantity_update', 'add_custom_fees' );

/**
 * Add custom fee on article specifics
* @param WC_Cart $cart
 */
function add_custom_fees(){
    $fees = 0;

    foreach( WC()->cart->get_cart() as $item ){ 
        // Check if odds and if it's the right item
        if( $item[ 'quantity' ] % 2 == 1 && get_post_meta( $item[ 'product_id' ], '_custom_fee_for_odds', true ) ){
            // You can also put a custom price in each produt with get_post_meta
            $fees += 10;
        }
    }

    if( $fees > 0 ){
        // You can customize the descriptions here
        WC()->cart->add_fee( 'Custom fee (odds paquets)', $fees);
    }

}
它不是一种“费用”,但适合您描述的复杂定价规则类型。
add_action('woocommerce_cart_calculate_fees' , 'add_custom_fees');
add_action( 'woocommerce_after_cart_item_quantity_update', 'add_custom_fees' );

/**
 * Add custom fee on article specifics
* @param WC_Cart $cart
 */
function add_custom_fees(){
    $fees = 0;

    foreach( WC()->cart->get_cart() as $item ){ 
        // Check if odds and if it's the right item
        if( $item[ 'quantity' ] % 2 == 1 && get_post_meta( $item[ 'product_id' ], '_custom_fee_for_odds', true ) ){
            // You can also put a custom price in each produt with get_post_meta
            $fees += 10;
        }
    }

    if( $fees > 0 ){
        // You can customize the descriptions here
        WC()->cart->add_fee( 'Custom fee (odds paquets)', $fees);
    }

}