I have looked extensively. I have found many examples for Magento 1.x but none for 2. Can someone point me in the right direction? I've got this, but it's not working:
public function savePiece($data) { $data['piece'] = unserialize($data['piece']); $this->_logger->debug('SavePiece data received: '. print_r($data, true)); $website = $this->websiteRepo->get($data['storeCode']); $store = $this->storeRepo->get($data['storeCode']); $group = $this->groupRepo->get($store->getStoreGroupId()); $category = $this->categoryRepo->get($group->getRootCategoryId(), $store->getId()); $productFactory = new ProductFactory($this->objectManager); $product = $productFactory->create(); $product->setSku($data['piece']['sku']); $product->setName($data['piece']['name']); $product->setPrice($data['piece']['al_original_price']); $product->setStoreId($store->getId()); $product->setWebsiteIds(array($website->getId())); $product->setTypeId('simple'); $product->setAttributeSetId(4); $product->setStatus(1); $product->setCategoryIds(array($category->getId())); $product->setCreatedAt(strtotime('now')); $product->setTaxClassId(2); $product->setDescription('Description'); $product = $this->productRepo->save($product); }
When I test this with CURL I get "Unable to save product." If instead I try $product->save(), I get this error:
Undefined offset: 1 in /usr/local/zend/var/apps/http/magento-dev.artsitesonline.com/80/_docroot_/vendor/magento/module-catalog-url-rewrite/Model/ProductUrlRewriteGenerator.php on line 166' in /usr/local/zend/var/apps/http/magento-dev.artsitesonline.com/80/_docroot_/vendor/magento/framework/Webapi/ErrorProcessor.php:194 Stack trace: #0 /usr/local/zend/var/apps/http/magento-dev.artsitesonline.com/80/_docroot_/vendor/magento/framework/Webapi/ErrorProcessor.php(139): Magento\Framework\Webapi\ErrorProcessor->_critical(Object(Exception)) #1 /usr/local/zend/var/apps/http/magento-dev.artsitesonline.com/80/_docroot_/vendor/magento/module-webapi/Controller/Rest.php(163): Magento\Framework\Webapi\ErrorProcessor->maskException(Object(Exception)) #2 /usr/local/zend/var/apps/http/magento-dev.artsitesonline.com/80/_docroot_/var/generation/Magento/Webapi/Controller/Rest/Interceptor.php(24): Magento\Webapi\Controller\Rest->dispatch(Object(Magento\Framework\App\Request\Http)) #3 /usr/local/zend/var/apps/http/magento-dev.artsitesonline.com/80/_docroot_/vendor/magento/framework/App/Http.php(115): Magento\Webapi\Controller\Rest\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http)) #4 /usr/local/zend/var/apps/http/magento-dev.artsitesonline.com/80/_docroot_/vendor/magento/framework/App/Bootstrap.php(258): Magento\Framework\App\Http->launch() #5 /usr/local/zend/var/apps/http/magento-dev.artsitesonline.com/80/_docroot_/index.php(39): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http)) #6 {main} [] []
Any thoughts? I also have a bunch of custom attributes I was going to use, but when I started getting errors I pared it back to just the settings I thought might be most likely to be required. I can add the others back after I get a simple example working. I appreciate everyone's help!
Best,
Dave
Just see how the core saves the products in the backend: https://github.com/magento/magento2/blob/2.0.0/app/code/Magento/Catalog/Controller/Adminhtml/Product...
check the following code snippet it worked for me to create simple products.
$simple_product = $this->objectManager->create('\Magento\Catalog\Model\Product'); $simple_product->setSku('123'); $simple_product->setName('Product Name'); $simple_product->setDescription('Description'); $simple_product->setAttributeSetId(4); $simple_product->setStatus(1); $simple_product->setTypeId('simple'); $simple_product->setPrice(0); $simple_product->setWebsiteIds(array(1)); $simple_product->setCategoryIds(array(1)); $simple_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 ) ); $simple_product->setData('manufacturer','etc'); // save attribute | attribute code, value $simple_product->setVisibility(4); // catalog and search try{ $simple_product->save(); } catch(Exception $e){ echo $e->getMessage(); }
Xubaer and Dimitry,
Thanks for the assistance. I looked at Dimitry's example and it was a bit confusing for me (I couldn't figure out where all the constructor arguments were coming from).
Xubaer's example was much more concrete and similar to what I had before. However, I just tried it and I'm getting an error. First off, I'm calling it in a custom API call. I have other custom API calls working, so I generally understand how they're supposed to be called. When I call this one with only Xubaer's code in the called function, I'm getting this error:
[2015-12-28 17:45:08] main.CRITICAL: exception 'Exception' with message 'Report ID: webapi-56817524ae0be; Message: Notice: Undefined offset: 1 in /usr/local/zend/var/apps/http/magento-dev.artsitesonline.com/80/_docroot_/vendor/magento/module-catalog-url-rewrite/Model/ProductUrlRewriteGenerator.php on line 166' in /usr/local/zend/var/apps/http/magento-dev.artsitesonline.com/80/_docroot_/vendor/magento/framework/Webapi/ErrorProcessor.php:194 Stack trace: #0 /usr/local/zend/var/apps/http/magento-dev.artsitesonline.com/80/_docroot_/vendor/magento/framework/Webapi/ErrorProcessor.php(139): Magento\Framework\Webapi\ErrorProcessor->_critical(Object(Exception)) #1 /usr/local/zend/var/apps/http/magento-dev.artsitesonline.com/80/_docroot_/vendor/magento/module-webapi/Controller/Rest.php(163): Magento\Framework\Webapi\ErrorProcessor->maskException(Object(Exception)) #2 /usr/local/zend/var/apps/http/magento-dev.artsitesonline.com/80/_docroot_/var/generation/Magento/Webapi/Controller/Rest/Interceptor.php(24): Magento\Webapi\Controller\Rest->dispatch(Object(Magento\Framework\App\Request\Http)) #3 /usr/local/zend/var/apps/http/magento-dev.artsitesonline.com/80/_docroot_/vendor/magento/framework/App/Http.php(115): Magento\Webapi\Controller\Rest\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http)) #4 /usr/local/zend/var/apps/http/magento-dev.artsitesonline.com/80/_docroot_/vendor/magento/framework/App/Bootstrap.php(258): Magento\Framework\App\Http->launch() #5 /usr/local/zend/var/apps/http/magento-dev.artsitesonline.com/80/_docroot_/index.php(39): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http)) #6 {main} [] []
If I comment out just the line where it calls $simple_product->save(), I get no error. Any thoughts?
I'm still working on this and neither of these solutions were working for me. However, I found the book Magento 2 Developers Guide and it's been very helpful. I'm now trying to create products via API with a call like this:
$testdata = json_encode(array( 'product' => array( 'sku' => 'testsku', 'name' => 'test API name', 'attribute_set_id' => 4, 'price' => 19.99, 'status' => 1, 'type_id' => 'simple', 'weight' => 1 ) )); $url = 'http://mydomain/rest/V1/products'; $authHeader = $this->oAuthClientService->buildAuthorizationHeaderForAPIRequest( 'POST', new Uri($url), unserialize($this->oAuthKeyService->findKeyByIdAndOwner('magento', 1)->getAccessToken()) ); $ch = curl_init($url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_POSTFIELDS, $testdata); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($testdata), 'Authorization: ' . $authHeader ));; $result = curl_exec($ch);
I have OAuth working in other calls, so I know that's not the problem. I'm getting this error:
Magento product creation result: exception 'PDOException' with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'catalogsearch_fulltext_scope2' doesn't exist' in /usr/local/zend/var/apps/http/magento-mydomain/80/_docroot_/vendor/magento/zendframework1/library/Zend/Db/Statement/Pdo.php:228
The first time I got this error it said catalogsearch_fulltext_scope6 instead of ...scope2. I restored the database to from a clean export I did right after first installing Magento, and now I'm getting scope2 instead of scope6.
I'm also seeing this error in my logs and I'm not sure why:
[2016-01-11 22:13:32] main.CRITICAL: exception 'Exception' with message 'Report ID: webapi-5694290c06f31; Message: Asymmetric transaction rollback.' in /usr/local/zend/var/apps/http/mydomain/80/_docroot_/vendor/magento/framework/Webapi/ErrorProcessor.php:194
I'm not familiar enough with Magento to know why it's doing this. I'm using Magento 2.0.0. Please advise. Your help is greatly appreciated.
I think I figured it out. I'm not sure why I was getting the catalogsearch_fulltext_scope6 errors, but when I reinstalled I was getting the same error but with ...scope2. After reinstalling I hadn't yet built the indexes, so apparently it had not created the index tables yet. After building the indexes for the first time, the problem table not found error about catalogsearch_fulltext_scope2 seems to have gone away.
I would love to hear what the other error is about, though ('Asymmetric transaction rollback').
Best,
Dave
How to add a configurable product to this simple product?