Viewing 10 replies - 1 through 10 (of 10 total)
  • Same you, i can’t add the product to cart on IOS
    This is my sample page :
    https://olaza.vn/san-pham/ao-thun-nam-ngan-tay/

    On desktop and Android device it ok, only ios device, can’t add to cart or buy now

    Plugin Contributor Mike Jolley

    (@mikejolley)

    I can see in the source some caching plugin is on. Cart page has to be excluded from caches.

    Thread Starter naresh11381

    (@naresh11381)

    Actually it seems to be not working in chrome now. Firefox Safari and iOS just add one item to the cart and then it stays one regardless how many items are added and it is empty when going to checkout.

    This is the code from functions

    //Pass to front-end URL to admin-ajax handler
    	function js_variables(){
    		$variables = array (
    			'ajax_url' => admin_url('admin-ajax.php'),
    		);
    		echo('<script type="text/javascript">window.wp_data = '.json_encode($variables).'</script>');
    	}
    	add_action('wp_head','js_variables');
    
    	//Recieved ajax data handler
    	function ajax_cart_callback(){
    		global $woocommerce;
    		$result = $woocommerce->cart->add_to_cart($_POST['id'], $_POST['qty']);
    		$count = $woocommerce->cart->get_cart_contents_count();
    		$post_name = get_post($_POST['id']);
    		if($result){
    			echo json_encode( array('status'=>1, 'count'=>$count, 'post_name'=> strip_tags($post_name->post_title) ) );
    		} else {
    			echo json_encode( array('status'=>0) );
    		}
    		wp_die();
    	 }	 
    
    	// if both logged in and not logged in users can send this AJAX request,
    	// add both of these actions, otherwise add only the appropriate one
    	add_action( 'wp_ajax_nopriv_ajax_cart', 'ajax_cart_callback' );
    	add_action( 'wp_ajax_ajax_cart', 'ajax_cart_callback' );

    Any ideas/tips would be great.
    Thanks

    Plugin Contributor Mike Jolley

    (@mikejolley)

    As I said, caching rules need to be configured. Cart, checkout need to be excluded.

    Thread Starter naresh11381

    (@naresh11381)

    I think we posted at the same time. I don’t have any caching at all.
    https://dcccdev.com/product/cobalt-s-lever-single-pack/

    Plugin Contributor Mike Jolley

    (@mikejolley)

    Does your host have varnish cache? What host?

    Thread Starter naresh11381

    (@naresh11381)

    It’s just on some cheap shared hosting while in development, just a basic setup. Last night I moved a version it to the production server which is Serverpilot > PHP7 PHP-FPM Nginx and it still does the same thing, it doesn’t seem to be a server configuration issue as the Serverpilot server is setup for wordpress (this server is only accessible if you change your hosts file as the domain resolves elsewhere ).

    It’s strange that only chrome will run it, if I run chrome in incognito it stops working. Does the code above look ok?

    Thread Starter naresh11381

    (@naresh11381)

    It turned out it would work for logged in users but not for any guests.
    The session cookie wasn’t being set.
    This is the updated code that is now working including session->set_customer_session_cookie

    //Recieved ajax data handler
    	function ajax_cart_callback(){
    		global $woocommerce;
    		WC()->session->set_customer_session_cookie(true);
    		$result = $woocommerce->cart->add_to_cart($_POST['id'], $_POST['qty']);
    		$count = $woocommerce->cart->get_cart_contents_count();
    		$post_name = get_post($_POST['id']);
    		if($result){
    			echo json_encode( array('status'=>1, 'count'=>$count, 'result'=>$result, 'post_name'=> strip_tags($post_name->post_title) ) );
    		} else {
    			echo json_encode( array('status'=>0) );
    		}
    		wp_die();
    	 }
    Plugin Contributor Mike Jolley

    (@mikejolley)

    So this was related to your custom ajax code and not core?

    Thread Starter naresh11381

    (@naresh11381)

    Correct, my custom code was missing WC()->session->set_customer_session_cookie(true); which is why any guest session wasn’t being added to the cart. Only logged in sessions.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Ajax add to cart in iOS not working’ is closed to new replies.