我已经尝试了一段时间才能使其工作但是我找不到任何能够完全满足我们需要的解决方案,而且我远非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%的费用,而不是整个购物车
答案 0 :(得分:8)
我遇到了同样的问题,并遇到了一个代码片段,它让我走上了正确的道路。对于我的生活,我找不到包含该片段的原始网站,但这是我的重做版本。
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)。如果你还没有找到解决方案,我希望这可以帮到你。
答案 1 :(得分: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' );
正如您所看到的,我刚刚从原始代码中注释掉了这些部分。希望有人可以从中受益:)