Add Flat Rate Fee Per Product With Free Shipping Minimum in WooCommerce

The code in this download folder enables you to set a flat fee shipping rate per product using WooCommerce. The code :

  1. Adds a custom number field to set your shipping fee on the single product edit screen.
  2. Outputs a notice before your cart contents encouraging shoppers to increase their orders above the minimum total to receive free shipping.
  3. Removes other shipping methods when products with custom flat rate shipping fees are added to the cart.

How The Code Works

  • The custom_shipping_costs function is designed to modify WooCommerce shipping rates based on specific conditions.
  • It calculates the order total, checks if certain products are in the cart, adds a flat rate fee for those products, and offers free shipping for orders over $100.
  • The code uses the woocommerce_package_rates filter to modify shipping rates during the cart/checkout process.
  • Custom Field for Flat Rate Fee:

    • The code adds a custom text input field named ‘Flat Rate Fee’ to the general product settings in the WooCommerce product editor.
    • It saves the entered value as post meta with the key ‘flat_rate_fee’ when the product is saved or updated.
  • Display Free Shipping Notice:

    • The display_free_shipping_notice function is intended to display a cart notice if the order total is below $100 and products with the ‘flat_rate_fee’ custom field are in the cart.
    • Initially, it checks if any of the specified products are in the cart, then it calculates the order total and displays a notice if conditions are met.
    • The code uses the loop_start or woocommerce_before_cart_table action hook to insert the notice on the cart page.
  • Adjusting Hook Priority:

    • The priority of the action hook determines when the associated function is executed. Lower priority values (e.g., 5) mean the function is executed earlier.
    • In some cases, adjusting the hook priority, as seen in the add_action lines, can affect where the notice is displayed.
  • Code Installation

    Copy and paste the code to the end of your child themes functions file or custom functionality/code snippets plugin.

    Download Folder

    Join 5000+ Followers

    Get The Latest Free & Premium Tutorials Delivered The Second They’re Published.