0
編程方式(手動)創建配置Magento的2產品:編程方式(手動)創建配置Magento的2產品
產品的正確生成,但我現在面臨的問題有,不產生產品配置詳細信息。
This Configuration詳細信息將有助於生成可配置產品和關聯子產品之間的關係,這些部分在編程創建產品時不起作用。
請爲此問題建議解決方案。
請找我下面的代碼:
<?php
ini_set("display_errors",1);
$magentoDirPath=dirname(dirname(__FILE__));
use \Magento\Framework\App\Bootstrap;
include($magentoDirPath.'/app/bootstrap.php'); // File Path
// add bootstrap
$bootstraps = Bootstrap::create(BP, $_SERVER);
$object_Manager = $bootstraps->getObjectManager();
$app_state = $object_Manager->get('\Magento\Framework\App\State');
$app_state->setAreaCode('frontend');
//simple product
$simple_product = $object_Manager->create('\Magento\Catalog\Model\Product');
$simple_product->setSku('test-simple-randdd');
$simple_product->setName('test name simple');
$simple_product->setAttributeSetId(4);
$simple_product->setColor(4); // value id of S size
$simple_product->setStatus(1);
$simple_product->setTypeId('simple');
$simple_product->setPrice(10);
$simple_product->setWebsiteIds(array(1));
$simple_product->setCategoryIds(array(4,5));
$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->save();
$simple_product_id = $simple_product->getId();
echo "simple product id: ".$simple_product_id."\n";
//configurable product
$configurable_product = $object_Manager->create('\Magento\Catalog\Model\Product');
$configurable_product->setSku('test-configurable');
$configurable_product->setName('test name configurable');
$configurable_product->setAttributeSetId(4);
$configurable_product->setColor(4); // value id of S size
$configurable_product->setStatus(1);
$configurable_product->setTypeId('configurable');
$configurable_product->setPrice(11);
$configurable_product->setWebsiteIds(array(1));
$configurable_product->setCategoryIds(array(4,5));
$configurable_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
)
);
$configurable_product->getTypeInstance()->setUsedProductAttributeIds(array(93),$configurable_product); //attribute ID of attribute 'size_general' in my store
$configurableAttributesData = $configurable_product->getTypeInstance()->getConfigurableAttributesAsArray($configurable_product);
$configurable_product->setCanSaveConfigurableAttributes(true);
$configurable_product->setConfigurableAttributesData($configurableAttributesData);
$configurableProductsData = array();
$configurableProductsData[$simple_product_id] = array(
'0' => array(
'label' => 'Color', //attribute label
'attribute_id' => '93', //attribute ID of attribute 'size_general' in my store
'value_index' => '4', //value of 'S' index of the attribute 'size_general'
'is_percent' => 0,
'pricing_value' => '10',
)
);
// 1 - Static ID of Product
$configurableProductsData['1'] = array(
'0' => array(
'label' => 'Color', //attribute label
'attribute_id' => '93', //attribute ID of attribute 'size_general' in my store
'value_index' => '5', //value of 'S' index of the attribute 'size_general'
'is_percent' => 0,
'pricing_value' => '10',
)
);
// 91 - Static ID of Product
$configurableProductsData['91'] = array(
'0' => array(
'label' => 'Color', //attribute label
'attribute_id' => '93', //attribute ID of attribute 'size_general' in my store
'value_index' => '6', //value of 'S' index of the attribute 'size_general'
'is_percent' => 0,
'pricing_value' => '10', ini_set("display_errors",1);
$magentoDirPath=dirname(dirname(__FILE__));
use \Magento\Framework\App\Bootstrap;
include($magentoDirPath.'/app/bootstrap.php'); // File Path
// add bootstrap
$bootstraps = Bootstrap::create(BP, $_SERVER);
$object_Manager = $bootstraps->getObjectManager();
$app_state = $object_Manager->get('\Magento\Framework\App\State');
$app_state->setAreaCode('frontend');
//simple product
$simple_product = $object_Manager->create('\Magento\Catalog\Model\Product');
$simple_product->setSku('test-simple-randdd');
$simple_product->setName('test name simple');
$simple_product->setAttributeSetId(4);
$simple_product->setColor(4); // value id of S size
$simple_product->setStatus(1);
$simple_product->setTypeId('simple');
$simple_product->setPrice(10);
$simple_product->setWebsiteIds(array(1));
$simple_product->setCategoryIds(array(4,5));
$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->save();
$simple_product_id = $simple_product->getId();
echo "simple product id: ".$simple_product_id."\n";
//configurable product
$configurable_product = $object_Manager->create('\Magento\Catalog\Model\Product');
$configurable_product->setSku('test-configurable');
$configurable_product->setName('test name configurable');
$configurable_product->setAttributeSetId(4);
$configurable_product->setColor(4); // value id of S size
$configurable_product->setStatus(1);
$configurable_product->setTypeId('configurable');
$configurable_product->setPrice(11);
$configurable_product->setWebsiteIds(array(1));
$configurable_product->setCategoryIds(array(4,5));
$configurable_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
)
);
$configurable_product->getTypeInstance()->setUsedProductAttributeIds(array(93),$configurable_product); //attribute ID of attribute 'size_general' in my store
$configurableAttributesData = $configurable_product->getTypeInstance()->getConfigurableAttributesAsArray($configurable_product);
$configurable_product->setCanSaveConfigurableAttributes(true);
$configurable_product->setConfigurableAttributesData($configurableAttributesData);
$configurableProductsData = array();
$configurableProductsData[$simple_product_id] = array(
'0' => array(
'label' => 'Color', //attribute label
'attribute_id' => '93', //attribute ID of attribute 'size_general' in my store
'value_index' => '4', //value of 'S' index of the attribute 'size_general'
'is_percent' => 0,
'pricing_value' => '10',
)
);
// 1 - Static ID of Product
$configurableProductsData['1'] = array(
'0' => array(
'label' => 'Color', //attribute label
'attribute_id' => '93', //attribute ID of attribute 'size_general' in my store
'value_index' => '5', //value of 'S' index of the attribute 'size_general'
'is_percent' => 0,
'pricing_value' => '10',
)
);
// 91 - Static ID of Product
$configurableProductsData['91'] = array(
'0' => array(
'label' => 'Color', //attribute label
'attribute_id' => '93', //attribute ID of attribute 'size_general' in my store
'value_index' => '6', //value of 'S' index of the attribute 'size_general'
'is_percent' => 0,
'pricing_value' => '10',
)
);
$configurable_product->setConfigurableProductsData($configurableProductsData);
$associatedProductIds =array($simple_product_id,'1','91');
$configurable_product->setCanSaveConfigurableAttributes(true);
$configurable_product->getTypeInstance()->setUsedProductAttributeIds(array(93), $configurable_product);
$configurable_product->setNewVariationsAttributeSetId($configurable_product->getAttributeSetId());
$configurable_product->setAssociatedProductIds($associatedProductIds);
$configurable_product->setConfigurableAttributesData($configurableAttributesData);
$configurable_product->save();
$configProductId = $configurable_product->getId();
echo "Product id: ".$configurable_product->getId()."\n";
exit;
)
);
$configurable_product->setConfigurableProductsData($configurableProductsData);
$associatedProductIds =array($simple_product_id,'1','91');
$configurable_product->setCanSaveConfigurableAttributes(true);
$configurable_product->getTypeInstance()->setUsedProductAttributeIds(array(93), $configurable_product);
$configurable_product->setNewVariationsAttributeSetId($configurable_product->getAttributeSetId());
$configurable_product->setAssociatedProductIds($associatedProductIds);
$configurable_product->setConfigurableAttributesData($configurableAttributesData);
$configurable_product->save();
$configProductId = $configurable_product->getId();
echo "Product id: ".$configurable_product->getId()."\n";
exit;
?>