【问题标题】:Add a fee to WooCommerce per product, based on category根据类别向每个产品的 WooCommerce 添加费用
【发布时间】:2020-06-02 14:40:14
【问题描述】:

我已经尝试了一段时间来使其正常工作,但我还没有找到任何可以完全满足我们需要的解决方案,而且我离 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 自己找到了这段代码 sn-p:

/**
 * 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)它会根据整个购物车价值添加费用,但它应该只对“门票”产品类别中的产品添加 5% 的费用,而不是整个购物车

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    我遇到了同样的问题,遇到了一个代码 sn-p,它让我走上了正确的道路。在我的一生中,我找不到包含 sn-p 的原始站点,但这是我重新设计的版本。

    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' );
    

    这将为购物车中带有门票类别(ID:86)的每件商品添加 5% 的附加费。如果您还没有找到解决方案,我希望这对您有所帮助。

    【讨论】:

    • 非常感谢您的回答@Bartoszkp / @Sartorius!将逻辑推得更远,您将如何根据数量动态更改费用?例如,如果少于 10 张门票收取 5% 的费用,如果有 11 至 20 张门票收取 2.5% 的费用,如果超过 20 张门票则不收取任何费用?会不会有这样的工作:if($specialfeecat == $catid ) { if($quantity <= '10') { $spfee = $spfee + $itmprice * $quantiy * ($spfeeperprod / 2);} } 再次感谢您的帮助!
    【解决方案2】:

    感谢您发布以上代码。我正是需要这个,但对我来说有一些问题。我不希望该类别成为其中的一部分,我希望它适用于所有产品,并且我还要求对每个产品收取固定费用。

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

    /** 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' );
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-23
      • 1970-01-01
      相关资源
      最近更新 更多