Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
try{
            $_product->setName($product["name"]);
            $_product->setTypeId('simple');
            $_product->setAttributeSetId(4);
            $_product->setSku($product["sku"]); // this must be unique everytime and get the exception in frontend
            $_product->setWebsiteIds(array(1));
            $_product->setVisibility(4);
            $_product->setDescription($product["description"]);
            $_product->setPrice($product["price"]);
            

            //$imagePath = $product['imgpath'];

            $_product->setMediaGallery(array('images'=>array (),'values'=>array ())); 
            $_product->addImageToMediaGallery('https://beebom-redkapmedia.netdna-ssl.com/wp-content/uploads/2016/01/Reverse-Image-Search-Engines-Apps-And-Its-Uses-2016.jpg', array('image', 'small_image', 'thumbnail'),false);
            // $_product->setImage('https://beebom-redkapmedia.netdna-ssl.com/wp-content/uploads/2016/01/Reverse-Image-Search-Engines-Apps-And-Its-Uses-2016.jpg');
        
            // $_product->setSmallImage('/testimg/test.jpg');
        
            // $_product->setThumbnail('/testimg/test.jpg');
            $_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' => $product['stockstatus'], //Stock Availability
                    'qty' =>  $product['quantity']
                    )
                );
            $_product->save();
            
        }                             
        catch(\Exception $e) { 
             echo $e->getMessage();
        }
try{
            $_product->setName($product["name"]);
            $_product->setTypeId('simple');
            $_product->setAttributeSetId(4);
            $_product->setSku($product["sku"]); // this must be unique everytime and get the exception in frontend
            $_product->setWebsiteIds(array(1));
            $_product->setVisibility(4);
            $_product->setDescription($product["description"]);
            $_product->setPrice($product["price"]);
            

            //$imagePath = $product['imgpath'];

            $_product->setMediaGallery(array('images'=>array (),'values'=>array ())); 
            $_product->addImageToMediaGallery('https://beebom-redkapmedia.netdna-ssl.com/wp-content/uploads/2016/01/Reverse-Image-Search-Engines-Apps-And-Its-Uses-2016.jpg', array('image', 'small_image', 'thumbnail'),false);
            // $_product->setImage('https://beebom-redkapmedia.netdna-ssl.com/wp-content/uploads/2016/01/Reverse-Image-Search-Engines-Apps-And-Its-Uses-2016.jpg');
        
            // $_product->setSmallImage('/testimg/test.jpg');
        
            // $_product->setThumbnail('/testimg/test.jpg');
            $_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' => $product['stockstatus'], //Stock Availability
                    'qty' =>  $product['quantity']
                    )
                );
            $_product->save();
            
        }                             
        catch(\Exception $e) { 
             echo $e->getMessage();
        }
try{
$_product->setName($product["name"]);
$_product->setTypeId('simple');
$_product->setAttributeSetId(4);
$_product->setSku($product["sku"]); // this must be unique everytime and get the exception in frontend
$_product->setWebsiteIds(array(1));
$_product->setVisibility(4);
$_product->setDescription($product["description"]);
$_product->setPrice($product["price"]);


//$imagePath = $product['imgpath'];

$_product->setMediaGallery(array('images'=>array (),'values'=>array ())); 
$_product->addImageToMediaGallery('https://beebom-redkapmedia.netdna-ssl.com/wp-content/uploads/2016/01/Reverse-Image-Search-Engines-Apps-And-Its-Uses-2016.jpg', array('image', 'small_image', 'thumbnail'),false);
// $_product->setImage('https://beebom-redkapmedia.netdna-ssl.com/wp-content/uploads/2016/01/Reverse-Image-Search-Engines-Apps-And-Its-Uses-2016.jpg');

// $_product->setSmallImage('/testimg/test.jpg');

// $_product->setThumbnail('/testimg/test.jpg');
$_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' => $product['stockstatus'], //Stock Availability
        'qty' =>  $product['quantity']
        )
    );
$_product->save();
}                 
catch(\Exception $e) { 
 echo $e->getMessage();
}
Source Link
Jay Kapoor
  • 603
  • 2
  • 12
  • 30

Magento 2 : Upload product image programatically

I am new to Magento and i want upload upload product programmatically along with product image, i am using this code to implement, please check

try{
            $_product->setName($product["name"]);
            $_product->setTypeId('simple');
            $_product->setAttributeSetId(4);
            $_product->setSku($product["sku"]); // this must be unique everytime and get the exception in frontend
            $_product->setWebsiteIds(array(1));
            $_product->setVisibility(4);
            $_product->setDescription($product["description"]);
            $_product->setPrice($product["price"]);
            

            //$imagePath = $product['imgpath'];

            $_product->setMediaGallery(array('images'=>array (),'values'=>array ())); 
            $_product->addImageToMediaGallery('https://beebom-redkapmedia.netdna-ssl.com/wp-content/uploads/2016/01/Reverse-Image-Search-Engines-Apps-And-Its-Uses-2016.jpg', array('image', 'small_image', 'thumbnail'),false);
            // $_product->setImage('https://beebom-redkapmedia.netdna-ssl.com/wp-content/uploads/2016/01/Reverse-Image-Search-Engines-Apps-And-Its-Uses-2016.jpg');
        
            // $_product->setSmallImage('/testimg/test.jpg');
        
            // $_product->setThumbnail('/testimg/test.jpg');
            $_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' => $product['stockstatus'], //Stock Availability
                    'qty' =>  $product['quantity']
                    )
                );
            $_product->save();
            
        }                             
        catch(\Exception $e) { 
             echo $e->getMessage();
        }

i provided the static path for the image and its opening in the browser, but getting error The image does not exist. while uploading product. please suggest.