1

I have been trying to add multiple products in my cart in PHP, but the cart only shows the last product added. It's like each time I try to add a new product, the previous one is removed, or a new cart is created and the previous one is removed? I don't know really. I haven't found anything on Google so I ended up here.

All the products are created correctly and I can see them in admin/commerce/products.

Thanks for anyone wanting to save me :)

Here's my code:

// Create custom product (type = ticket)
  $order_id = commerce_cart_order_id($uid);

  $sku = 'ticket_'.$order_id;

  // product exists ?
  $product = commerce_product_load_by_sku($sku);
  if ($product != false) {
    $product_id = $product->product_id;
    commerce_product_delete($product_id);
  }

  $product = commerce_product_new('ticket');
  $product->sku = $sku;
  $product->title = "Delegate Everything order for ".$event_short_name;
  $product->language = LANGUAGE_NONE;
  $product->uid = 1;
  $product->commerce_price[LANGUAGE_NONE][0] = array(
      'amount' => $total_price * 100,
      'currency_code' => "GBP",
  );
  commerce_product_save($product);

  $new_product = commerce_product_load_by_sku($sku);
  $product_id = $new_product->product_id;

  $line_item = commerce_product_line_item_new(commerce_product_load($product_id));
  commerce_cart_product_add($uid, $line_item);

  $i = 1;


  foreach($ticket_options as $ticket_option){
      $load_ticket_options = mysql_query("SELECT * FROM ticket_options WHERE ticket_option_id='$ticket_option'");
      while($load_ticket_option = mysql_fetch_array($load_ticket_options)) {
          echo $ticket_option_price = $load_ticket_option['option_price'];
          echo $ticket_option_title = $load_ticket_option['option_title'];

          $option_sku = 'option'.$i.'_'.$order_id;

          // product exists ?
          $option_product = commerce_product_load_by_sku($option_sku);
          if ($option_product != false) {
              $option_product_id = $option_product->product_id;
              commerce_product_delete($option_product_id);
          }

          $option_product = commerce_product_new('option');
          $option_product->sku = $option_sku;
          $option_product->title = "Option - ".$ticket_option_title;
          $option_product->language = LANGUAGE_NONE;
          $option_product->uid = 1;
          $option_product->commerce_price[LANGUAGE_NONE][0] = array(
              'amount' => $ticket_option_price * 100, // $10
              'currency_code' => "GBP",
          );
          commerce_product_save($option_product);

          $new_product = commerce_product_load_by_sku($option_sku);
          $option_product_id = $new_product->product_id;
          $option_line_item = commerce_product_line_item_new(commerce_product_load($option_product_id), 1, $order_id);
          commerce_cart_product_add($uid, $option_line_item);

      }
      $i++;
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.