• Hey there, hope you’re well..

    At first. It is not possible to share a link, cause it’s a local installation.. Hope it’s nevertheless ok to ask a question here..

    I write a plugin to add products with ajax to woo-cart. This works very well with variable products, but if I try it with simple products, they are added twice..

    This is the shortened code, normally I use sanitizing, nonce etc. All values are grabbed right, if I check them inside the response.

    This is my function for wp-ajax:

    if ( substr( $_SERVER['REQUEST_URI'], 0, 6 ) === '/cart/' )
    {
    
    	add_action( 'wp_enqueue_scripts', function(){
    		wp_enqueue_script( 'shop-cart', get_stylesheet_directory_uri().'/js/shop-cart.js', array( 'jquery' ), '1.0.0', true );
    		wp_localize_script( 'shop-cart', 'php', array(
    			'fy_nonce' => wp_create_nonce( 'fy-nonce' ),
    			'fy_spinn' => admin_url( '/images/wpspin_light.gif' ),
    			'fy_url'   => admin_url( 'admin-ajax.php' )
    			)
    		);
    	});
    
    }
    
    add_action( 'wp_ajax_fy_add_to_cart', 		 'fy_add_to_cart' );
    add_action( 'wp_ajax_nopriv_fy_add_to_cart', 'fy_add_to_cart' );
    
    function fy_add_to_cart()
    {
    
    	$product_id     =  (int) $_POST['add-to-cart'];
    	$quantity       =  (int) $_POST['quantity'];
    
    	if ( ! $variation_id )
    	{
    
    		ob_start();
    		$response = WC()->cart->add_to_cart( $product_id, $quantity );
    
    	}
    
    	echo $respond;
    
    	die();
    }

    This is my code inside the js-file:

    (function($){
    	$(function(){
    	"use strict";
    
    		$('button.single_add_to_cart_button').on('click', function(){
    
    			var data   = {
    					action: 'fy_add_to_cart',
    					values: $theGrabbedValues, // add-to-cart, quantity etc.
    					nonce:  php.fy_nonce
    				}
    
    			$.ajax( {
    				type: "post",
    				url: php.fy_url,
    				data: data,
    				success: function(r){
    					resp.addClass('success').html(r);
    				},
    				error: function(r){
    					resp.addClass('error').html(error);
    		        }
    			});
    
    			return false;
    		});
    
    	});
    }(jQuery));

    If I debug the values from $_POST inside the ajax-response, it shows me the right quantities. But if I reload the cart, everytime the product-quantity is added twice.

    3 => 6, 5=> 10 and so on..

    Can anybody tell me the reason for this and how to fix it?

    Thanks a lot and best regards..

    https://www.ads-software.com/plugins/woocommerce/

Viewing 13 replies - 1 through 13 (of 13 total)
  • Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    Hard to say without access to debug. This is all custom so I don’t see how WC would intercept your code.

    Thread Starter IndikatorDesign

    (@indikatordesign)

    Hello Mike,

    thanks for your reply.

    What exactly do you need to debug? Admin-Access and FTP?

    I will transfer it to a live domain and give you access. Debug Bar is installed, with it’s extension Kint Debuger.

    Where can I send you the credentials?

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    Sorry, I cannot debug non-core things for you – this is custom code and out of support scope. If you need a developer to look, consider jobs.wordpress.net so you can give them access.

    Thread Starter IndikatorDesign

    (@indikatordesign)

    Ok. I understand..

    Perhaps you can show me the right direction?

    Just Twennty-Sixteen and Woocommerce is active. It seems that

    WC()->cart->add_to_cart( $product_id, $quantity );

    is fired twice, without a $variation_id..

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    Check your console and see if the ajax events are firing twice.

    Thread Starter IndikatorDesign

    (@indikatordesign)

    No. The ajax-event just firing on time:

    jquery.js?ver=1.12.3:4 XHR finished loading: POST “https://domain.com/wp-admin/admin-ajax.php”.

    But if I refresh the card, this line firing on cart and also on the single-product page:

    jquery.js?ver=1.12.3:4 XHR finished loading: POST “https://domain.com/shop/testprodukt/?wc-ajax=get_refreshed_fragments”.

    Thread Starter IndikatorDesign

    (@indikatordesign)

    There are also no error-messages for empty css backgrounds or anything like this.

    PHP 7.0 (mamp) without OpCache or any other caching routine.

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    get_refreshed_fragments is normal and won’t affect your script afaik. Look on the network tab at XHR requests, not just the console.

    Thread Starter IndikatorDesign

    (@indikatordesign)

    On the network tab it is the same. It fires just once but add it twice.

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    I guess you’d better start adding logging to your code then ??

    https://php.net/manual/en/function.error-log.php

    Thread Starter IndikatorDesign

    (@indikatordesign)

    Hey Mike,

    that seems to be a nice idea : ) Thanks

    I know you do not debugg custom code, but I’m pretty sure you will get the same results, cause in the core I just use Woo’s own methods with TweentySixteen and just woo active..

    I wrapped it into a small plugin for you. It should work for you out of the box.

    The only solution that seems to work for simple products, is to build up a link like this:

    domain/cart/?add-to-cart=1&quantity=1

    and send it with ajax in the background.

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    Do some debugging yourself first, if you cannot work it out I’ll see if I can find some time. But I’m handling a major launch right now ??

    Thread Starter IndikatorDesign

    (@indikatordesign)

    Oh yes.. That seems to be more important : )

    I normally use this to debug ajax or post requests. Once placed inside the themes functions.php, you can create very comfortable different logs to compare them.

    // Var Dump Helper Function To Custom-File
    if ( ! function_exists( 'bb_var_dump_helperfunc' ) )
    {
        function bb_var_dump_helperfunc ( $bb_var_to_dump, $bb_output_name, $bb_file_name, $override = false )
        {
            if ( ! isset( $bb_var_to_dump ) || empty( $bb_var_to_dump ) ) return;
    
    		$separator1 = '';
    		$separator2 = '';
    		for ( $i = 0; $i <= 30; $i++ ){$separator1 .= '#';}
    		for ( $i = 0; $i <= 38; $i++ ){$separator2 .= '-';}
    		$path = get_stylesheet_directory()."/dump_logs/dump_log_".$bb_file_name.".log";
    		if ( $override )   file_put_contents( $path, '### Start'.$separator1."\r\n"."\r\n", LOCK_EX );
    		if ( ! $override ) file_put_contents( $path, '### Start'.$separator1."\r\n"."\r\n", FILE_APPEND | LOCK_EX );
    		file_put_contents( $path, var_export( $bb_output_name, true ).":"."\r\n"."\r\n", FILE_APPEND | LOCK_EX );
    		file_put_contents( $path, $separator2."\r\n"."\r\n", FILE_APPEND | LOCK_EX );
    		file_put_contents( $path, var_export( $bb_var_to_dump, true )."\r\n"."\r\n", FILE_APPEND | LOCK_EX );
    		file_put_contents( $path, '### End  '.$separator1."\r\n"."\r\n", FILE_APPEND | LOCK_EX );
    
        }
    }
Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Add simple product with ajax – twice’ is closed to new replies.