如何在WooCommerce中从特定产品中排除费用?

时间:2019-01-11 21:32:16

标签: php woocommerce

我一直在尝试在WordPress上找到一个插件,该插件向WooCommerce的特定产品项或类别添加费用,但是还没有运气,我一直在使用对我有用的这段代码

这可以通过向每个商品添加产品费用来完成其需要做的事情。但是现在我需要它来从特定产品ID中排除费用。

这是我在这里找到的将所有项目加收费用的解决方案。我无法将其排除在特定产品IDS之外。感谢您的任何帮助。

/** add handling fee **/
function df_add_handling_fee( $cart_object ) {

global $woocommerce;
// $specialfeecat = 3711; // category id for the special fee
$spfee = 0.00; // initialize special fee
$spfeeperprod = 3.99; //special fee per product

//Getting Cart Contents. 
$cart = $woocommerce->cart->get_cart();
//Calculating Quantity
foreach($cart as $cart_val => $cid){
   $qty += $cid['quantity']; 
}
foreach ( $cart_object->cart_contents as $key => $value ) {

    $proid = $value['product_id']; //get the product id from cart
    //$quantiy = $value['quantity']; //get quantity from cart
    $itmprice = $value['data']->price; //get product price

    $terms = get_the_terms( $proid, 'product_cat' ); //get taxonamy of the prducts
    if ( $terms && ! is_wp_error( $terms ) ) :
        foreach ( $terms as $term ) {
            //$catid = $term->term_id;
            //if($specialfeecat == $catid ) {
                $spfee = $qty * $spfeeperprod;
            //}
        }
    endif;  
}

if($spfee > 0 ) {

    $woocommerce->cart->add_fee( 'Extra Fee', $spfee, false, 'standard' );
}

}

add_action( 'woocommerce_cart_calculate_fees', 'df_add_handling_fee' );

0 个答案:

没有答案