• Resolved nicowebmaster

    (@nicowebmaster)


    Hello,

    I want to take a particular action when adding a product to the cart, whether from the product or product.
    After clicking on “add to cart”, I have a right column that opens with the basket in a sidebar but it is necessary that the product is add and increment also in the counter next to the cart icon in my topbar. For now I can show my sidebar. But the product does not add to the basket.
    How to do ?

    My jquery code :

    jQuery(document).ready(function (){
    
    	recalculatePageHeight();
    	jQuery(window).resize(recalculatePageHeight);
    
    	function recalculatePageHeight() {
    		
    		var width_wrap = jQuery('.wrap').width();
    		var width_window = jQuery(window).width();
    		var width_total = width_window - width_wrap;
    		var margin_cart = (width_window - width_wrap) / 2;
    
    		jQuery('#cart-topbar').css('right',margin_cart);
    	};
    
    	jQuery('.single_add_to_cart_button').click(function(){
    
    	    var action = jQuery('#taille').val();
    	  
    	  	if( action === ""){
    	  		jQuery('.variations .value').append('<span id="info-option">Veuillez sélectionner une taille</span>');
    	  	}
    	  	else{
    	  		jQuery('#info-option').css('display','none');
    	  	}
    		 if (action != ""){
    		  	jQuery('body').addClass('oc-sidebar-opned');
    		  	return false;
    	  	} 
    	  	
    		jQuery('.oc-close').click(function(){
    	  		jQuery('body').removeClass('oc-sidebar-opned');
    	 	});
    
    	});
    
    	jQuery('.woocommerce-ordering select option').addClass('filter-option');
    });

    The page I need help with: [log in to see the link]

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Contributor Gerhard Potgieter

    (@kloon)

    For our own Cart Fragments functionality we use the following jQuery trigger

    $( document.body ).on( 'added_to_cart removed_from_cart'

    You should be able to use the same to have it fire at the right time.

    Thread Starter nicowebmaster

    (@nicowebmaster)

    Hello,
    I’m trying to do with hook add_action_cart.
    I can display a simple select, but I want to open this cart once the addition is done.

    Plugin Contributor Gerhard Potgieter

    (@kloon)

    There is woocommerce_add_to_cart, however, the problem with using this hook in your scenario would be that add to cart events usually have a redirect after adding to cart, so if you use the above hook to output your JS it will print before the redirect and then the page will redirect and your code will not execute, that is why I suggested you use the JS triggers instead as that will fire after the redirect as well as on Ajax requests which will also not work if you use woocommerce_add_to_cart

    Thread Starter nicowebmaster

    (@nicowebmaster)

    Infact I do not want there to be redirect. I just want my sidebar that contains the basket to open when you add it to the cart.
    Is it possible ?

    Plugin Contributor Gerhard Potgieter

    (@kloon)

    In WP-Admin under WooCommerce -> Settings -> Products make sure your add to cart settings is like this https://cld.wthms.co/kalsTI

    Then you will need to use the JS triggers I mentioned in my first post to update your mini cart.

    There is actually a function in WooCommerce called get_refreshed_fragments which basically does what you are trying to do already, search for that and try and replicate that, or just use that to get the right information.

    Thread Starter nicowebmaster

    (@nicowebmaster)

    So here I managed to do something nice is that I add the product to the basket, then the window opens, closes immediately and after the product is added. Can I simplify that?

    My code:

    jQuery('.single_add_to_cart_button').click(function(){
    
    	    var action = jQuery('#taille').val();
    	  
    	  	if( action === ""){
    	  		jQuery('.variations .value').append('<span id="info-option">Veuillez sélectionner une taille</span>');
    	  	}
    	  	else{
    	  		jQuery('#info-option').css('display','none');
      		 	jQuery('body').addClass('oc-sidebar-opned');
    	  	}
    	  	
    		jQuery('.oc-close').click(function(){
    	  		jQuery('body').removeClass('oc-sidebar-opned');
    	 	});
    
    	});

    @nicowebmaster Simplify in what way?

    Thread Starter nicowebmaster

    (@nicowebmaster)

    With this code, when I add to the basket, the sidebar with basket opens (the sidebar is hidden and arrives on the right). But it closes a few seconds later and the product is then added. I would like it to add when the basket sidebar is visible

    Thread Starter nicowebmaster

    (@nicowebmaster)

    I use plugin woo side cart with custom css

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘action after add to cart’ is closed to new replies.