简体   繁体   English

使用 php(WooCommerce 4.5.1)根据产品的类别和数量添加购物车费用

[英]Adding cart fees based on product’s category and quantity using php (WooCommerce 4.5.1 )

I've tried searching for an answer to this but most of the code is old or not exactly what I'm looking for.我试过寻找这个问题的答案,但大部分代码都很旧,或者不是我正在寻找的。 Here is the scenario - when people order digital image rights from our store, they must pay for it's usage as well as a copy fee.这是一个场景 - 当人们从我们的商店订购数字图像版权时,他们必须支付使用费和复印费。 The usage fee is variable based on several factors and non-taxable - this would be the product added to the cart.使用费因多种因素而异,并且不征税 - 这将是添加到购物车的产品。 The copy fee is a fixed, taxable, cost per image.复印费是固定的、应纳税的、每张图像的成本。 I want this to be automatically added to my cart whenever an product from the 'images' category is added.我希望每当添加“图像”类别中的产品时,它会自动添加到我的购物车中。

I found code on WPFactory that adds a fee based on category that works well.我发现 WPFactory 上的代码根据运行良好的类别增加了费用。

add_action( 'woocommerce_cart_calculate_fees', 'wpf_wc_add_cart_fees_by_product_category' );
if ( ! function_exists( 'wpf_wc_add_cart_fees_by_product_category' ) ) {
    /**
     * wpf_wc_add_cart_fees_by_product_category.
     */
    function wpf_wc_add_cart_fees_by_product_category( $cart ) {
        // You need to enter your fees here, in `category slug` => `fee amount` format
        $fees = array(
            'images'    => 25,
        );
        foreach ( $cart->get_cart() as $cart_item_key => $values ) {
            $product_categories = get_the_terms( $values['product_id'], 'product_cat' );
            if ( $product_categories && ! is_wp_error( $product_categories ) ) {
                $product_categories = wp_list_pluck( $product_categories, 'slug' );
                foreach ( $product_categories as $product_category ) {
                    if ( ! empty( $fees[ $product_category ] ) ) {
                        $name      = 'Digital Image Copy Fee'
                        $amount    = $fees;
                        $taxable   = true;
                        $tax_class = '';
                        $cart->add_fee( $name, $amount, $taxable, $tax_class );
                    }
                }
            }
        }
    }
}

The problem is, it only adds the fee once and I need it to multiply the fee based on the total quantity of items from the category in the cart.问题是,它只添加一次费用,我需要它根据购物车中类别的商品总数乘以费用。 I've attempted to calculate $qty and multiply $fee by $qty but my php is incredibly rusty and I have not been successful.我试图计算 $qty 并将 $fee 乘以 $qty,但我的 php 非常生疏,我没有成功。 Any help would be much appreciated.任何帮助将非常感激。

Here is the solution:这是解决方案:

add_action( 'woocommerce_cart_calculate_fees', 'wpf_wc_add_cart_fees_by_product_category', 10, 1 );
function wpf_wc_add_cart_fees_by_product_category( $cart ) {

    $product_cat_id = 119; // set "image" cat id
    $total_qty = 0; // initialize qty products
    $fee = 25; // set fee amount

    foreach ( $cart->get_cart() as $cart_item ) {
        $product_id = $cart_item['product_id'];
        // get all category ids by product id
        $categories = wp_get_post_terms( $product_id, 'product_cat', array( 'fields' => 'ids' ) );
        // if product has "image" category
        if ( in_array( $product_cat_id, $categories ) )  {
            // get quantity
            $total_qty += $cart_item['quantity'];
        }
    }
    
    if ( $total_qty > 0 ) {
        $tax = true; // set "false" for no tax
        $cart->add_fee( __( "Digital Image Copy Fee", "woocommerce" ), $fee * $total_qty, $tax ); // add custom fee
    }

}

The code goes into your theme's functions.php file and it works.代码进入您的主题的functions.php 文件并且它工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 基于 Woocommerce 中特定产品类别购物车项目数量的购物车费用 - Cart fee based on specific product category cart item quantity in Woocommerce 根据Woocommerce中的产品类别将最大商品数量添加到购物车 - Max item quantity added to cart based on product category in Woocommerce 根据 WooCommerce 产品类别禁用特定的购物车项目数量字段 - Disable specific cart item quantity fields based on WooCommerce product category "基于数量计算的产品类别的购物车折扣" - Cart discount for a product category based on quantity calculations 基于 WooCommerce 产品类别购物车项目计数的费用 - Fees based from WooCommerce product categories cart item count 如何计算 WooCommerce 购物车中添加的产品 ID 的额外费用 - How to sum the additional fees of added product ID's in WooCommerce cart 在Woocommerce中仅允许更新一个商品类别的购物车商品数量 - Allow cart item quantity update for only one product category in Woocommerce 为特定WooCommerce产品类别的购物车项目设置最小数量 - Set a minimum quantity for cart items from specific WooCommerce product category 折扣基于 WooCommerce 购物车中某个类别的商品数量计数 - Discount based on item quantity count for a category in WooCommerce cart 根据 WooCommerce 购物车小计自动添加可变数量的特定产品 - Auto add a variable quantity of specific product based on WooCommerce cart subtotal
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM