skip to Main Content

How to make the Formcraft contact form be called when clicking the pre-order button? (only pre-order products) Thank you!

Unfortunately I did not find any information in the wordpress CODEX on how I can influence the add to cart button via functions.php to change button link only for products on-backorder.

2

Answers


  1. Chosen as BEST ANSWER

    @Hamdallah thank you for your method, but woocommerce default button still adds products to cart, but it should not be happening in my case.

    That what I could make by now:

    // removing default woocommerce button only for on-backorder type of products
    
    add_filter( 'woocommerce_loop_add_to_cart_link', 'remove_add_to_cart_onbackorder_products', 25, 2 );
                  
    function remove_add_to_cart_onbackorder_products( $add_to_cart_html, $product ) {
        
        if( $product->is_type('simple') && $product->is_on_backorder()) {
            return '';
        }
        return $add_to_cart_html;
        
    }
    
    // making forminator button to call pop-up preorder form 
    
    function preorderbtn(){
        echo do_shortcode( '[button text="Pre-order" link="#checkprice" class="clickbutton"]' );
    }
    
    // calling button on-backorder products 
    
    add_filter('woocommerce_product_add_to_cart_url', 'pre_order_button_url', 10, 2); 
    function pre_order_button_url($url, $product) { 
        if ($product->is_on_backorder()) { 
        preorderbtn();
        } 
        return $url; 
    } 

    This works only for product category pages. I still need make it for single product page.

    This action for single product page - remove_action('woocommerce_single_product_summary') doesn't let me replace default button to my own, as the place for button is getting invisible.


  2. Please check this code

    add_filter('woocommerce_loop_add_to_cart_link', 'custom_pre_order_button', 10, 2);
    add_filter('woocommerce_single_product_summary', 'custom_pre_order_button_single', 30);
    
    function custom_pre_order_button($button, $product) {
        if ($product->is_on_backorder()) {
            $button = '<a href="#" class="button pre-order-button" onclick="openFormcraftForm()">Pre-order Now</a>';
        }
        return $button;
    }
    
    function custom_pre_order_button_single() {
        global $product;
    
        if ($product->is_on_backorder()) {
            remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30);
            echo '<a href="#" class="button pre-order-button" onclick="openFormcraftForm()">Pre-order Now</a>';
        }
    }
    
    function openFormcraftForm() {
        ?>
        <script type="text/javascript">
            function openFormcraftForm() {
                jQuery('#formcraft-form-id').trigger('click');
            }
        </script>
        <?php
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search