Adding custom shipping methods to the WooCommerce checkout process can be essential for tailoring shipping options to specific products. This blog post will guide you through the process of adding a custom shipping method when a particular product is in the cart using WooCommerce hooks and filters.
Step-by-Step Guide
Prerequisites
- A running WooCommerce store.
- Basic knowledge of PHP and WordPress/WooCommerce hooks.
Step 1: Identify the Product ID
First, you need to know the ID of the product for which you want to add custom shipping. You can find the product ID by going to the WooCommerce Products page and hovering over the product name.
Step 2: Add Custom Code to Your Theme
We will add the custom code to the functions.php
file of your active theme. You can access this file via the WordPress admin dashboard or using FTP.
Step 3: Write the Code
Below is the code to add a custom shipping method when a specific product is in the cart:
// Add custom shipping method for specific product in WooCommerce
function add_custom_shipping_method( $available_methods ) {
global $woocommerce;
// Set the product ID for which the custom shipping should be applied
$target_product_id = 123; // Replace 123 with your product ID
// Loop through the cart items
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $cart_item ) {
if ( $cart_item['product_id'] == $target_product_id ) {
// Define the custom shipping method
$custom_shipping_method = array(
'custom_shipping' => array(
'id' => 'custom_shipping',
'label' => __( 'Custom Shipping', 'woocommerce' ),
'cost' => '10.00', // Set the custom shipping cost
),
);
// Merge custom shipping method with available methods
$available_methods = array_merge( $available_methods, $custom_shipping_method );
break; // No need to add the shipping method more than once
}
}
return $available_methods;
}
add_filter( 'woocommerce_package_rates', 'add_custom_shipping_method' );
Step 4: Save and Test
After adding the code, save the functions.php
file and test your WooCommerce checkout process:
- Add the specific product (with the ID you specified) to your cart.
- Proceed to the checkout page.
- You should see the custom shipping method applied to the order.
Explanation of the Code
- Hooking into the Shipping Methods: We use the
woocommerce_package_rates
filter to add our custom shipping method to the available shipping methods. - Product ID and Shipping Cost: We set the target product ID and the custom shipping cost.
- Cart Loop: We loop through the cart items to check if the target product is in the cart.
- Define Custom Shipping Method: If the target product is found, we define the custom shipping method with its ID, label, and cost.
- Merge Shipping Methods: We merge the custom shipping method with the existing available shipping methods.
Conclusion
By following these steps, you can easily add a custom shipping method to the WooCommerce checkout process for specific products. This approach can be extended and customized further to meet various business requirements, such as applying different shipping methods based on product categories, quantities, or customer locations.
Feel free to leave a comment below if you have any questions or need further assistance!
Happy coding!