cancel
Showing results for 
Search instead for 
Did you mean: 

Setup:upgrade cannot download image. Error: The image does not exists.

Setup:upgrade cannot download image. Error: The image does not exists.

Hi,
I tried import products from xml file (about 10000 products) with setup:upgrade, but I can not import image.
Error: The image does not exists.
$img_url string is correct and it looks like this: https://www.site-name.com/assets/productimgs/m/image-name.jpg
I tried to download image on virtualbox guest with wget and it works.
Any help please.
I run Magento CE 2.2.6, on Ubuntu 17.10 server Oracle VM VirtualBox guest on Ubuntu 18.04 host.
Here is my InstallData.php code:


<?php

namespace Test\Sample\Setup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface {
/**
* @var string
*/
protected $productType = \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE;

/**
* @var \Magento\Catalog\Model\ProductFactory
*/
protected $productFactory;

/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $storeManager;

/** @var \Magento\Framework\App\State **/
private $_state;

public function __construct(
\Magento\Catalog\Model\ProductFactory $productFactory,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\App\State $state
) {
$this->productFactory = $productFactory;
$this->storeManager = $storeManager;
$this->_state = $state;
}

/**
* {@inheritdoc}
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
$this->_state->setAreaCode(\Magento\Framework\App\Area::AREA_GLOBAL);
// $this->state->setAreaCode(\Magento\Framework\App\Area::AREA_FRONTEND);
// or \Magento\Framework\App\Area::AREA_ADMINHTML, depending on your needs
$xmlUrl = "/home/ubuntu/bcg-products.xml";
$xmlDoc = simplexml_load_file($xmlUrl);
$i = 0;
foreach($xmlDoc->product as $product) {
$i++;
$sku = (string)$product->sku;
$price = (string)$product->price;
settype($price, "float");
$special_price = (string)$product->special_price;
settype($special_price, "float");
$fix = (string)$product->fix;
settype($fix, "integer");
$akc = (string)$product->akc;
settype($akc, "integer");
$categoryid = (string)$product->categoryid;
settype($categoryid, "integer");
$category = (string)$product->category;
$brand = (string)$product->brand;
$name = (string)$product->name;
$atribute = (string)$product->attributes->atribute;
$value = (string)$product->attributes->value;
$img_url = (string)$product->images->img_url;
$desc = (string)$product->desc;
$url_key = $name.'-'.$sku;

$data = [
'name' => $name,
'sku' => $sku,
'price' => $price,
'special_price' => $special_price,
'fix' => $fix,
'akc' => $akc,
'categoryid' => $categoryid,
'category' => $category,
'brand' => $brand,
'atribute' => $atribute,
'value' => $value,
'description' => $desc
];

$attributeSetId = 4; //Attribute set default
$product = $this->productFactory->create();
$product->setData($data);
$product->setTypeId($this->productType)
->setAttributeSetId($attributeSetId)
->setWebsiteIds([$this->storeManager->getDefaultStoreView()->getWebsiteId()])
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status:Smiley FrustratedTATUS_ENABLED)
->setStockData(['is_in_stock' => 1, 'manage_stock' => 0, 'qty' => 100])
->setUrlKey($url_key)
->setStoreId(\Magento\Store\Model\Store:Smiley Very HappyEFAULT_STORE_ID);
if (empty($data['visibility'])) {
$product->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);
}
$product->addImageToMediaGallery($img_url, array('image', 'small_image', 'thumbnail'), false, false);
$product->save();
}
}
}

?>