• Resolved luciddigital1

    (@luciddigital1)


    I have a development website that has a product builder that passes an array of product I.D’s through an ajax request and then loops over this array and adds all the products to the basket. This works locally but not on the staging link for the companies website. The code is below.

    function add_cb_to_cart() {
    	$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 ) {
    		$productCartId = WC()->cart->generate_cart_id( $row );
    		if ( ! WC()->cart->find_product_in_cart( $productCartId ) ) {
    			// If the product is not in the cart then add it.
    			WC()->cart->add_to_cart( $row, 1 );
    		}
    	}
    
    	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' );
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support RK a11n

    (@riaanknoetze)

    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.

    Plugin Support Gabriel – a11n

    (@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)
  • The topic ‘Automatically add array of products to basket’ is closed to new replies.