Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
product_export_with_images.php
Go to the documentation of this file.
1 <?php
7 require dirname(__DIR__, 2) . '/Catalog/_files/product_image.php';
8 require dirname(__DIR__, 2) . '/Catalog/_files/product_simple.php';
9 
12 $productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
13 $product = $productRepository->get('simple');
14 $product->setStoreId(0)
15  ->setImage('/m/a/magento_image.jpg')
16  ->setSmallImage('/m/a/magento_image.jpg')
17  ->setThumbnail('/m/a/magento_image.jpg')
18  ->setData(
19  'media_gallery',
20  [
21  'images' => [
22  [
23  'file' => '/m/a/magento_image.jpg',
24  'position' => 1,
25  'label' => 'Image Alt Text',
26  'disabled' => 0,
27  'media_type' => 'image',
28  ],
29  ],
30  ]
31  )->save();
32 $image = array_shift($product->getData('media_gallery')['images']);
33 $product = $productRepository->get('simple', false, 1, true);
34 $product->setData(
35  'media_gallery',
36  [
37  'images' => [
38  [
39  'value_id' => $image['value_id'],
40  'file' => $image['file'],
41  'disabled' => 1,
42  'media_type' => 'image',
43  ],
44  ],
45  ]
46 );
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60