根据类别为每个产品向WooCommerce添加费用


Add a fee to WooCommerce per product, based on category

我已经尝试了一段时间,但我还没有找到任何能满足我们需求的解决方案,而且我还不是PHP方面的专家,所以我有点不知所措。

我们使用WooCommerce和WooTickets。目标是仅对"门票"类别(ID:34)的产品增加5%的"服务费"费用。

我们发现了这个代码剪切器,它添加了基于产品类别的固定成本:

// Add Service Fee to Category
function woo_add_cart_fee() {
$category_ID = '23';
global $woocommerce;
foreach ($woocommerce->cart->cart_contents as $key => $values ) {
    // Get the terms, i.e. category list using the ID of the product
$terms = get_the_terms( $values['product_id'], 'product_cat' );
    // Because a product can have multiple categories, we need to iterate through the list of the products category for a match
    foreach ($terms as $term) {
        // 23 is the ID of the category for which we want to remove the payment gateway
        if($term->term_id == $category_ID){
         $excost = 6;
         }
         }
        $woocommerce->cart->add_fee('Service Fee', $excost, $taxable = false, $tax_class = '');
}
}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );

这个解决方案的主要问题是它增加了固定成本,而我们需要一个百分比成本。

我们还发现了WooThemes本身的代码片段:

/**
 * Add a 1% surcharge to your cart / checkout
 * change the $percentage to set the surcharge to a value to suit
 * Uses the WooCommerce fees API
 *
 * Add to theme functions.php
 */
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
  global $woocommerce;
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;
    $percentage = 0.05;
    $surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;    
    $woocommerce->cart->add_fee( 'Service Fee', $surcharge, true, 'standard' );
}

但再一次,这个解决方案有一些问题。。。

1) 未考虑产品类别2) 它根据整个购物车的价值增加费用,但它只应为"Tickets"产品类别中的产品增加5%的费用,而不是整个购物车

我遇到了同样的问题,并遇到了一个代码片段,该代码片段将我设置为正确的路径。就我的一生而言,我找不到包含该片段的原始网站,但这是我的修改版本。

function df_add_ticket_surcharge( $cart_object ) {
    global $woocommerce;
    $specialfeecat = 86; // category id for the special fee
    $spfee = 0.00; // initialize special fee
    $spfeeperprod = 0.05; //special fee per product
    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 = $spfee + $itmprice * $quantiy * $spfeeperprod;
                }
            }
        endif;  
    }
    if($spfee > 0 ) {
        $woocommerce->cart->add_fee( 'Ticket Concierge Charge', $spfee, true, 'standard' );
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'df_add_ticket_surcharge' );

这将为购物车中的每件物品添加5%的附加费,该物品属于车票类别(id:86)。如果你还没有找到解决方案,我希望这能帮助你。

感谢您发布上述代码。我确实需要这个,但对我来说也有一些问题。我不希望这个类别成为其中的一部分,我希望它适用于所有产品,而且我还要求每个产品都有固定的费用。

我遇到的另一个问题是,如果我的购物车里有两种不同的产品,它只会将其计算为一种产品。所以我做了一些研究,找到了一种计算购物车中产品总数的方法。这是我提出的最后一个代码。认为这可能会帮助其他人寻找类似的答案

/** 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 = 10; //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( 'Handling Fee', $spfee, true, 'standard' );
}
}
add_action( 'woocommerce_cart_calculate_fees', 'df_add_handling_fee' );

正如你所看到的,我刚刚注释掉了原始代码中的部分。希望有人能从中受益:)