Support » Plugin: Woo Ajax Add to Cart product page » Session storage not updated after product is added to cart

  • I have used this plugin and just noticed an issue which prevents fair execution of “wc_fragments_refreshed” event.

    Basically in your wc-ajax-product-add-to-cart-public.js you have this code:

    if ( typeof $supports_html5_storage !== 'undefined' && $supports_html5_storage && typeof wc_cart_fragments_params !== 'undefined' && wc_cart_fragments_params ) {
        sessionStorage.setItem( wc_cart_fragments_params.fragment_name, JSON.stringify( data.fragments ) );
        set_cart_hash( data.cart_hash );
    
        if ( data.cart_hash ) {
            set_cart_creation_timestamp();
        }
    }

    However $supports_html5_storage, set_cart_hash, set_cart_creation_timestamp are not defined elsewhere in the file.

    My suggested fix is wrapping the entire event with DOMDocumentLoaded event and copy these missing variables, functions from original WooCommerce files:

    /* global wc_prod_ajax_to_cart */
    (function( $ ) {
    	'use strict';
    
    	$( document.body ).ready( function() {
    
    		/* Storage Handling */
    		var $supports_html5_storage = true,
    			cart_hash_key           = wc_cart_fragments_params.cart_hash_key;
    
    		try {
    			$supports_html5_storage = ( 'sessionStorage' in window && window.sessionStorage !== null );
    			window.sessionStorage.setItem( 'wc', 'test' );
    			window.sessionStorage.removeItem( 'wc' );
    			window.localStorage.setItem( 'wc', 'test' );
    			window.localStorage.removeItem( 'wc' );
    		} catch( err ) {
    			$supports_html5_storage = false;
    		}
    
    		/* Cart session creation time to base expiration on */
    		function set_cart_creation_timestamp() {
    			if ( $supports_html5_storage ) {
    				sessionStorage.setItem( 'wc_cart_created', ( new Date() ).getTime() );
    			}
    		}
    
    		/** Set the cart hash in both session and local storage */
    		function set_cart_hash( cart_hash ) {
    			if ( $supports_html5_storage ) {
    				localStorage.setItem( cart_hash_key, cart_hash );
    				sessionStorage.setItem( cart_hash_key, cart_hash );
    			}
    		}
    
    		/**
    		 * All of the code for your public-facing JavaScript source
    		 * should reside in this file.
    		 *
    		 * Note: It has been assumed you will write jQuery code here, so the
    		 * $ function reference has been prepared for usage within the scope
    		 * of this function.
    		 *
    		 * This enables you to define handlers, for when the DOM is ready:
    		 *
    		 * $(function() {
    		 *
    		 * });
    		 *
    		 * When the window is loaded:
    		 *
    		 * $( window ).load(function() {
    		 *
    		 * });
    		 *
    		 * ...and/or other possibilities.
    		 *
    		 * Ideally, it is not considered best practise to attach more than a
    		 * single DOM-ready or window-load handler for a particular page.
    		 * Although scripts in the WordPress core, Plugins and Themes may be
    		 * practising this, we should strive to set a better example in our own work.
    		 */
    		$(document).on('submit', 'body.single-product form.cart', function(e){
    			e.preventDefault();
    			var $this = $(this);
    			$( document.body ).trigger( 'adding_to_cart' );
    			if( wc_prod_ajax_to_cart.enable_blockui === '1' ) {
    				$this.block({
    					message: null,
    					overlayCSS: {
    						background: '#fff',
    						opacity: 0.6
    					}
    				});
    			}
    			$.ajax({
    				method: 'POST',
    				url: wc_prod_ajax_to_cart.ajax_url,
    				data: $this.serialize(),
    				success: function( data ) {
    
    					if ( data && data.fragments ) {
    						$.each( data.fragments, function( key, value ) {
    							$( key ).replaceWith( value );
    						});
    
    						if( wc_prod_ajax_to_cart.enable_blockui === '1' ) {
    							$this.unblock();
    						}
    
    						if ( typeof $supports_html5_storage !== 'undefined' && $supports_html5_storage && typeof wc_cart_fragments_params !== 'undefined' && wc_cart_fragments_params ) {
    							sessionStorage.setItem( wc_cart_fragments_params.fragment_name, JSON.stringify( data.fragments ) );
    							set_cart_hash( data.cart_hash );
    
    							if ( data.cart_hash ) {
    								set_cart_creation_timestamp();
    							}
    						}
    
    						$( document.body ).trigger( 'wc_fragments_refreshed' );
    					}
    				},
    				error: function( response ) {
    					$( document.body ).trigger( 'added_to_cart' );
    					if( wc_prod_ajax_to_cart.enable_blockui === '1' ) {
    						$this.unblock();
    					}
    				},
    			});
    		});
    	} );
    
    })( jQuery );

    Link to file:
    https://d.pr/f/tiwwAn

    I hope you can fix this issue.

Viewing 1 replies (of 1 total)
  • Plugin Author m1tk00

    (@m1tk00)

    Hey Arlind,

    Thanks for the patch. Will review it and update it soon.

    Mitko

Viewing 1 replies (of 1 total)
  • The topic ‘Session storage not updated after product is added to cart’ is closed to new replies.