I m trying to add configurable product programatically magento 2.0.7 this is what I did so far
I used this link which is @Kandy oferred to me:link
This code throw an error
$optionsFactory = $ObjectManager->create(Factory::class);
Error is
Fatal error: Uncaught exception 'Magento\Framework\Exception\LocalizedException' with message 'Source class "\Magento\ConfigurableProduct\Helper\Product\Options" for "Magento\ConfigurableProduct\Helper\Product\Options\Factory" generation does not exist.'
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
/* use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Catalog\Model\Product\Type;
use Magento\Catalog\Model\Product\Visibility;
use Magento\Catalog\Setup\CategorySetup;
use Magento\ConfigurableProduct\Helper\Product\Options\Factory;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
use Magento\Eav\Api\Data\AttributeOptionInterface;
use Magento\Framework\App\Bootstrap;
use Magento\ConfigurableProduct\Helper\Product\Options;
use \Magento\ConfigurableProduct\Api\Data\OptionInterfaceFactory;
include('../../../../app/bootstrap.php');*/
$bootstrap = Bootstrap::create(BP, $_SERVER);
//$bootstrap->getInstance()->reinitialize();
$ObjectManager = $bootstrap->getObjectManager();
include('Yasin-magento2-configurable_attribute.php');
$state = $ObjectManager->get(\Magento\Framework\App\State::class);
$state->setAreaCode('frontend');
/** @var ProductRepositoryInterface $productRepository */
$productRepository = $ObjectManager
->create(ProductRepositoryInterface::class);
/** @var $installer CategorySetup */
$installer = $ObjectManager->create(CategorySetup::class);
/* Create simple products per each option value*/
/** @var AttributeOptionInterface[] $options */
$options = $attribute->getOptions();
$attributeValues = [];
$attributeSetId = $installer->getAttributeSetId('catalog_product', 'Default');
$associatedProductIds = [];
$productIds = [3974,3973];
array_shift($options); //remove the first option which is empty
foreach ($options as $option) {
/** @var $product Product */
$product = $ObjectManager->create(Product::class);
$productId = array_shift($productIds);
$product->setTypeId(Type::TYPE_SIMPLE)
->setId($productId)
->setAttributeSetId($attributeSetId)
->setWebsiteIds([1])
->setFeatured(1)
->setName('Configurable Option' . $option->getLabel())
->setSku('simple_' . $productId)
->setPrice('10.00')
->setTestConfigurable($option->getValue())
->setVisibility(Visibility::VISIBILITY_NOT_VISIBLE)
->setStatus(Status::STATUS_ENABLED)
->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1]);
$product = $productRepository->save($product);
/** @var \Magento\CatalogInventory\Model\Stock\Item $stockItem */
$stockItem = $ObjectManager->create('Magento\CatalogInventory\Model\Stock\Item');
$stockItem->load($productId, 'product_id');
if (!$stockItem->getProductId()) {
$stockItem->setProductId($productId);
}
$stockItem->setUseConfigManageStock(1);
$stockItem->setQty(1000);
$stockItem->setIsQtyDecimal(0);
$stockItem->setIsInStock(1);
$stockItem->save();
$attributeValues[] = [
'label' => $option->getLabel(),
'attribute_id' => $attribute->getId(),
'value_index' => $option->getValue(),
];
$associatedProductIds[] = $product->getId();
}
/** @var $product Product */
$product = $ObjectManager->create(Product::class);
/** @var Factory $optionsFactory */
$optionsFactory = $ObjectManager->create(Factory::class);
$configurableAttributesData = [
[
'attribute_id' => $attribute->getId(),
'code' => $attribute->getAttributeCode(),
'label' => $attribute->getStoreLabel(),
'position' => '0',
'values' => $attributeValues,
],
];
$configurableOptions = $optionsFactory->create($configurableAttributesData);
$extensionConfigurableAttributes = $product->getExtensionAttributes();
$extensionConfigurableAttributes->setConfigurableProductOptions($configurableOptions);
$extensionConfigurableAttributes->setConfigurableProductLinks($associatedProductIds);
$product->setExtensionAttributes($extensionConfigurableAttributes);
// Remove any previously created product with the same id.
/** @var \Magento\Framework\Registry $registry */
$registry = $ObjectManager->get('Magento\Framework\Registry');
$registry->unregister('isSecureArea');
$registry->register('isSecureArea', true);
try {
$productToDelete = $productRepository->getById(1);
$productRepository->delete($productToDelete);
} catch (\Exception $e) {
echo "Nothing to remove";
}
$registry->unregister('isSecureArea');
$registry->register('isSecureArea', false);
$product->setTypeId(Configurable::TYPE_CODE)
->setId(1)
->setAttributeSetId($attributeSetId)
->setWebsiteIds([1])
->setName('Configurable Product')
->setSku('configurable')
->setFeatured(1)
->setVisibility(Visibility::VISIBILITY_BOTH)
->setStatus(Status::STATUS_ENABLED)
->setStockData(['use_config_manage_stock' => 1, 'is_in_stock' => 1]);
$productRepository->save($product);
Any Ideas ?
1 Answer 1
Looks like Magento\ConfigurableProduct\Helper\Product\Options\Factory available only in 2.1 version.
You can replace code
$configurableOptions = $optionsFactory->create($configurableAttributesData);
with
$configurableOptions = [];
foreach ($configurableAttributesData as $itemData) {
$configurableOptions[] = $ObjectManager->create('\Magento\ConfigurableProduct\Api\Data\OptionInterface', $itemData);
}
-
Thats new error Fatal error: Call to a member function setConfigurableProductOptions() on null in /home/rugbouti/public_html/staging/yasin/Documentation/Examples/Reader/Yasin-magento2-kandy.php on line 93, I m gonna upgrade magento 2.1 now, if its solve the problem I ll let u know.Whatatimetobealive– Whatatimetobealive2016年06月23日 21:35:40 +00:00Commented Jun 23, 2016 at 21:35
-
Replace $extensionConfigurableAttributes = $product->getExtensionAttributes(); with $extensionConfigurableAttributes = $ObjectManager->get('\Magento\Framework\Api\ExtensionAttributesFactory')->create('Magento\Catalog\Api\Data\ProductInterface');KAndy– KAndy2016年06月23日 21:40:11 +00:00Commented Jun 23, 2016 at 21:40
-
Call to undefined method Magento\Framework\Api\ExtensionAttributesFactory::create(), new error looks like best case scenario to upgrade 2.1Whatatimetobealive– Whatatimetobealive2016年06月23日 21:46:03 +00:00Commented Jun 23, 2016 at 21:46
-
I upgraded 2.1 still have an error Fatal error: Uncaught Exception\CouldNotSaveException: Unable to save product in .../Model/ProductRepository.php:550, vendor/magento/module-configurable-product/Model/Plugin/AroundProductRepositorySave.php(62), when I look AroundProductRepositorySave.php its again $saveOptions I think this is wrong can u write for me exact format of conf size attribute with sample content. $attribute->setData [ ...... ..... 'option' => [ 'value' => ['option_0' => ['Option 1'], 'option_1' => ['Option 2']], 'order' => ['option_0' => 1, 'option_1' => 2],Whatatimetobealive– Whatatimetobealive2016年06月24日 16:25:10 +00:00Commented Jun 24, 2016 at 16:25
-
I figured it out but this answer helped me a lot so I accept this, Thank for your time..Whatatimetobealive– Whatatimetobealive2016年06月24日 19:14:22 +00:00Commented Jun 24, 2016 at 19:14
$attributeis not set. Could you supply full working code? That would be helpfull (also for future refference)