Neil Matthews

Author: Neil Matthews

  • How To Troubleshoot WooCommerce Emails

    How To Troubleshoot WooCommerce Emails

    In this video post I’ll show you how to troubleshoot your WooCommerce emails with a very handy plugin.

    If you are developing a new template for your emails, adding styling or troubleshooting an issue with your Woo emails it’s a real pain in the behind to have to create test orders to see what the email looks like.

    This plugin will speed up your development and troubleshooting efforts.

    Video

    Plugin

    Here’s a link to the plugin used in the video

    Wrap UP

    This is a great plugin to speed up your development process.

    If you need help customising your WooCommerce emails please get in touch.

    Photo by Mathyas Kurmann on Unsplash

  • The Vital Importance of Keeping WordPress Plugins Up to Date

    The Vital Importance of Keeping WordPress Plugins Up to Date

    WordPress is the backbone of a significant portion of the web, powering everything from personal blogs to major corporate websites. One of the key features that make WordPress so popular is its extensive ecosystem of plugins, which add functionality and customization to websites. However, with great power comes great responsibility. Keeping WordPress plugins up to date is not just a matter of taking advantage of the latest features; it’s crucial for security, performance, and overall site health. In this blog post, we’ll explore the reasons why maintaining up-to-date plugins is essential for any WordPress site owner.

    1. Security

    Protection Against Vulnerabilities

    Plugins are often the target of cyber-attacks because they can have vulnerabilities that hackers exploit to gain unauthorized access to a website. These vulnerabilities can arise from coding errors, outdated practices, or overlooked security gaps. By updating your plugins regularly, you ensure that you are protected against known vulnerabilities that developers have patched.

    Preventing Malware and Hacks

    Outdated plugins can be an entry point for malware and hackers. Once they gain access, they can deface your website, steal sensitive information, or even use your site to distribute malware to your visitors. Regular updates help to close these security loopholes, making your site less attractive to malicious actors.

    2. Performance and Compatibility

    Improved Functionality

    Plugin updates often include enhancements that improve the performance and functionality of the plugin. This can result in faster load times, reduced server load, and a smoother user experience. Outdated plugins may not perform as efficiently, which can slow down your website and frustrate visitors.

    Ensuring Compatibility

    The WordPress core is updated regularly to introduce new features, improve security, and enhance performance. However, these updates can sometimes cause compatibility issues with plugins that have not been updated. Keeping your plugins up to date ensures that they remain compatible with the latest version of WordPress, preventing potential conflicts and site crashes.

    3. Bug Fixes

    Eliminating Errors

    No software is perfect, and plugins are no exception. Developers continually work to fix bugs and issues that users report. By updating your plugins, you benefit from these fixes, which can resolve errors you might have encountered and improve the overall stability of your site.

    Enhancing User Experience

    Buggy plugins can cause various issues, from broken features to site crashes. These problems can negatively impact the user experience, leading to higher bounce rates and lost traffic. Ensuring that your plugins are up to date helps maintain a seamless and enjoyable experience for your visitors.

    4. Access to New Features

    Staying Competitive

    Plugin developers regularly add new features and improvements to keep up with industry trends and user demands. By keeping your plugins updated, you can take advantage of these new features, which can help you stay competitive and offer the best possible experience to your users.

    Leveraging Innovations

    Technology evolves rapidly, and what was cutting-edge a year ago might be outdated today. Updates often include innovations that leverage the latest technologies, ensuring that your site remains modern and relevant.

    5. Support and Community

    Developer Support

    When you keep your plugins up to date, you are more likely to receive support from developers. Most developers prioritize helping users who are running the latest version of their software. If you encounter issues with an outdated plugin, you might find it challenging to get the help you need.

    Community and Resources

    The WordPress community is vast, with forums, tutorials, and resources available for troubleshooting and learning. However, most of this support is centered around the latest versions of plugins. Keeping your plugins updated means you can fully benefit from the collective knowledge and resources of the community.

    Conclusion

    Maintaining up-to-date WordPress plugins is a critical aspect of managing a successful and secure website. From enhanced security and performance to access to new features and improved support, the benefits are clear. Make it a routine to check for updates and apply them regularly. By doing so, you can ensure that your WordPress site remains secure, efficient, and competitive in the ever-evolving digital landscape. Don’t wait for issues to arise; be proactive in keeping your plugins up to date and enjoy the peace of mind that comes with a well-maintained website.

    If you need help keeping your site up to date check out my ongoing woocommerce support plans.

  • How I Add WooCommerce Orders to My FreshBooks Accounting Software

    How I Add WooCommerce Orders to My FreshBooks Accounting Software

    Running an online store involves juggling multiple tasks, from managing orders and inventory to handling accounting and financial records. One way to streamline these tasks is by automating the process of adding WooCommerce orders to your FreshBooks accounting software. With the help of Zapier and the WooCommerce Zapier plugin, you can set up a workflow that automatically creates and marks invoices as paid in FreshBooks whenever a WooCommerce order is marked as complete. Here’s how you can do it.

    Why Automate WooCommerce Orders to FreshBooks?

    Automating the transfer of WooCommerce orders to FreshBooks offers several benefits:

    • Time-Saving: Reduce the manual effort required to create invoices and update payment statuses.
    • Accuracy: Minimize errors by automating data entry.
    • Efficiency: Streamline your accounting process, allowing you to focus on other aspects of your business.

    Getting Started

    To begin, you’ll need to have the following:

    1. A WooCommerce store with the WooCommerce Zapier plugin installed and activated.
    2. A FreshBooks account.
    3. A Zapier account.

    Step-by-Step Guide

    Step 1: Install and Configure the WooCommerce Zapier Plugin

    1. Purchase and Download: Get the WooCommerce Zapier plugin from the WooCommerce Marketplace.
    2. Install and Activate: Follow the instructions to install and activate the plugin on your WooCommerce site.
    3. Generate API Keys: Navigate to WooCommerce > Settings > Zapier and generate a new API key. Copy the Consumer Key and Consumer Secret.

    Step 2: Create a Zap in Zapier

    1. Log In to Zapier: Log in to your Zapier account or sign up for a new account.
    2. Make a Zap: Click on the “Make a Zap” button to start creating a new Zap.
    3. Set Up the Trigger:
      • App: Choose WooCommerce.
      • Trigger Event: Select “Order Status Updated”.
      • Connect Account: Enter the Consumer Key and Consumer Secret to connect your WooCommerce account.
      • Set Trigger Conditions: Configure the trigger to fire when an order status is updated to “Completed”.

    Step 3: Set Up the Action

    1. Choose Action App: Select FreshBooks as the action app.
    2. Action Event: Choose “Create Invoice”.
    3. Connect Account: Link your FreshBooks account to Zapier.
    4. Map Fields: Map the necessary WooCommerce order fields to the FreshBooks invoice fields. Ensure to include details such as customer information, order items, and total amount.
    5. Additional Action:
      • Add another action to update the invoice status to “Paid” in FreshBooks once it’s created.

    Step 4: Test and Activate Your Zap

    1. Test the Zap: Run a test to ensure the workflow works correctly. Zapier will use sample data to create an invoice in FreshBooks.
    2. Activate the Zap: Once the test is successful, name your Zap and turn it on.

    Example Workflow

    Here’s a practical example of how the Zap works:

    1. A customer places an order on your WooCommerce store.
    2. You mark the order as “Completed” in WooCommerce.
    3. The Zap triggers, and the order details are sent to FreshBooks.
    4. An invoice is created in FreshBooks with all the relevant information.
    5. The invoice is automatically marked as “Paid” in FreshBooks.

    Conclusion

    Integrating WooCommerce with FreshBooks via Zapier simplifies your accounting process, ensuring that your financial records are always up-to-date without manual intervention. By following the steps outlined in this post, you can set up an efficient workflow that saves you time and reduces errors.

    For more information on the WooCommerce Zapier plugin, visit the WooCommerce Marketplace. To learn more about Zapier and its capabilities, check out the Zapier Help Center.

    Automate your WooCommerce to FreshBooks workflow today and experience a seamless accounting process!

  • Integrating WooCommerce with Zapier via the Zapier Plugin

    Integrating WooCommerce with Zapier via the Zapier Plugin

    In today’s fast-paced digital world, automating tasks is key to maintaining efficiency and productivity. One powerful way to streamline your WooCommerce operations is by integrating it with Zapier, a popular automation tool that connects your WooCommerce store with over 2,000 other apps. This integration can save you countless hours by automating repetitive tasks, such as order management, customer follow-ups, and inventory updates. In this blog post, we’ll walk you through how to integrate WooCommerce with Zapier using the Zapier plugin.

    Why Integrate WooCommerce with Zapier?

    Zapier allows you to create automated workflows, known as “Zaps,” between your WooCommerce store and various apps. These workflows can handle a multitude of tasks, such as:

    • Automatically adding new WooCommerce customers to your email marketing list.
    • Sending order notifications to your team via Slack.
    • Updating your CRM with new order details.
    • Creating tasks in project management tools like Trello or Asana when a new order is placed.

    By integrating WooCommerce with Zapier, you can focus more on growing your business and less on managing mundane tasks.

    Getting Started with the Zapier Plugin

    To integrate WooCommerce with Zapier, you’ll need to use the official WooCommerce Zapier plugin. Follow these steps to get started:

    1. Install the WooCommerce Zapier Plugin
    • Purchase and download the WooCommerce Zapier plugin from the WooCommerce Marketplace.
    • Install and activate the plugin on your WooCommerce site. For detailed instructions on how to install a WordPress plugin, refer to the WooCommerce documentation.
    1. Connect WooCommerce to Zapier
    • After activating the plugin, navigate to WooCommerce > Settings > Zapier.
    • Click on the “Add Key” button to generate a new API key for Zapier. Copy the Consumer Key and Consumer Secret provided.
    1. Create a Zap in Zapier
    • Log in to your Zapier account or sign up for a free account if you don’t have one.
    • Click on the “Make a Zap” button to start creating a new Zap.
    • Search for “WooCommerce” and select it as the trigger app.
    • Choose a trigger event, such as “New Order” or “New Customer,” and click “Continue.”
    • Connect your WooCommerce account by entering the Consumer Key and Consumer Secret you copied earlier.
    1. Set Up the Action
    • After setting up the trigger, select the action app. This could be any of the 2,000+ apps available on Zapier.
    • For example, if you want to add new WooCommerce customers to your Mailchimp list, select “Mailchimp” as the action app and choose the “Add/Update Subscriber” action.
    • Connect your Mailchimp account and map the WooCommerce customer data to the corresponding Mailchimp fields.
    1. Test and Activate Your Zap
    • Test the Zap to ensure everything is working correctly.
    • Once the test is successful, give your Zap a name and turn it on.

    Examples of Useful Zaps for WooCommerce

    Here are a few examples of useful Zaps you can create to automate your WooCommerce workflows:

    • Email Marketing: Add new WooCommerce customers to your Mailchimp or Constant Contact list.
    • Team Communication: Send a Slack message to your team when a new order is placed.
    • Customer Relationship Management (CRM): Add new orders to your CRM, such as Salesforce or HubSpot.
    • Project Management: Create a new task in Trello or Asana for each new WooCommerce order.

    Conclusion

    Integrating WooCommerce with Zapier opens up a world of automation possibilities, allowing you to save time and streamline your business operations. By following the steps outlined in this post, you can easily connect your WooCommerce store with Zapier and start creating powerful automated workflows.

    For more information and to get started with the WooCommerce Zapier plugin, visit the WooCommerce Marketplace. Happy automating!

    Additional Resources

    By automating your WooCommerce store with Zapier, you can take your business efficiency to the next level. Start integrating today and watch as repetitive tasks are handled seamlessly, allowing you to focus on what truly matters – growing your business.

  • Adding Custom Shipping to WooCommerce Checkout for Heavy Products

    Adding Custom Shipping to WooCommerce Checkout for Heavy Products

    When running an e-commerce store, shipping logistics can be a crucial aspect to consider, especially when dealing with heavy products. WooCommerce provides a flexible platform that allows you to customize shipping rules based on various conditions. In this blog post, we will walk you through the steps to add custom shipping to the WooCommerce checkout if a product in the cart weighs more than 1000kg.

    Step 1: Add the Custom Shipping Function

    First, you need to add a function that checks the weight of the products in the cart and applies a custom shipping fee if any product exceeds the specified weight limit. Place this code in your theme’s functions.php file or a custom plugin.

    // Add custom shipping fee for heavy products
    add_action( 'woocommerce_cart_calculate_fees', 'add_custom_shipping_for_heavy_products' );
    
    function add_custom_shipping_for_heavy_products() {
        global $woocommerce;
    
        // Define the weight limit
        $weight_limit = 1000; // Weight in kg
    
        // Define the custom shipping fee amount
        $custom_shipping_fee = 50; // Change this to the desired fee amount
    
        // Check if any product in the cart exceeds the weight limit
        $heavy_product_found = false;
        foreach ( WC()->cart->get_cart() as $cart_item ) {
            $product = $cart_item['data'];
            $product_weight = $product->get_weight();
    
            if ( $product_weight > $weight_limit ) {
                $heavy_product_found = true;
                break;
            }
        }
    
        // If a heavy product is found, add the custom shipping fee
        if ( $heavy_product_found ) {
            WC()->cart->add_fee( __( 'Heavy Product Shipping', 'woocommerce' ), $custom_shipping_fee );
        }
    }

    Step 2: Customize the Shipping Fee and Weight Limit

    In the code snippet above, you can customize the following parameters:

    • $weight_limit: Set this variable to the weight limit (in kg) for which you want to apply the custom shipping fee.
    • $custom_shipping_fee: Set this variable to the amount you want to charge as a custom shipping fee.

    Step 3: Add the Custom Shipping Label Translation

    To ensure the custom shipping label is translatable, wrap the label in the __() function, as shown in the code above. You can replace 'Heavy Product Shipping' with any other label that suits your needs.

    Step 4: Testing the Custom Shipping Fee

    After adding the code to your functions.php file or custom plugin, it’s important to test the functionality:

    1. Add a Heavy Product: Add a product that weighs more than the defined weight limit (1000kg) to the cart.
    2. Proceed to Checkout: Go to the checkout page and verify that the custom shipping fee is applied correctly.
    3. Remove the Product: Remove the heavy product from the cart and ensure that the custom shipping fee is no longer applied.

    Example Use Case

    Let’s consider an example use case where you sell industrial machinery, some of which weigh more than 1000kg. By implementing this custom shipping rule, you can ensure that customers purchasing these heavy items are charged an additional shipping fee to cover the extra shipping costs.

    Conclusion

    By following these steps, you can easily add a custom shipping fee to the WooCommerce checkout process for heavy products. This customization can help you manage additional shipping costs for specific products and improve your store’s functionality.

    Feel free to modify the code to fit your specific requirements. Happy customizing!

  • How To Make WooCommerce Products Exempt From VAT

    How To Make WooCommerce Products Exempt From VAT

    In this video I’ll show you how to make your products exempt or reduced rate for VAT.

    VAT (value added tax) is the sales tax we have here in the UK.

    In our example we’ll talk about zero rated children’s clothes regarding VAT and reduce rate for example child car seats which attract 5% VAT not 20%.

    In this video I’ll show you how to set tax classes for 20%, 5% and zero rated, then I’ll take you through the process of setting a product to use those new tax classes.

    Video

    Wrap Up

    If you need help setting up tax on your WooCommerce store, please get in touch.

  • Adding a Custom Fee to WooCommerce Checkout Based on Product Category

    Adding a Custom Fee to WooCommerce Checkout Based on Product Category

    WooCommerce provides a flexible platform for customizing your online store. One useful customization is adding a custom fee to the checkout process based on the product category in the cart. This can be particularly useful if you need to charge extra for certain types of products, such as those in a “custom fee” category.

    In this blog post, we will guide you through the steps to add a custom fee to the WooCommerce checkout if a product from the “custom fee” category is in the cart. We will include the necessary code snippets and explanations to help you implement this feature.

    Step 1: Add the Custom Fee Function

    First, we need to add a function that checks if there are any products from the “custom fee” category in the cart and then adds the custom fee accordingly. Place this code in your theme’s functions.php file or a custom plugin.

    // Add a custom fee if a product from the "custom fee" category is in the cart
    add_action( 'woocommerce_cart_calculate_fees', 'add_custom_fee_based_on_category' );
    
    function add_custom_fee_based_on_category() {
        global $woocommerce;
    
        // Define the category for which the custom fee should be applied
        $custom_fee_category = 'custom fee';
    
        // Define the custom fee amount
        $custom_fee_amount = 10; // Change this to the desired fee amount
    
        // Check if any product from the defined category is in the cart
        $category_found = false;
        foreach ( WC()->cart->get_cart() as $cart_item ) {
            $product = wc_get_product( $cart_item['product_id'] );
            if ( has_term( $custom_fee_category, 'product_cat', $product->get_id() ) ) {
                $category_found = true;
                break;
            }
        }
    
        // If a product from the category is found, add the custom fee
        if ( $category_found ) {
            WC()->cart->add_fee( __( 'Custom Fee', 'woocommerce' ), $custom_fee_amount );
        }
    }

    Step 2: Customize the Fee Amount and Category

    In the code snippet above, you can customize the following parameters:

    • $custom_fee_category: Set this variable to the slug of the product category for which you want to apply the custom fee.
    • $custom_fee_amount: Set this variable to the amount you want to charge as a custom fee.

    Step 3: Add the Custom Fee Label Translation

    To ensure the custom fee label is translatable, wrap the label in the __() function, as shown in the code above. You can replace 'Custom Fee' with any other label that suits your needs.

    Step 4: Testing the Custom Fee

    After adding the code to your functions.php file or custom plugin, it’s important to test the functionality:

    1. Add a Product from the “Custom Fee” Category: Add a product that belongs to the “custom fee” category to the cart.
    2. Proceed to Checkout: Go to the checkout page and verify that the custom fee is applied correctly.
    3. Remove the Product: Remove the product from the cart and ensure that the custom fee is no longer applied.

    Conclusion

    By following these steps, you can easily add a custom fee to the WooCommerce checkout process based on the product category. This customization can help you manage additional charges for specific products and improve your store’s functionality.

    Feel free to modify the code to fit your specific requirements. Happy customizing!

  • Creating a Custom Email in WooCommerce

    Creating a Custom Email in WooCommerce

    WooCommerce is a powerful and flexible e-commerce platform built on WordPress. Customizing it to suit your specific needs can be a game-changer for your online business. One such customization is creating custom email notifications to send to your vendors. This can be useful for order notifications, updates, or any specific information that needs to be communicated to your vendors.

    In this blog post, we will walk through the steps to create a custom email in WooCommerce to send to a vendor. We will cover the following:

    1. Setting up the custom email class.
    2. Registering the custom email with WooCommerce.
    3. Triggering the email based on specific events.

    Step 1: Setting Up the Custom Email Class

    The first step is to create a custom email class. This class will define the content and behavior of the email. Place this code in your theme’s functions.php file or a custom plugin.

    if ( ! class_exists( 'WC_Vendor_Email' ) ) {
        class WC_Vendor_Email extends WC_Email {
    
            public function __construct() {
                $this->id = 'wc_vendor_email';
                $this->title = 'Vendor Email';
                $this->description = 'This email is sent to the vendor when a new order is placed.';
                $this->heading = 'New Order Notification';
                $this->subject = 'New Order Received';
    
                $this->template_html  = 'emails/vendor-email.php';
                $this->template_plain = 'emails/plain/vendor-email.php';
    
                add_action( 'woocommerce_order_status_completed_notification', array( $this, 'trigger' ) );
    
                parent::__construct();
            }
    
            public function trigger( $order_id ) {
                if ( ! $order_id ) return;
    
                $this->object = wc_get_order( $order_id );
                $this->recipient = '[email protected]'; // Replace with the vendor's email address
    
                if ( ! $this->is_enabled() || ! $this->get_recipient() ) {
                    return;
                }
    
                $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
            }
    
            public function get_content_html() {
                return wc_get_template_html( $this->template_html, array(
                    'order'         => $this->object,
                    'email_heading' => $this->get_heading(),
                    'sent_to_admin' => false,
                    'plain_text'    => false,
                    'email'         => $this,
                ) );
            }
    
            public function get_content_plain() {
                return wc_get_template_html( $this->template_plain, array(
                    'order'         => $this->object,
                    'email_heading' => $this->get_heading(),
                    'sent_to_admin' => false,
                    'plain_text'    => true,
                    'email'         => $this,
                ) );
            }
    
        }
    }

    Step 2: Registering the Custom Email with WooCommerce

    Next, we need to register our custom email with WooCommerce. This will make WooCommerce aware of the new email class we just created. Add the following code to the same file where you defined the custom email class.

    add_filter( 'woocommerce_email_classes', 'register_vendor_email' );
    function register_vendor_email( $email_classes ) {
        $email_classes['WC_Vendor_Email'] = include( 'path/to/WC_Vendor_Email.php' ); // Adjust the path as needed
        return $email_classes;
    }

    Step 3: Triggering the Email Based on Specific Events

    In our custom email class, we added a trigger to send the email when an order is completed. This is done using the woocommerce_order_status_completed_notification hook. If you want to trigger the email based on different events, you can change the hook accordingly.

    For example, to trigger the email when an order is placed, you can use the woocommerce_thankyou hook.

    add_action( 'woocommerce_thankyou', 'trigger_vendor_email', 10, 1 );
    function trigger_vendor_email( $order_id ) {
        $email = WC()->mailer()->emails['WC_Vendor_Email'];
        $email->trigger( $order_id );
    }

    Customizing the Email Template

    Finally, you need to create the email templates. Place the HTML and plain text templates in your theme’s WooCommerce email directory (your-theme/woocommerce/emails/).

    HTML Template (vendor-email.php):

    <?php
    if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
    
    echo $email_heading . "\n\n";
    
    echo '<p>Dear Vendor,</p>';
    echo '<p>You have received a new order. Here are the details:</p>';
    
    // Include order details template
    wc_get_template( 'emails/email-order-details.php', array( 'order' => $order ) );
    
    echo '<p>Best regards,</p>';
    echo '<p>Your Company</p>';
    
    echo $email_footer;
    ?>

    Plain Text Template (plain/vendor-email.php):

    <?php
    if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
    
    echo $email_heading . "\n\n";
    
    echo "Dear Vendor,\n";
    echo "You have received a new order. Here are the details:\n\n";
    
    // Include order details template
    wc_get_template( 'emails/plain/email-order-details.php', array( 'order' => $order ) );
    
    echo "Best regards,\n";
    echo "Your Company\n";
    
    echo $email_footer . "\n";
    ?>

    Conclusion

    By following these steps, you can create a custom email in WooCommerce to send notifications to your vendors. This customization can improve communication and streamline processes in your e-commerce store. Remember to test your custom email thoroughly to ensure it works as expected.

    Feel free to modify the code and templates to fit your specific needs. Happy coding!

  • How I Use Elementor To Customise WooCommerce Single Product Pages

    How I Use Elementor To Customise WooCommerce Single Product Pages

    In this video I’ll show you how I use Elementor to customise WooCommerce single product pages.

    Elementor is a drag and drop design tool that allows me to customise posts, pages and other content types using drag and drop in a no-code manner. No code means you don’t need to be a developer to create great looking designs.

    Video

    Wrap Up

    If you need help customising your WooCommerce store, please get in touch.

  • How to Add an Additional Product to the Cart if a Certain Product is Present in WooCommerce

    How to Add an Additional Product to the Cart if a Certain Product is Present in WooCommerce

    In WooCommerce, there are times when you might want to automatically add an additional product to the cart when a specific product is added. This is often referred to as a “forced sell.” In this blog post, we will walk you through how to achieve this using WooCommerce hooks and custom code.

    Step-by-Step Guide

    Prerequisites

    • A running WooCommerce store.
    • Basic knowledge of PHP and WordPress/WooCommerce hooks.

    Step 1: Identify Product IDs

    First, you need to know the IDs of both the primary product and the additional product you want to add to the cart. You can find the product IDs by going to the WooCommerce Products page and hovering over the product names.

    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 automatically add an additional product to the cart when a specific product is added:

    // Add additional product to the cart if a certain product is present
    function add_forced_sell_product( $cart_item_data, $product_id, $variation_id ) {
        // Set the product ID of the primary product
        $target_product_id = 123; // Replace 123 with your primary product ID
    
        // Set the product ID of the additional product
        $additional_product_id = 456; // Replace 456 with your additional product ID
    
        // Check if the primary product is being added to the cart
        if ( $product_id == $target_product_id ) {
            // Check if the additional product is already in the cart
            $found = false;
            foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
                if ( $cart_item['product_id'] == $additional_product_id ) {
                    $found = true;
                    break;
                }
            }
    
            // If the additional product is not found, add it to the cart
            if ( ! $found ) {
                WC()->cart->add_to_cart( $additional_product_id );
            }
        }
    
        return $cart_item_data;
    }
    add_filter( 'woocommerce_add_cart_item_data', 'add_forced_sell_product', 10, 3 );

    Step 4: Save and Test

    After adding the code, save the functions.php file and test your WooCommerce store:

    1. Add the primary product (with the ID you specified) to your cart.
    2. Check the cart to see if the additional product is automatically added.

    Explanation of the Code

    • Hooking into the Cart Process: We use the woocommerce_add_cart_item_data filter to hook into the process of adding items to the cart.
    • Product IDs: We set the target product ID (the product that triggers the addition) and the additional product ID (the product to be added).
    • Cart Check: When the target product is added to the cart, we check if the additional product is already in the cart.
    • Add Product: If the additional product is not already in the cart, we add it using the add_to_cart method.

    Conclusion

    By following these steps, you can easily set up a forced sell in WooCommerce, where adding a specific product to the cart automatically adds another product. This approach can be customized further to meet various business requirements, such as adding multiple products, applying conditions, or modifying quantities.

    Feel free to leave a comment below if you have any questions or need further assistance!

    Happy coding!

  • WooCommerce: Should I Request A Phone Number At Checkout

    WooCommerce: Should I Request A Phone Number At Checkout

    In this video post I’ll talk about the thorny question of requesting a phone number at checkout and the dangers of making the field required.

    I’ll show you a plugin to change options on the checkout phone field and a better way to approach getting a phone number for legitimate purposes.

    REMEMBER: Nobody – Likes – Unsolicited – Sales – Calls

    Here’s a link to the plugin I recommend using to edit checkout fields.

    Checkout Field Editor

    Video

    Wrap UP

    Nobody likes unsolicited sales calls, don’t be a dick.

    If you need help customising your checkout please get in touch for a no obligation quote.

  • Adding Custom Shipping to WooCommerce Checkout for Specific Products

    Adding Custom Shipping to WooCommerce Checkout for Specific Products

    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:

    1. Add the specific product (with the ID you specified) to your cart.
    2. Proceed to the checkout page.
    3. 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!