Categories

Sunday, December 22, 2024
#919814419350 therichposts@gmail.com
WoocommerceWoocommerce HooksWordpress

How to offer shipping on products but local pickup only in WooCommerce?

How to offer shipping on products but local pickup only in WooCommerce?

Setting up different delivery options for specific products in WooCommerce involves using conditional logic to control shipping methods based on the products in the cart. Here’s a step-by-step guide on how to offer shipping for one product and local pickup only for another within WooCommerce:

Step 1: Set Up Shipping Zones

First, you need to configure your shipping zones and methods in WooCommerce:

  1. Go to WooCommerce > Settings > Shipping in your WordPress dashboard.
  2. Add a Shipping Zone and name it (e.g., “Domestic” for local shipping).
  3. Add Shipping Methods to this zone. You’ll likely want to add both ‘Flat Rate’ and ‘Local Pickup’. You can configure these based on your needs.

Step 2: Install a Plugin for Conditional Shipping

Since WooCommerce by default doesn’t allow you to disable shipping methods based on specific products directly, you’ll need a plugin that can handle conditional logic for shipping. Here are a couple of popular options:

  • WooCommerce Conditional Shipping and Payments: This plugin allows you to create conditions for shipping methods and payment gateways based on various factors, including product, category, cart subtotal, and more.
  • Table Rate Shipping by WooCommerce (if you need more detailed shipping rules based on weight, items, etc.).

Step 3: Configure the Plugin

Once you have installed your chosen plugin, configure it to set up conditions for your products:

Using WooCommerce Conditional Shipping and Payments

  1. Go to WooCommerce > Settings > Conditional Shipping and Payments.
  2. Add a New Condition. Here, you can specify conditions such as:
  • If Product A is in the cart, enable ‘Flat Rate’.
  • If Product B is in the cart, disable ‘Flat Rate’ and enable only ‘Local Pickup’.

Step 4: Test Your Setup

  • Add Product A to the cart and check if both shipping and pickup options are available.
  • Add Product B to the cart and check if only the local pickup is available.

Make sure to test various combinations of products to ensure that the conditions work as expected. Testing in a staging environment before going live is also a good practice to avoid disrupting your live store.

Optional: Customize Further with Code

If you’re comfortable with coding or have access to a developer, you can also achieve this functionality by adding custom functions to your theme’s functions.php file. This method requires a good understanding of PHP and WooCommerce hooks.

Here’s a basic example of how you might code this:

function custom_override_shipping_methods($available_methods) {
    global $woocommerce;
    $specific_product_id = 123;  // ID of the product that should only have local pickup

    // Check if the specific product is in the cart
    foreach($woocommerce->cart->cart_contents as $key => $values) {
        if($values['product_id'] == $specific_product_id) {
            // Remove all methods except local pickup
            foreach($available_methods as $method_id => $method) {
                if($method_id !== 'local_pickup:1') {  // Ensure the correct ID for local pickup
                    unset($available_methods[$method_id]);
                }
            }
            break;
        }
    }

    return $available_methods;
}
add_filter('woocommerce_package_rates', 'custom_override_shipping_methods', 100);

This code checks if a specific product is in the cart and, if so, removes all shipping methods except local pickup.

Final Thoughts

Using a plugin is the easiest and most flexible approach for most users, especially those who prefer not to edit code. If your needs grow more complex, consider hiring a developer to customize your WooCommerce store to precisely match your business requirements.

therichpost
the authortherichpost
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 19, MedusaJs, Next.js, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.