0

Client requires that I create an external page to add product. Got the below code online and it's working, only that the product image is not showing after adding product successfully.

Images have been uploaded to pub/media/catalog/product/ directory

use \Magento\Framework\App\Bootstrap; include('app/bootstrap.php');

// add bootstrap
$bootstraps = Bootstrap::create(BP, $_SERVER);
$object_Manager = $bootstraps->getObjectManager();

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

// get date 
$today_date = date("m/d/Y");
$added_date = date('m/d/Y',strtotime("+17 day"));

$set_product = $object_Manager->create('\Magento\Catalog\Model\Product');

try{
    $set_product->setWebsiteIds(array(1));
    $set_product->setAttributeSetId(4);
    $set_product->setTypeId('simple');
    $set_product->setCreatedAt(strtotime('now')); 
    // time of product creation
    $set_product->setName($product_name); 
    // add Name of Product
    $set_product->setSku($product_sku);
    // add sku hear
//    $set_product->setWeight(3.6700);
    // add weight of product
    $set_product->setStatus(1);
    $category_id= array(1,2);
    // add your catagory id
    $set_product->setCategoryIds($category_id); 
    // Product Category
    $set_product->setTaxClassId(0); 
    // type of tax class 
    // (0 - none, 1 - default, 2 - taxable, 4 - shipping)
    $set_product->setVisibility(4); 
    // catalog and search visibility
    $set_product->setNewsFromDate($today_date); 
    // product set as new from
    $set_product->setNewsToDate($added_date); 
    // add image path hear
    $set_product->setPrice($product_price) ;
    // price in form 100.99
//    $set_product->setCost(33116.33); 
    // price in form 88.33
//    $set_product->setSpecialPrice(33004.85); 
    // special price in form 99.85
//    $set_product->setSpecialFromDate('06/1/2017'); 
    // special price from (MM-DD-YYYY)
//    $set_product->setSpecialToDate('07/30/2017'); 
    // special price to (MM-DD-YYYY)
//    $set_product->setMsrpEnabled(1); 
    // enable MAP
    $set_product->setMsrpDisplayActualPriceType(1); 
    // display actual price 
    // (1 - on gesture, 2 - in cart, 3 - before order confirmation, 4 - use config)
    $set_product->setMetaTitle($product_name);
    $set_product->setMetaKeyword($product_keywords);
    $set_product->setMetaDescription($product_short_desc);
    $set_product->setDescription($product_long_desc);
    $set_product->setShortDescription($product_short_desc);

    $set_product->setStockData(
        array(
        'use_config_manage_stock' => 0, 
        // checkbox for 'Use config settings' 
        'manage_stock' => 1, // manage stock
        'min_sale_qty' => 1, // Shopping Cart Minimum Qty Allowed 
        'max_sale_qty' => 2, // Shopping Cart Maximum Qty Allowed
        'is_in_stock' => 0, // Stock Availability of product
        'qty' => $product_quantity // qty of product
        )
    );
     //add image to product
    $set_product ->setImage("'/test.jpg");
    $set_product ->setSmallImage("/".$product_photo);
    $set_product ->setThumbnail("/".$product_photo);

  $set_product->save();

   $get_product_id = $set_product->getId();

                echo '<br><div class="alert alert-success" role="alert">
                    <strong>Done!</strong> Product Upload successful
                </div>';
}
catch(Exception $exception)
{
    echo 'Product upload failed!';
}

What am i doing wrong please? Running Magento 2.1.x

1 Answer 1

1

Used addImageToMediaGallery to add image with product.

$file='test.jpg';
$filepath='/catalog/product' .trim($file);
try{
    $set_product->addImageToMediaGallery($filepath,array('image','thumbnail','small','swatch'), false, false);
   }catch(Exception $e){}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.