72549

Progressive cart item fee based on state and on product category in Woocommerce

Question:

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.

Answer1:

There is some errors and your code is a bit outdated… The code bellow will set a progressive fee based on cart item quantity from a specific product category and for a specific state only.

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 ); } }

Code goes in function.php file of your active child theme (or active theme). Tested and works.

Recommend

  • Why do I constantly get ActivityNotFound exceptions?
  • Why addDefaultArguments() removed from Navigation Component version 1.0.0-alpha09? What is WorkAroun
  • Is it necessary to use `relationship()` in SQLAlchemy?
  • Google Cloud CDN started ignoring query strings for storage buckets
  • Pass a ViewModel to Reusable Knockout Control
  • Why is AngularJS considered MV*
  • Access SharePoint online using client object model- Forbidden error
  • change(rewrite) json object's property value in JavaScript
  • Three.js picking is not working correctly with custom geometry
  • Using Decodable protocol with multiples keys
  • Python - Converting XLSX to PDF
  • How to run a .jar file, which contains OpenCV code and was created using ant, from command line?
  • Implementation of ZipCrypto / Zip 2.0 encryption in java
  • Invalidate ASP.NET Identity 2.0 login without removing the application cookie
  • How Can I access WCF services using a Web Reference?
  • search the database based on the value selected from database
  • Starting Bootstrap tour with a button after ending tour once
  • Getting an error serving images from App_Themes when using precompilation?
  • Storyboard iOS MBProgressHUD
  • How to achieve density/heat map effect in iOS (iPhone/iPad)?
  • Retrieving custom entities in CRM 4 C#
  • QueryOver - add restriction on primitive collection
  • Can't perform CORS request using Angularjs
  • VS2010: Ctrl-PgUp / -PgDown like in browsers
  • Delete a ListItem by clicking the ImageView within the ListItem
  • Year over Year Stats from a Crossfilter Dataset
  • Cloud Code: Creating a Parse.File from URL
  • How to integrate angular2-material (alpha 8.2) with angular2-Quickstart app
  • How to get rgb from transparent pixel in js
  • Cross compile glibc for arm, got undefined reference to some unwind functions
  • How do I use TagLib-Sharp to write custom (PRIV) ID3 frames?
  • media foundation H264 decoder not working properly
  • CAS 4 - Not able to retrieve the LDAP groups after successful authentication
  • Running R's aov() mixed effects model from Python using rpy2
  • Access to a Matlab gui from the web
  • JavaScript RegExp Replace