Automatically add array of products to basket
-
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)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Automatically add array of products to basket’ is closed to new replies.