Un controlador con respuesta Json y devolviendo las imagenes de un producto especifico
<?php
namespace Practice\Slideshow\Controller\Product;
use \Magento\Framework\App\Action\Action as Action;
use \Magento\Framework\App\Action\Context as Context;
use \Magento\Framework\View\Result\PageFactory as PageFactory;
use \Magento\Catalog\Model\ProductFactory as ProductFactory;
use \Magento\Catalog\Helper\Image as Image;
use \Magento\Framework\Controller\Result\JsonFactory as JsonFactory;
class Load extends Action
{
/**
* @var \Magento\Catalog\Model\ProductFactory
*/
protected $_productFactory;
/** @var \Magento\Framework\View\Result\PageFactory */
protected $resultPageFactory;
/**
* @var \Magento\Catalog\Helper\Image
*/
protected $_helperImage;
/**
* @var \Magento\Framework\Controller\Result\JsonFactory
*/
protected $_jsonFactory;
/**
* Load constructor.
* @param Context $context
* @param PageFactory $resultPageFactory
* @param ProductFactory $productFactory
* @param Image $helperImage
* @param JsonFactory $jsonFactory
*/
public function __construct(
Context $context,
PageFactory $resultPageFactory,
ProductFactory $productFactory,
Image $helperImage,
JsonFactory $jsonFactory
)
{
$this->resultPageFactory = $resultPageFactory;
$this->_productFactory = $productFactory;
$this->_helperImage = $helperImage;
$this->_jsonFactory = $jsonFactory;
parent::__construct($context);
}
public function execute()
{
$result = $this->_jsonFactory->create();
$imagesToSlideshow = [];
$product = $this->_productFactory->create()->load(2);
$images = $product->getMediaGalleryImages();
$dataProduct = [
'product_name' => $product->getName(),
'product_url' => $product->getProductUrl(),
'product_price' => $product->getPrice()
];
foreach ($images as $image){
$imageFinal = $this->_helperImage->init($product,$image->getId())
->constrainOnly(TRUE)
->keepAspectRatio(TRUE)
->keepTransparency(TRUE)
->keepFrame(FALSE)
->resize(300, 300)
->getUrl();
$imagesToSlideshow[] = $imageFinal;
}
$dataProduct['images'] = $imagesToSlideshow;
return $result->setData($imagesToSlideshow);
}
}