<p>You probably have it figured by now, but for anyone wondering, here's a complete working example.</p>

<pre><code>use craft\commerce\elements\Product;
use craft\commerce\elements\Variant;

// Create product
$product = new Product();
$product-&gt;title = 'Hello world';
$product-&gt;typeId = 2;
$product-&gt;enabled = true;

// Create variant
$variant = new Variant();
$variant-&gt;title = 'hey';
$variant-&gt;sku = 'hey';
$variant-&gt;price = 123;
$variant-&gt;stock = 5;

// Save product with variant
$product-&gt;setVariants([$variant]);
Craft::$app-&gt;elements-&gt;saveElement($product);
</code></pre>

CC BY-SA 4.0
2022-03-31T19:53:10.157Z
hey thank you. this helped me a lot and was working great until a recent update. any idea what has changed? i think my product is no longer validating for some (new) reason.
Steve Mock
CC BY-SA 4.0
2019-03-06T14:56:54.970Z
2019-03-06T14:56:54.970Z
Stian Karlsen
<ol>
<li>Populate a product model (<code>$product</code>)</li>
<li>Populate an array of variant models (<code>$variants</code>)</li>
<li><code>$product-&gt;setVariants($variants);</code></li>
<li><code>Craft::$app-&gt;getElements()-&gt;saveElement($product);</code></li>
</ol>

<p>If step 4 returns false, validation errors occurred on the variants or product, and the errors be on the <code>$product</code>.</p>

CC BY-SA 4.0
2018-10-29T08:00:04.797Z
Thanks for your help. I'm getting the error `Call to a member function getProductTypes() on null` when I try to run my code. I've updated my question to add the code I'm trying.
themartin
CC BY-SA 4.0
2018-10-24T05:37:48.513Z
2018-10-24T05:37:48.513Z
Luke Holder
<p>The <a href="https://craftcommerce.com/docs/purchasables" rel="nofollow noreferrer">docs for v1</a> mention a method <code>craft()-&gt;commerce_purchasables-&gt;saveElement()</code>.</p>

<p>This is omitted in the <a href="https://github.com/craftcms/commerce-docs" rel="nofollow noreferrer">docs for v2</a>. Does anyone know how I can create a product programmatically in PHP?</p>

<pre><code>    $product = new Product();
        $product-&gt;title = "Test Product 1";
        $product-&gt;typeId = 2;
        $product-&gt;enabled = true;
        $product-&gt;promotable = true;

    $variant = new Variant();
            $variant-&gt;isDefault = true;

    $product-&gt;setVariants([$variant]);

    Craft::$app-&gt;getElements()-&gt;saveElement($product);
</code></pre>

<p>With the above code I get the following error:</p>

<pre><code>Call to a member function getProductTypes() on null
</code></pre>

<p>I know the typeId is correct, because I can output it on a product template with <code>{{ product.typeId }}</code></p>

<craft3><plugin-craftcommerce2>
Programmatically create a product using Craft Commerce 2?
# Programmatically create a product using Craft Commerce 2?

(Asked by: username_0 on 2018-10-24)

The docs for v1 mention a method craft()->commerce_purchasables->saveElement().
This is omitted in the docs for v2. Does anyone know how I can create a product programmatically in PHP?
    $product = new Product();
        $product->title = "Test Product 1";
        $product->typeId = 2;
        $product->enabled = true;
        $product->promotable = true;

    $variant = new Variant();
            $variant->isDefault = true;

    $product->setVariants([$variant]);

    Craft::$app->getElements()->saveElement($product);

With the above code I get the following error:
Call to a member function getProductTypes() on null

I know the typeId is correct, because I can output it on a product template with {{ product.typeId }}

**Tags:** craft3, plugin-craftcommerce2

---

## 2 answers

### Answer by username_1 on 2019-03-06. Score: 3

You probably have it figured by now, but for anyone wondering, here's a complete working example.
use craft\commerce\elements\Product;
use craft\commerce\elements\Variant;

// Create product
$product = new Product();
$product->title = 'Hello world';
$product->typeId = 2;
$product->enabled = true;

// Create variant
$variant = new Variant();
$variant->title = 'hey';
$variant->sku = 'hey';
$variant->price = 123;
$variant->stock = 5;

// Save product with variant
$product->setVariants([$variant]);
Craft::$app->elements->saveElement($product);

> **Comments:**
> **username_2**: hey thank you. this helped me a lot and was working great until a recent update. any idea what has changed? i think my product is no longer validating for some (new) reason.  

---

### Answer by username_3 on 2018-10-24. Score: 1

1. Populate a product model (`$product`)
2. Populate an array of variant models (`$variants`)
3. `$product->setVariants($variants);`
4. `Craft::$app->getElements()->saveElement($product);`

If step 4 returns false, validation errors occurred on the variants or product, and the errors be on the `$product`.

> **Comments:**
> **username_0**: Thanks for your help. I'm getting the error `Call to a member function getProductTypes() on null` when I try to run my code. I've updated my question to add the code I'm trying.  

---

