如何在magento 2中以编程方式导入产品

时间:2017-10-12 12:55:18

标签: magento2

我尝试在magento 2中导入单个产品。它失败了..

我在magento 2根目录中尝试了一个脚本 我的代码是

 <?php 
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$params = $_SERVER;
$params[Bootstrap::PARAM_REQUIRE_MAINTENANCE] = true; // default false
$params[Bootstrap::PARAM_REQUIRE_IS_INSTALLED] = false; // default true
$bootstrap = Bootstrap::create(BP, $params);
$objectManager = $bootstrap->getObjectManager();

$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

    $simple_product = $objectManager->create('\Magento\Catalog\Model\Product');
    $simple_product->setSku('Tops');
    $simple_product->setName('tops');
    $simple_product->setAttributeSetCode('Default');
    $simple_product->setCategories('Default Category/Women');
    $simple_product->setStatus(1);
    $simple_product->setTypeId('simple');
    $simple_product->setPrice(10);
    $simple_product->setProductWebsites('base');
    $simple_product->setCategoryIds(array(31));
    $simple_product->setUrlKey ('tops');
$simple_product->setColor('Red');
    $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";
?>

我收到以下错误

Fatal error: Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`magento`.`catalog_product_entity`, CONSTRAINT `CAT_PRD_ENTT_ATTR_SET_ID_EAV_ATTR_SET_ATTR_SET_ID` FOREIGN KEY (`attribute_set_id`) REFERENCES `eav_attribute_set` (`attribute_set_id`) ON DELET) in /var/www/html/magento/vendor/magento/zendframework1/library/Zend/Db/Statement/Pdo.php:228 Stack trace: #0 /var/www/html/magento/vendor/magento/zendframework1/library/Zend/Db/Statement/Pdo.php(228): PDOStatement->execute(Array) #1 /var/www/html/magento/vendor/magento/framework/DB/Statement/Pdo/Mysql.php(95): Zend_Db_Statement_Pdo->_execute(Array) #2 /var/www/html/magento/vendor/magento/zendframework1/library/Zend/Db/Statement.php(303): Magento\Framework\DB\Statement\Pdo\Mysql->_execute(Array) #3 /var/www/html/magento/vendor/magento/zendframework1/library/Zend/Db/Adapter/Abstract.php(480): Zend_Db_Statement->execute(Array) #4 /var/www/html/magento/vendor/magento/zendframework1/ in /var/www/html/magento/vendor/magento/zendframework1/library/Zend/Db/Statement/Pdo.php on line 235

有人能给我一个解决方案吗?

3 个答案:

答案 0 :(得分:0)

这是您的代码的更新版本。请使用此代码创建产品。

use Magento\Framework\App\Bootstrap;
include("app/bootstrap.php");
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$simple_product = $objectManager->create('\Magento\Catalog\Model\Product');
$simple_product->setSku('tops123');
$simple_product->setName('tops');
$simple_product->setAttributeSetId(4);;
$simple_product->setCategories('Default Category/Women');
$simple_product->setStatus(1);
$simple_product->setTypeId('simple');
$simple_product->setPrice(10);
$simple_product->setWebsiteIds(array(1));
$simple_product->setCategoryIds(array(31));
$simple_product->setUrlKey ('tops343225');
$simple_product->setColor('Red');
$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";

由于

答案 1 :(得分:0)

适用于M2的简单产品     

use Magento\Framework\App\Bootstrap;

include("../app/bootstrap.php");
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$simpleProduct = $objectManager->create('\Magento\Catalog\Model\Product');
$simpleProduct->setSku('Testing3');
$simpleProduct->setName('Testing3');
$simpleProduct->setAttributeSetId(9);
$simpleProduct->setCategoryIds(3);
$simpleProduct->setDescription('This is for testing');
$simpleProduct->setStatus(1);
$simpleProduct->setTypeId('simple');
$simpleProduct->setPrice(500);
$simpleProduct->setWebsiteIds(array(1));
$simpleProduct->setVisibility(4);
$simpleProduct->setUrlKey('Testing3');

$simpleProduct->setStockData(array(
    'is_in_stock' => 1, //Stock Availability
    'qty' => 100//qty
        )
);

$attr = $simpleProduct->getResource()->getAttribute('color');
$attributeOptionId = $attr->getSource()->getOptionId('Red'); //name in Default Store View
$simpleProduct->setData('color', $attributeOptionId);

$simpleProduct->save();
$simpleProductId = $simpleProduct->getId();
echo "Simple Product ID: " . $simpleProductId . "\n";
?>

答案 2 :(得分:0)

适用于M2的可配置产品

<?php

use Magento\Framework\App\Bootstrap;

include("../app/bootstrap.php");
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');


$productId = 809; // Configurable Product Id
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($productId); // Load Configurable Product
$attributeModel = $objectManager->create('Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute');
$position = 0;
$attributes = array(93, 135); // Super Attribute Ids Used To Create Configurable Product
$associatedProductIds = array(806, 807); //Product Ids Of Associated Products
foreach ($attributes as $attributeId) {
    $data = array('attribute_id' => $attributeId, 'product_id' => $productId, 'position' => $position);
    $position++;
    $attributeModel->setData($data)->save();
}
$product->setTypeId("configurable"); // Setting Product Type As Configurable
$product->setAffectConfigurableProductAttributes(4);
$objectManager->create('Magento\ConfigurableProduct\Model\Product\Type\Configurable')->setUsedProductAttributeIds($attributes, $product);
$product->setNewVariationsAttributeSetId(9); // Setting Attribute Set Id
$product->setAssociatedProductIds($associatedProductIds); // Setting Associated Products
$product->setCanSaveConfigurableAttributes(true);
$product->save();
$configProductId = $product->getId();
        echo "Configurable product id: " . $configProductId . "\n";
?>