0

I have created a custom product for which I am able programmatically create products.

The problem is that i am unable to generate a price for these products. the field is left blank when the product is created.

This is how I create the product:

$_product = $this->productFactory->create();


            $_product->setName($array['ProductName']);
            $_product->setTypeId('Dome');
            $_product->setAttributeSetId(4);
            $_product->setSku('test-SKU');
            $_product->setWebsiteIds(array(1));
            $_product->setVisibility(4);
            $_product->setPrice(array(350));
            $_product->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
            $_product->setStockData(array(
                    'use_config_manage_stock' => 0, //'Use config settings' checkbox
                    'manage_stock' => 1, //manage stock
                    'min_sale_qty' => 1, //Minimum Qty Allowed in Shopping Cart
                    'max_sale_qty' => 2, //Maximum Qty Allowed in Shopping Cart
                    'is_in_stock' => 1, //Stock Availability
                    'qty' => 100 //qty
                    )
                );

            $product = $this->productRepository->save($_product);


            $id = $product->getId();

            return $id;

I have also tried this to no avail:

$_product->setPrice(350);

This is how the custom product was created:

product_types.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Catalog:etc/product_types.xsd">
    <type name="Dome" label="Event Dome Product" modelInstance="Mymodule\Example\Model\Product\Type\EventDome"
          composite="false" isQty="true" canUseQtyDecimals="false" sortOrder="83">
        <priceModel instance="Mymodule\Example\Model\Product\Type\Price"/>
        <customAttributes>
            <attribute name="refundable" value="true"/>
            <attribute name="is_real_product" value="false"/>
            <attribute name="taxable" value="true"/>
        </customAttributes>
    </type>
</config>
0

Please try with below code to add price like :

$_product->setPrice(350.00)

or try with below example :

$storeId = '1'; //Store ID
$product_id = 7878;
$price = "350.01";
$productFactory = $objectManager->get('\Magento\Catalog\Model\ProductFactory');
$product = $productFactory->create()->setStoreId($storeId)->load($product_id);
$product->setPrice($price);
$product->save();

hope its working for you

  • hi. Thanks for replying. but it did not work – theSeeker Sep 9 '19 at 9:35
  • try with store id. or put the value - 350.01 – Anas Mansuri Sep 9 '19 at 10:19
  • how do you mean with store Id? – theSeeker Sep 9 '19 at 10:20
  • i update my answer with example please check. – Anas Mansuri Sep 9 '19 at 10:31
  • $_product->setStoreId(1); add this line in your code. – Anas Mansuri Sep 9 '19 at 10:32
0

After this script:

        $_product = $this->productFactory->create();
        $_product->setName($array['ProductName']);
        $_product->setTypeId('Dome');
        $_product->setAttributeSetId(4);
        $_product->setSku('test-SKU');
        $_product->setWebsiteIds(array(1));
        $_product->setVisibility(4);
        $_product->setPrice(array(350));
        $_product->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
        $_product->setStockData(array(
                'use_config_manage_stock' => 0, //'Use config settings' checkbox
                'manage_stock' => 1, //manage stock
                'min_sale_qty' => 1, //Minimum Qty Allowed in Shopping Cart
                'max_sale_qty' => 2, //Maximum Qty Allowed in Shopping Cart
                'is_in_stock' => 1, //Stock Availability
                'qty' => 100 //qty
                )
            );

        $product = $this->productRepository->save($_product);


        $id = $product->getId();

** Try This **

//Now Load product model and save price

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();<br/>
$productObj = $objectManager->create('Magento\Catalog\Model\Product')->load($id);<br/>
$productObj->setPrice(100)->save();
  • hi. i tried it. it does not work. – theSeeker Sep 9 '19 at 14:00
  • i think i know what is wrong. i did not put price within my attribue set for productsetup-installData. do you know how i can remove the previous attribute set and install a new one – theSeeker Sep 9 '19 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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