The code in this download folder enables you to set a flat fee shipping rate per product using WooCommerce. The code :
- Adds a custom number field to set your shipping fee on the single product edit screen.
- Outputs a notice before your cart contents encouraging shoppers to increase their orders above the minimum total to receive free shipping.
- Removes other shipping methods when products with custom flat rate shipping fees are added to the cart.
How The Code Works
custom_shipping_costsfunction 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_ratesfilter to modify shipping rates during the cart/checkout process.
- 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_noticefunction 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
woocommerce_before_cart_tableaction hook to insert the notice on the cart page.
- 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_actionlines, can affect where the notice is displayed.
Copy and paste the code to the end of your child themes functions file or custom functionality/code snippets plugin.