How to create product and product variations in Drupal 8 for Drupal Commerce

Posted by admin on Wed, 01/17/2018 - 10:18

Code snippets for creating Drupal 8 products and product variations for Drupal Commerce.

Create a product variation first:

// The price of the variation.
$price = new \Drupal\commerce_price\Price('24.99', 'USD');
 
$variation = \Drupal\commerce_product\Entity\ProductVariation::create([
  'type' => 'default', // The default variation type is 'default'.
  'sku' => 'test-product-01', // The variation sku.
  'status' => 1, // The product status. 0 for disabled, 1 for enabled.
  'price' => $price,
]);
$variation->save();

First load your store. If you haven't that - create it!

$store = \Drupal\commerce_store\Entity\Store::load(1);

Create a product attached to that store with that variation created above:

$product = \Drupal\commerce_product\Entity\Product::create([
  'uid' => 1, // The user id that created the product
  'type' => 'default', // Just like variation, the default product type is 'default'
  'title' => 'My product',
  'stores' => [$store], // The store we loaded above
  'variations' => [$variation], // The variation we created above
]);
$product->save();

You can also add a variation to a product using the addVariation() method.

$product->addVariation($variation);
$product->save();
×

Hey! Don't miss a new cool stuff!

Comments

shawnkemp (not verified)

Thanks a lot for the kind of perfect topic I have not a lot of information about it but I have got an extra unique info in your unique post.Perfect description you have provided thanks for sharing.https://www.topcelebrityjackets.com/rocky-balboa-4-sylvester-stallone-ja...

davidjoe (not verified)

Very informative and rich in content article. It is very important to taKe care of the health. I also would like to recommend this high quality custom https://www.essaycloud.co.uk/write-my-essay writing service where all students can get premium help with writing effective academic assignments.

Anonymous (not verified)
asd (not verified)

Thanks a lot for sharing us about this update. karamba Hope you will not get tired on making posts as informative as this.

carl (not verified)

This is the code that I have been looking for. This code will help me solve the college-paper review mystery and that is the only thing that's bothering me right now. I am extremely pleased that the writer shared this code with us here.

aftabak (not verified)

Here are a great deal of sites made just for composing audits on the assortment of gambling clubs. hopa

asd (not verified)

A debt of gratitude is in order for posting this information. I simply need to tell you that I simply look at your site and I discover it exceptionally fascinating and educational. I can hardly wait to peruse bunches of your posts. iq option
a

emily seo (not verified)

you may get sets of poker chips that incorporate 100 chips, 200 chips, et cetera, as far as possible up to 1000 chips. storspelare bonus

sudoku (not verified)

If you want to play free sudoku online with fun so follow our link and play free of cost.

aftab ali (not verified)

I trust you have delighted in perusing these football wagering tips and your excitement for football lay wagering has been fuelled. 1xbet

Anonymous (not verified)

the most straightforward to bet on. Instead of ball and football, where you need to manage point spreads and other confounding stuff. nightrush

Add new comment