Product add on plugin, add to cart with url link – rest api


  • I need document because I can not download the extension because it is included in hosting plan.

    https://woo.com/document/product-add-ons/#is-product-add-ons-compatible-with-the-woocommerce-rest-apiIs Product Add-Ons compatible with the WooCommerce REST API?

    Yes! Documentation for how to work with Product Add-ons via the WC REST API is included within the plugin files.

    You can find the documentation by:

    • Downloading a .zip of the latest version of Product Add-Ons from your account’s Downloads Page
    • Unzipping the folder, then looking in the woocommerce-product-addons/docs/api folder.





      WHY?

    I want to add the Woocommerce product to the cart with the url link. There is a special text area on the product page (PRODUCT ADD ON PLUGIN’S FIELD). I want to fill it with “abcd”.

    I can add the functions.php codes required for this and any other codes, if any.

    The blog I need help with is: (visible only to logged in users)

  • answer to my own question

    // Add the inline script to functions.php
    // 
    // change "addon-143-0" input id (product add on plugin id on your page)
    // 
    //  example url link:
    //  
    //  https://domainName.com/product/superProductName/?addon-143-0=fuckTheSystem
    //  
    function add_custom_script_to_functions() {
        ?>
        <script>
            document.addEventListener('DOMContentLoaded', function () {
                // Function to get parameter value from URL
                function getParameterByName(name, url) {
                    if (!url) url = window.location.href;
                    name = name.replace(/[\[\]]/g, "\\$&");
                    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
                        results = regex.exec(url);
                    if (!results) return null;
                    if (!results[2]) return '';
                    return decodeURIComponent(results[2].replace(/\+/g, " "));
                }
    
                // Get the value of the addon parameter from the URL
                var addonValue = getParameterByName('addon-143-0');
    
                // Check if the addon parameter is present in the URL
                if (addonValue !== null) {
                    // Set the value of the text input field with the addon parameter
                    document.getElementById('addon-143-0').value = addonValue;
    
                    // Add a 1-second delay before triggering the add to cart button click
                    setTimeout(function () {
                        // Optional: Automatically trigger the add to cart button click
                        // You may need to adjust the button selector based on your specific WooCommerce setup
                        var addToCartButton = document.querySelector('.single_add_to_cart_button');
                        if (addToCartButton) {
                            addToCartButton.click();
                        }
    					
    					setTimeout(function () {
    							window.location.href = '<?php echo wc_get_checkout_url(); ?>';
                 		   }, 500);
    
    					
                    }, 1000); // 1000 milliseconds = 1 second
                }
            });
        </script>
        <?php
    }
    
    add_action('wp_footer', 'add_custom_script_to_functions');
    
  • Hi there, I’m glad you found the information you needed! Thanks for letting us know.

  • The topic ‘Product add on plugin, add to cart with url link – rest api’ is closed to new replies.