根据类别,为每种产品向WooCommerce添加费用 [英] Add a fee to WooCommerce per product, based on category
问题描述
我已经尝试了一段时间以使其正常工作,但是我没有找到能完全满足我们需要的解决方案,而且我离PHP专家还很远,所以我有点迷茫. /p>
我们使用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)它会根据整个购物车的价值添加费用,但应仅对门票"产品类别中的产品添加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' );
这将向购物车中具有票证类别的所有物品(id:86)增加5%的附加费.希望这对您有所帮助,如果您还没有找到解决方案的话.
I've been trying for a while now to get this working but I haven't find any solution that does exactly what we need, and I'm far from an expert in PHP so I'm a bit lost.
We use WooCommerce and WooTickets. The goal is to add a 5% fee for "Service Fee" only to products in the "Tickets" category (ID:34).
We have found this code snipper, that add a fixed cost based on a product category :
// 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' );
The main problem with this solution is that it adds a fixed cost, whereas we need a percentage cost.
We also found this code snippet from WooThemes themselves :
/**
* 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' );
}
But once again, there are a few problems with this solution...
1) The product category is not taken into consideration 2) It adds a fee based on the entire cart value, but it should only add a 5% fee to products in the "Tickets" product category, not the entire cart
I was having the same issue and came across a code snippet that set me on the correct path. For the life of me, I cannot find the original site that housed the snippet, but here is my reworked version.
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' );
This will add a 5% surcharge to each item in the cart with the category of ticket (id: 86). I hope this helps you out, if you have not found a solution already.
这篇关于根据类别,为每种产品向WooCommerce添加费用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!