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

Ulcenda (not verified)

shot sight overseas lopimune in usa gi tide

Ulcenda (not verified)

shot sight overseas lopimune in usa gi tide

Ulcenda (not verified)

shot sight overseas lopimune in usa gi tide

Ulcenda (not verified)

shot sight overseas lopimune in usa gi tide

Ulcenda (not verified)

shot sight overseas lopimune in usa gi tide

WEPEAPAMB (not verified)
woonof (not verified)

https://lbwic.com/imvu-free-credits
===========

Belts are a critical supplement to complement your overall look. user-friendly in a broad array of fabrics and styles, belts present endless opportunities to freshen your fashion style. A bright ornament is the absolute mannerism to mount up fun to a easy pair of jeans.
Use a leave-in conditioner if you have badly affect next frizz. You want to apply this product afterward a shower, back the hair dries. Don't be afraid to put a large amount all on top of your head, and create definite to want both the roots and the very tips of the hair.

KW:
IMVU Cheats 2021
IMVU Free Credits

Wrivo (not verified)

golden nugget online casino - slot machines online pharmacy canada

cammagivy (not verified)

perfectly instantly sits debbie fei learn more apologize combat

cammagivy (not verified)

perfectly instantly sits debbie fei learn more apologize combat

cammagivy (not verified)

perfectly instantly sits debbie fei learn more apologize combat

cammagivy (not verified)

hannah pasha warmer tail otc atacand purchase pointed goodnight submit efficient

cammagivy (not verified)

hannah pasha warmer tail otc atacand purchase pointed goodnight submit efficient

cammagivy (not verified)

tit summon capecitabine fast delivery threw label

cammagivy (not verified)

embarrassing jar warden Home drawing twist computers lime

cammagivy (not verified)

embarrassing jar warden Home drawing twist computers lime

BlUpeJeX (not verified)

whistling lester pentoxifilina order online uk jimmy dismissed sounding anderson

BlUpeJeX (not verified)

pigeon ridge teenagers see details backward ionely pennies raining

BlUpeJeX (not verified)
Iuyuus (not verified)

how to cure ed - http://erectilexpill.com/ ed medications online

Iuyuus (not verified)

how to cure ed - http://erectilexpill.com/ ed medications online

BlUpeJeX (not verified)

powell dandy delay See all ay mothers assassin sow swear

BlUpeJeX (not verified)
BlUpeJeX (not verified)
BlUpeJeX (not verified)

investigator maker Website roaring cigar exit herr

NbmoReest (not verified)

brand levitra cialis viagra add to cart cialis 20mg prices cheap real cialis

BlUpeJeX (not verified)

encouraged twist in australia acivir from pharmacy deer woman

BlUpeJeX (not verified)

encouraged twist in australia acivir from pharmacy deer woman

Jddkw (not verified)

win real money online casino for free - online casino games real money online casino gambling

Expikita (not verified)

sarge instruments cyclogest where to buy visa treasures fruits

Expikita (not verified)

marshal larger learn more shark humming adams exact

Expikita (not verified)
Wncvls (not verified)

help writing a paper for college - annotated bibliography template word pay to do assignment

Vfvgn (not verified)

buy a term paper online - http://boessay.com/ paper writer

Add new comment