0

I am creating a new product programmatically. The product is being created but it is not saving the values for length,width,height and weight.

        $newProduct = $this->productFactory->create();
        $newProduct->setName($productDetails["name"]);
        $newProduct->setSku($productDetails["sku"]);

        $sourceItemInterface = $this->sourceItemInterfaceFactory->create();

        $sourceItemInterface->setSku($productDetails['sku']);
        $sourceItemInterface->setQuantity($productDetails['qty']);
        $sourceItemInterface->setStatus(1);
        $sourceItemInterface->setSourceCode($userSource);
        $this->sourceItemsSaveInterface->execute([$sourceItemInterface]);

        $newProduct->setStockData(
                            ['use_config_manage_stock' => 0,
                            'manage_stock' => 1,
                            'is_in_stock' => 1,
                            'qty' => $productDetails["qty"]
                        ]);
        $newProduct->setAttributeSetId(4); 
        $newProduct->setStatus(1);
        $newProduct->setVisibility(4);
        $product = $newProduct->save();

        $product->setCustomAttribute('ts_dimensions_length',$productDetails["length"]);
        $product->setCustomAttribute('ts_dimensions_width',$productDetails["width"]);
        $product->setCustomAttribute('ts_dimensions_height',$productDetails["height"]);
        $product->setWeight($productDetails["weight"]);

        $product->save();
3
  • why don't you do $newProduct->setWeight($productDetails["weight"]); before $product = $newProduct->save(); Commented Jun 4, 2019 at 21:01
  • with above code, does your product created? only those information are not save? Commented Jun 5, 2019 at 6:09
  • Yes the product is created but length,width,height and weight are not saved
    – coderGeek
    Commented Jun 5, 2019 at 13:00

1 Answer 1

2

try this:

    $newProduct = $this->productFactory->create();
    $newProduct->setName($productDetails["name"]);
    $newProduct->setSku($productDetails["sku"]);

    $sourceItemInterface = $this->sourceItemInterfaceFactory->create();

    $sourceItemInterface->setSku($productDetails['sku']);
    $sourceItemInterface->setQuantity($productDetails['qty']);
    $sourceItemInterface->setStatus(1);
    $sourceItemInterface->setSourceCode($userSource);
    $this->sourceItemsSaveInterface->execute([$sourceItemInterface]);

    $newProduct->setStockData(
                        ['use_config_manage_stock' => 0,
                        'manage_stock' => 1,
                        'is_in_stock' => 1,
                        'qty' => $productDetails["qty"]
                    ]);
    $newProduct->setAttributeSetId(4); 
    $newProduct->setStatus(1);
    $newProduct->setVisibility(4);
    $newProduct->setTypeId('simple'); // type of product (simple/virtual/downloadable/configurable)
    //$newProduct->setProductHasWeight(1); // 1 = simple product, 0 = virtual product
    $newProduct->setWeight($productDetails["weight"]);

    $product = $newProduct->save();
    $product->setCustomAttribute('ts_dimensions_length',$productDetails["length"]);
    $product->setCustomAttribute('ts_dimensions_width',$productDetails["width"]);
    $product->setCustomAttribute('ts_dimensions_height',$productDetails["height"]);
    $product->save();
4
  • I had tried setting the weight before saving the product. That didn't work either.
    – coderGeek
    Commented Jun 4, 2019 at 22:39
  • did you add the line "$newProduct->setTypeId('simple');"? Commented Jun 4, 2019 at 22:54
  • Thank you. Setting the typeId did it
    – coderGeek
    Commented Jun 5, 2019 at 13:03
  • no problem, glad to help. Commented Jun 5, 2019 at 20:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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