0

I am having trouble with creating products for a commerce project with multiple sellers (each with their own store). The products these sellers are selling is a baseball card.

When a user adds a new product to sell, in the form submission I create the form as:

// create price...
$price = new Price($vals['price']['number'], $vals['price']['currency_code']);

// init variation
$variation_vals = [
  'type' => 'card_type_baseball',
  'status' => 1,
  'sku' => 'baseball-1',
  'price' => $price,
  'field_card_image' => [$vals['image_fid']]
];
$variant = ProductVariation::create($variation_vals);
$variant->save();

// init product
$product_vals = [
  'uid' => $this->getUID(),
  'title' => $vals['name'],
  'type' => 'card',
  'stores' => [$this->store],
  'variations' => [$variant],
];
$product = Product::create($product_vals);
$product->save();

It works, however, when I view the product after creation, I have many copies of image and field values for some reason.

I think I'm missing a concept.

When a user wants to sell their product, and they upload the product image, set the price, and enter the title for that card (through the form I provide via the Form API) I want to create the product that I can display in a view for the front page, or under their store on their store pages (which I can display via Views).

Every card has the same properties: an image, title, and a price. Examples,

1 - Barry Bonds, $20.00 (with img) 2 - Mark McGuire, $15.00 (with img)

Am I missing something in relation to creating products?

1
  • It's looking like it may have been a rendering issue, where I was rendering the variation entity, and within the variation, I was rendering the product, getting what I assume to be a cascading effect. I'm still unsure, however, that I have the concept down for the multi-selling system.
    – vintorg
    Jul 28 '19 at 7:01
1

The code in my example was fine. In my product variation's display I was rendering the product entity, and in my product entity I was rendering my variant. If anyone encounters multiple copies of their product rendered, check how the fields are rendered in the display.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.