Magento 2.2.5,如何使用简单的产品创建可配置产品

时间:2018-07-31 10:13:28

标签: magento product configurable

我想以编程方式创建一个与之关联的简单产品的可配置产品。 脚本执行没有错误,但是我只能看到创建的可配置产品没有关联,而创建的简单产品却没有任何链接。 有什么建议吗?这是我的代码:

    <?php
    use \Magento\Framework\App\Bootstrap;
    include('../app/bootstrap.php');
    $bootstrap = Bootstrap::create(BP, $_SERVER);
    $objectManager = $bootstrap->getObjectManager();
    $url = \Magento\Framework\App\ObjectManager::getInstance();
    $storeManager = $url->get('\Magento\Store\Model\StoreManagerInterface');
    $mediaurl= $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
    $state = $objectManager->get('\Magento\Framework\App\State');
    $state->setAreaCode('frontend');

    $simple_product = $objectManager->create('\Magento\Catalog\Model\Product');
    $simple_product->setSku('test-simple-32');
    $simple_product->setName('test name simple 32');
    $simple_product->setAttributeSetId(4);
    $simple_product->setColor(10);
    //$simple_product->setSize_general(10); // 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(31));
    $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 = $objectManager->create('\Magento\Catalog\Model\Product');
    $configurable_product->setSku('test-configurable-26');
    $configurable_product->setName('test name configurable 26');
    $configurable_product->setAttributeSetId(93);   
    $configurable_product->setStatus(1);
    $configurable_product->setTypeId('configurable');
    $configurable_product->setPrice(11);
    $configurable_product->setWebsiteIds(array(1));
    $configurable_product->setCategoryIds(array(31));
    $configurable_product->setStockData(array(
        'use_config_manage_stock' => 0, //'Use config settings' checkbox
        'manage_stock' => 1, //manage stock
        'is_in_stock' => 1, //Stock Availability
        )
    );

    $configurable_product->save();
    $configProductId = $configurable_product->getId();
    $attributeSetId = 93;


    $associatedProductIds=array($simple_product_id);
    $optionsFactory = $objectManager->create('\Magento\ConfigurableProduct\Helper\Product\Options\Factory');

    $configurableOptions = $optionsFactory->create($configurableProductsData);
    $extensionConfigurableAttributes = $configurable_product->getExtensionAttributes();
    $extensionConfigurableAttributes->setConfigurableProductOptions($configurableOptions);
    $extensionConfigurableAttributes->setConfigurableProductLinks($associatedProductIds);
    $configurable_product->setExtensionAttributes($extensionConfigurableAttributes);    


    $associatedProductIds=array($simple_product_id);
    $configurable_product->setAssociatedProductIds($associatedProductIds);
    $configurable_product->setConfigurableProductsData($configurableProductsData); 
    $configurable_product->save();
    */


    //$associatedProductIds = array($simplProductId1,$simplProductId2,$simplProductId3,$simplProductId4);//Simple Product ids array
    $associatedProductIds = array($simple_product_id);
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $product = $objectManager->create('Magento\Catalog\Model\Product')->load($configProductId); // Load Configurable Product
    $attributeModel = $objectManager->create('Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute');
    $position = 0;
    $attributes = array(10);
    //$attributes = array($attributeColorId, $attributeSizeId); // Super Attribute Ids Used To Create Configurable Product(list of supper attribute ids what ever belong to that the attribute set under which the configurable product is)
    foreach ($attributes as $attributeId) {
        $data = array('attribute_id' => $attributeId, 'product_id' => $configProductId, 'position' => $position);
        $position++;
        $attributeModel->setData($data);//->save();
    }
    $product->setTypeId("configurable");
    $product->setAffectConfigurableProductAttributes($attributeSetId);
    $objectManager->create('Magento\ConfigurableProduct\Model\Product\Type\Configurable')->setUsedProductAttributeIds($attributes, $product);
    $product->setNewVariationsAttributeSetId($attributeSetId);
    $product->setAssociatedProductIds($associatedProductIds);// Setting Associated Products
    $product->setCanSaveConfigurableAttributes(true);
    $product->save();

    echo "configurable product id: ".$configurable_product->getId()."\n";

?>  

1 个答案:

答案 0 :(得分:0)

我建议您使用magmi / magmi-m2从外部magento插入产品。这样可以减少您的工作量。请参阅configurables product insertion here