Skip to main content
added 216 characters in body
Source Link

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 }}

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);

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 }}

added 410 characters in body
Source Link

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);

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?

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);
Source Link

Programmatically create a product using Craft Commerce 2?

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?