[英]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.