Php Woocommerce中基于状态和产品类别的累进购物车项目费用
最近,我尝试在一个Woocommerce项目中使用2个sniped代码,其中我需要为特定产品类别(术语ID:19)和特定国家/地区(佛罗里达州“FL”)设置费用 此外,我需要将该费率乘以该产品类别中的项目(19) 这是我的实际代码:Php Woocommerce中基于状态和产品类别的累进购物车项目费用,php,wordpress,woocommerce,state,fee,Php,Wordpress,Woocommerce,State,Fee,最近,我尝试在一个Woocommerce项目中使用2个sniped代码,其中我需要为特定产品类别(术语ID:19)和特定国家/地区(佛罗里达州“FL”)设置费用 此外,我需要将该费率乘以该产品类别中的项目(19) 这是我的实际代码: add_action('woocommerce_cart_calculate_fees','woocommerce_custom_surcharge'); function woocommerce_custom_surcharge() { $categor
add_action('woocommerce_cart_calculate_fees','woocommerce_custom_surcharge');
function woocommerce_custom_surcharge() {
$category_ID = '19';
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$state = array('FL');
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)
{
// 19 is the ID of the category for which we want to remove the payment gateway
if($term->term_id == $category_ID){
$surcharge = 1;
if ( in_array( WC()->customer->shipping_state, $state && ) ) {
$woocommerce->cart->add_fee( 'State Tire Fee', $surcharge, true, '' );
}
}
如何根据特定类别的商品和特定州的客户设置累进费用
任何帮助都将不胜感激。有一些错误,您的代码有点过时
下面的代码将根据特定产品类别的购物车项目数量以及仅针对特定状态设置累进费用
add_action( 'woocommerce_cart_calculate_fees', 'wc_custom_surcharge', 20, 1 );
function wc_custom_surcharge( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return; // Exit
## Your Settings (below) ##
$categories = array(19);
$targeted_states = array('FL');
$base_rate = 1;
$user_state = WC()->customer->get_shipping_state();
$user_state = empty($user_state) ? WC()->customer->get_billing_state() : $user_state;
$surcharge = 0; // Initializing
// If user is not from florida we exit
if ( ! in_array( $user_state, $targeted_states ) )
return; // Exit
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {
if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ){
// calculating fee based on the defined rate and on item quatinty
$surcharge += $cart_item['quantity'] * $base_rate;
}
}
// Applying the surcharge
if ( $surcharge > 0 ) {
$cart->add_fee( __("State Tire Fee", "woocommerce"), $surcharge, true );
}
}
代码进入活动子主题(或活动主题)的
function.php
文件。经过测试,效果良好。你的问题是什么?当我试图将其放入我的wordpress模板时,我遇到了一个错误。。。该网站只是不工作。。。你能帮我找出可能的错误吗?非常感谢!工作完美!我正在使用这个代码,我希望能帮助别人。。。!祝你过得愉快Loic先生