0

I am trying to add product programatically with json format which we can used create product through API but unable to create product.

$json = '{
"sku": "samsung-a50",
"name": "Samsung A50 ",
"attribute_set_id": 4,
"price": 150000,
"status": 1,
"visibility": 1,
"type_id": "Default",
"weight": "1"
}'; 
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productObj = $objectManager->create("\Magento\Catalog\Model\ProductRepository"); 
$productData = $objectManager->create("\Magento\Catalog\Api\Data\ProductInterface");
$productData->setData($json);
$productObj->save($productData);

Can you please help to create product with ProductRepository which we used in API format

Actually i want create product with json to achieve multiple products create with single API request

3 Answers 3

1

To create a product programmatically using the ProductRepository in Magento 2, you can use the following code:

    use Magento\Catalog\Api\Data\ProductInterface;
    use Magento\Catalog\Api\ProductRepositoryInterface;
    use Magento\Framework\Api\DataObjectHelper;
    use Magento\Framework\App\State;
    use Magento\Framework\Exception\CouldNotSaveException;

    $json = '{
        "sku": "samsung-a50",
        "name": "Samsung A50",
        "attribute_set_id": 4,
        "price": 150000,
        "status": 1,
        "visibility": 1,
        "type_id": "simple",
        "weight": "1"
    }';

    // Instantiate the object manager and required dependencies
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $productRepository = $objectManager->get(ProductRepositoryInterface::class);
    $dataObjectHelper = $objectManager->get(DataObjectHelper::class);
    $appState = $objectManager->get(State::class);

    // Set the application area code to avoid Area code not set exception
    $appState->setAreaCode('frontend');

    // Create a new product object and set the data from the JSON string
    $product = $objectManager->create(ProductInterface::class);
    $dataObjectHelper->populateWithArray($product, json_decode($json, true), ProductInterface::class);

    // Save the product using the repository
    try {
        $productRepository->save($product);
        echo "Product created successfully.";
    } catch (CouldNotSaveException $e) {
        echo "Error: " . $e->getMessage();
    }

In this code, we first instantiate the ProductRepositoryInterface, DataObjectHelper, and State objects using the object manager. We also set the application area code to avoid the "Area code not set" exception.

Next, we create a new ProductInterface object and use the DataObjectHelper to populate it with the data from the JSON string. We then save the product using the ProductRepositoryInterface.

Note that in the JSON string, you should use the correct product type ID, such as simple, configurable, or virtual. Also, it is recommended to avoid using the object manager directly and instead use dependency injection for better code maintainability and testability.

0

With your code, just decode json should work :

$json = '{
"sku": "samsung-a50",
"name": "Samsung A50 ",
"attribute_set_id": 4,
"price": 150000,
"status": 1,
"visibility": 1,
"type_id": "Default",
"weight": "1"
}'; 
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productObj = $objectManager->create("\Magento\Catalog\Model\ProductRepository"); 
$productData = $objectManager->create("\Magento\Catalog\Api\Data\ProductInterface");
$productData->setData(json_decode($json, true));
$productObj->save($productData);
0

You can use Factory and Repository:

<?php

namespace Vendor\MyModule\Model;

class Product
{

    /**
     * @var \Magento\Catalog\Api\ProductRepositoryInterface
     */
    private $productRepository;

    /**
     * @var \Magento\Catalog\Api\Data\ProductInterfaceFactory
     */
    private $productFactory;

    public function __construct(
        \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
        \Magento\Catalog\Api\Data\ProductInterfaceFactory $productFactory
    ) {
        $this->productRepository = $productRepository;
        $this->productFactory = $productFactory;
    }

    public function create()
    {
        $json = '{
            "sku": "samsung-a50",
            "name": "Samsung A50",
            "attribute_set_id": 4,
            "price": 150000,
            "status": 1,
            "visibility": 1,
            "type_id": "simple",
            "weight": "1"
        }';
        
        $newProduct = $this->productFactory->create();
        $newProduct->setData(json_decode($json, true));
        $product = $this->productRepository->save($newProduct);
        return $product->getId();
    }
}

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.