Recently I tryied to use 2 sniped codes in one for this Woocommerce project where I need to set a fee for a specific product category (term ID: 19) and and a specific country state (Florida 'FL')
Additionally I should need to multiply this fee rate by the items from this product category (19).
This is my actual code:
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, '' );
}
}
How can I set a progressive fee based on items from a specific category and customers from a specific state?
Any help will be appreciated.