Pour tout problème contactez-nous par mail : support@froggit.fr | La FAQ :grey_question: | Rejoignez-nous sur le Chat :speech_balloon:

Skip to content
Snippets Groups Projects
Commit 6596132f authored by Nicolas's avatar Nicolas
Browse files

Création de l'Helper

parent c83313f6
No related branches found
No related tags found
No related merge requests found
<?php
namespace NicolasBejean\Importer\Helper\Cron;
use \Magento\Framework\App\Helper\AbstractHelper;
use \Magento\Framework\App\Helper\Context;
use \Magento\Catalog\Api\ProductRepositoryInterface;
use \Magento\Catalog\Model\Product;
use \Magento\Catalog\Model\Product\Gallery\Processor as ProductImageProcessor;
use \Magento\Framework\App\Config\ScopeConfigInterface;
use \Magento\Store\Model\ScopeInterface;
use \Magento\Store\Model\StoreManagerInterface;
use \Magento\Framework\Exception\CouldNotSaveException;
use \Magento\Framework\Exception\InputException;
use \Magento\Framework\Exception\LocalizedException;
use \Magento\Framework\Exception\NoSuchEntityException;
use \Magento\Framework\Exception\StateException;
use \Magento\Framework\Filesystem\DirectoryList;
/**
* Class ProductImages
*
* @category PHP
* @package NicolasBejean\Importer\Helper\Cron\ProductImages
* @author Nicolas Béjean <nicolas@bejean.eu>
* @license https://github.com/nicolasbejean/importer/blob/master/licence.txt BSD Licence
* @link https://www.bejean.eu
*/
class ProductImages extends AbstractHelper
{
/**
* @var DirectoryList
*/
protected $directoryList;
/**
* @var ScopeConfigInterface
*/
protected $scopeConfig;
/**
* @var StoreManagerInterface
*/
protected $storeManager;
/**
* @var ProductRepositoryInterface
*/
private $productInterface;
/**
* @var ProductImageProcessor
*/
protected $productImageProcessor;
/**
* @var string
*/
protected $delimiter;
/**
* @var string
*/
protected $directory;
/**
* ProductImages constructor.
*
* @param Context $context
* @param DirectoryList $directoryList
* @param ScopeConfigInterface $scopeConfig
* @param StoreManagerInterface $storeManager
* @param ProductRepositoryInterface $productInterface
* @param ProductImageProcessor $productImageProcessor
*/
public function __construct (
Context $context,
DirectoryList $directoryList,
ScopeConfigInterface $scopeConfig,
StoreManagerInterface $storeManager,
ProductRepositoryInterface $productInterface,
ProductImageProcessor $productImageProcessor
) {
$this->directoryList = $directoryList;
/* Pour récupérer les paramétres du module */
$this->scopeConfig = $scopeConfig;
$this->storeManager = $storeManager;
$this->productInterface = $productInterface;
$this->productImageProcessor = $productImageProcessor;
//$this->directory = $this->directoryList->getPath();
$this->delimiter = $this->getDelimiter();
parent::__construct($context);
}
/**
* @return void
*/
public function execute()
{
// /* Définit le SKU du produit en fonction du nom du fichier */
// $sku = $this->getSku($file);
//
// /* Recherche un produit existant en fonction du nom du fichier */
// try {
// /** @var Product $product */
// $product = $this->productInterface->get($sku);
// } catch (NoSuchEntityException $e) {
// echo 'no-product';
// exit;
// }
//
// /* Supprime tous les rôles assignés aux images d'un produit */
// $this->clearMediaAttribute($product);
//
// /* Supprime toutes les images d'un produit */
// $this->deleteProductGallery($product);
//
//
//
// var_dump($result);
//
// /* Ajout l'image
// $this->addImageToMediaGallery($product, $result);
//
//
// die();
//
// echo 'product-found';
}
/**
* Retourne la valeur du délimiteur indiqué dans le BO, sinon renvoie la valeur par défaut "_"
*
* @return string
*/
private function getDelimiter () {
try {
$storeId = $this->storeManager->getStore()->getId();
} catch (NoSuchEntityException $e) {
return '_';
}
return $this->scopeConfig->getValue('importimagesproduct/pattern/delimiter', ScopeInterface::SCOPE_STORE, $storeId);
}
/**
* Retourne le SKU en fonction du nom du fichier
*
* @param array $file
* @return string
*/
private function getSku ($file) {
/* On récupère le nom du fichier */
list($fileName) = explode('.', $file['name']);
/* On récupère le sku */
list($sku) = explode($this->delimiter, $fileName);
return $sku;
}
/**
* Supprime tous les rôles assignés aux images d'un produit
*
* @param Product $product
*/
private function clearMediaAttribute (Product $product) {
$this->productImageProcessor->clearMediaAttribute($product, $this->productImageProcessor->getMediaAttributeCodes());
try {
$this->productInterface->save($product);
} catch (CouldNotSaveException $e) {
} catch (InputException $e) {
} catch (StateException $e) {
}
}
/**
* Supprime toutes les images d'un produit
*
* @param Product $product
*/
private function deleteProductGallery (Product $product) {
$existingGallery = $product->getMediaGalleryImages();
foreach ($existingGallery as $image) {
$this->productImageProcessor->removeImage($product, $image->getFile());
}
try {
$this->productInterface->save($product);
} catch (CouldNotSaveException $e) {
} catch (InputException $e) {
} catch (StateException $e) {
}
}
/**
* Supprime toutes les images d'un produit
*
* @param Product $product
* @param array $file
*/
private function addImageToMediaGallery (Product $product, array $file) {
$filePath = $file['path'] . '/' . $file['name'];
try {
$product->addImageToMediaGallery($filePath, $this->productImageProcessor->getMediaAttributeCodes(), true, false);
} catch (LocalizedException $e) {
var_dump($e->getMessage());
die();
}
try {
$this->productInterface->save($product);
} catch (CouldNotSaveException $e) {
} catch (InputException $e) {
} catch (StateException $e) {
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment