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
Php 以编程方式从购物车中删除应用的特定费用_Php_Wordpress_Woocommerce - Fatal编程技术网

Php 以编程方式从购物车中删除应用的特定费用

Php 以编程方式从购物车中删除应用的特定费用,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我已通过以下方式向我的WooCommerce购物车申请了特定费用: WC()->cart->add_fee( __( "Delivery Fee"), 50); 上面的代码所做的是,除了小计和运费之外,它还将送货费添加到总数中,并正确显示总计 我现在想以编程方式删除申请的费用,但我无法这样做 我试过这个,但不起作用: WC()->cart->remove_fees( __( "Delivery Fee")); add_action( 'woocommerce_befo

我已通过以下方式向我的WooCommerce购物车申请了特定费用:

WC()->cart->add_fee( __( "Delivery Fee"), 50);
上面的代码所做的是,除了小计和运费之外,它还将送货费添加到总数中,并正确显示总计

我现在想以编程方式删除申请的费用,但我无法这样做

我试过这个,但不起作用:

WC()->cart->remove_fees( __( "Delivery Fee"));
add_action( 'woocommerce_before_cart', 'custom_fees' );
function custom_fees() {
    // Add Fees - This WORKS
    WC()->cart->add_fee( __( "Delivery Fee"), 50);

    // Remove Fees - This DOES NOT WORK
    WC()->cart->remove_fees( __( "Delivery Fee"));
}
这是我的完整代码:

WC()->cart->remove_fees( __( "Delivery Fee"));
add_action( 'woocommerce_before_cart', 'custom_fees' );
function custom_fees() {
    // Add Fees - This WORKS
    WC()->cart->add_fee( __( "Delivery Fee"), 50);

    // Remove Fees - This DOES NOT WORK
    WC()->cart->remove_fees( __( "Delivery Fee"));
}

如何在不必清除购物车的情况下以编程方式删除申请的费用?

取决于您的需要,这里有一个解决方案:

add_action( 'woocommerce_before_calculate_totals', 'custom_fees' );
function custom_fees() {
    // Add Fees - This WORKS
    WC()->cart->add_fee( __( "Delivery Fee"), 50); // gets removed
    WC()->cart->add_fee( __( "Delivery Fee2"), 150); // will not be removed.

    $fees = WC()->cart->get_fees();
    foreach ($fees as $key => $fee) {
        if($fees[$key]->name === __( "Delivery Fee")) {
            unset($fees[$key]);
        }
    }
    WC()->cart->fees_api()->set_fees($fees);
}

根据您的需要,这里有一个解决方案:

add_action( 'woocommerce_before_calculate_totals', 'custom_fees' );
function custom_fees() {
    // Add Fees - This WORKS
    WC()->cart->add_fee( __( "Delivery Fee"), 50); // gets removed
    WC()->cart->add_fee( __( "Delivery Fee2"), 150); // will not be removed.

    $fees = WC()->cart->get_fees();
    foreach ($fees as $key => $fee) {
        if($fees[$key]->name === __( "Delivery Fee")) {
            unset($fees[$key]);
        }
    }
    WC()->cart->fees_api()->set_fees($fees);
}