PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, August 22, 2022

[FIXED] how to import a product programatically in magento 2

 August 22, 2022     magento2     No comments   

Issue

I tried importing a single product in magento 2..It failed..

I tried a script in magento 2 root directory and My code is

 <?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";
?>

and i am getting the following error

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

Can anyone give me a solution?


Solution

For Simple Product Working on 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";
?>


Answered By - Ramanathan
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing