Support » Plugin: Woocommerce Upsell Popup » When will click on my custom button then display popup instead of “add to cart”

  • Resolved vasaragova

    (@vasaragova)


    Hello

    I need to popup display on click on a custom button instead of add to cart button into a single product page

    How is possible? help me

    If any way to display a popup without a click on add to cart button? because of add to cart button display slide panel of the cart items

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author 10Horizons

    (@tentenbiz)

    If I understand you correctly, you want the popup to show when user clicks on a custom button on a single product page, is that right? Meaning you don’t want the product to be added to cart?

    If that’s what you want, drop this code to your theme’s functions.php file.

    add_action( 'init', 'remove_popup_trigger' );
    function remove_popup_trigger(){
    	remove_action( 'woocommerce_add_to_cart', 'thp_upsell_popup_trigger' );
    }
    
    add_action( 'woocommerce_after_add_to_cart_form', 'my_custom_button' );
    function my_custom_button() {
    	global $product;
    	$id = $product->get_id();
    	
    	//here is your custom button
    	echo '<form id="your-id" method=post>';
    	echo '<input name="wc-upsell-popup-custom-btn" type="submit" value="Show upsell popup!">';
    	echo '<input name="wc-upsell-popup-product-id" type="hidden" value="'.$id.'">';
    	echo '</form>';
    	
    }
    
    add_action( 'wp_loaded', 'fire_popup');
    function fire_popup() {
    	if (isset($_POST['wc-upsell-popup-custom-btn'])) {
    		$prod_id = filter_input( INPUT_POST, 'wc-upsell-popup-product-id', FILTER_SANITIZE_NUMBER_INT );
    		thp_fire_php_include($prod_id);
    	}
    }

    Change the code accordingly to suit your needs.

    Plugin Author 10Horizons

    (@tentenbiz)

    In case you also want the current product to be added to cart, use the following code instead.

    It’s basically the same code as above except that when user clicks the custom button, current product will be added to cart and then the popup will show.

    Here’s the code:

    add_action( 'init', 'remove_popup_trigger' );
    function remove_popup_trigger(){
    	remove_action( 'woocommerce_add_to_cart', 'thp_upsell_popup_trigger' );
    }
    
    add_action( 'woocommerce_after_add_to_cart_form', 'my_custom_button' );
    function my_custom_button() {
    	global $product;
    	$id = $product->get_id();
    	
    	//here is your custom button
    	echo '<form id="your-id" method=post>';
    	echo '<input name="wc-upsell-popup-custom-btn" type="submit" value="Show upsell popup!">';
    	echo '<input name="wc-upsell-popup-product-id" type="hidden" value="'.$id.'">';
    	echo '</form>';
    	
    }
    
    add_action( 'wp_loaded', 'fire_popup');
    function fire_popup() {
    	if (isset($_POST['wc-upsell-popup-custom-btn'])) {
    		$prod_id = filter_input( INPUT_POST, 'wc-upsell-popup-product-id', FILTER_SANITIZE_NUMBER_INT );
    		WC()->cart->add_to_cart($prod_id); //this line of code adds the current product to cart
    		thp_fire_php_include($prod_id);
    	}
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.