Support » Plugin: WooCommerce » Add to cart ajax not working if user is logged in.

  • Resolved luciddigital1

    (@luciddigital1)


    When you add the products to the cart when a user is logged in nothing happens but when you are not logged in it works.

    I have deactivated all plugins and it still doesn’t work.

    I cannot deactivate the theme as the functionality needed is within the theme’s code.

    <?php
    /**
     * Adds all selected and checked product items to the basket.
     */
    function add_cb_to_cart() {
    	global $woocommerce;
    	$selectedProductArray = $_POST['selected_product_array'];
    	$checkedProductArray  = $_POST['checked_product_array'];
    	// Merge all products.
    	$productArray = array_merge( (array) $selectedProductArray, (array) $checkedProductArray );
    	// Loops over each product and add it to basket.
    	foreach ( $productArray as $row ) {
    		$row           = str_replace( '\"', '', $row );
    		$productCartId = $woocommerce->cart->generate_cart_id( $row );
    		$product = get_product( $row );
    		if ( ! $woocommerce->cart->find_product_in_cart( $productCartId ) ) {
    			// If the product is not in the cart then add it.
    			if ($product->is_type( 'grouped' )) {
    				$grouped_products = [];
    				foreach( $product->get_children() as $children_id ){
    		            $grouped_products[] = $children_id;
    		        }
    		        foreach ($grouped_products as $groupedRow) {
    		        	$woocommerce->cart->add_to_cart( $groupedRow );
    		        }
    	        } else {
    				$woocommerce->cart->add_to_cart( $row );
    			}
    		}
    	}
    
    	die();
    }
    
    add_action( 'wp_ajax_add_cb_to_cart', 'add_cb_to_cart' );
    add_action( 'wp_ajax_nopriv_add_cb_to_cart', 'add_cb_to_cart' );
     add_action('woocommerce_before_cart','QuadLayers_apply_coupon_cart_values');

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

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support gabrielfuentes

    (@gabrielfuentes)

    Hi there 👋

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    Cheers

    Plugin Support gabrielfuentes

    (@gabrielfuentes)

    We’ve not seen any activity on this thread for a while, so I’m marking this thread as resolved.

    Hopefully, you were able to find a solution to your problem! If you have further questions, please feel free to open a new topic.

    Cheers!

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.