Preciso exibir algumas imagens do produto no front-end com base na condição: o uso do espelho virtual deve ser verificado.
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Dcw\Vm\Observer;
use Magento\Framework\Event\ObserverInterface;
class ChangeTemplateObserver extends \Magento\ProductVideo\Observer\ChangeTemplateObserver
{
/**
* @param mixed $observer
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @return void
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
$observer->getBlock()->setTemplate('Dcw_Vm::helper/gallery.phtml');
}
}
Modelo:
<div class="admin__field field-image-vm">
<div class="admin__field-control">
<div class="admin__field admin__field-option">
<input type="checkbox"
id="use-for-vm"
data-role="vm-save"
data-form-part="<?php /* @escapeNotVerified */ echo $formName ?>"
value="1"
class="admin__control-checkbox"
name="<?php /* @escapeNotVerified */ echo $elementName ?>[<%- data.file_id %>][vm]"
<% if (data.useforvm == 1) { %>checked="checked"<% } %> />
<label for="use-for-vm" class="admin__field-label">
<?php /* @escapeNotVerified */ echo __('Use for Virutal Mirror')?>
</label>
</div>
</div>
</div>
Script de instalação:
<?php
namespace Dcw\Vm\Setup;
use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Catalog\Model\ResourceModel\Product\Gallery;
class InstallSchema implements InstallSchemaInterface {
public function install(SchemaSetupInterface $setup, ModuleContextInterface $context) {
$setup->startSetup();
$setup->getConnection()->addColumn(
$setup->getTable(Gallery::GALLERY_TABLE), 'vm', [
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
'unsigned' => true,
'nullable' => false,
'default' => 0,
'comment' => 'use for Vm' ]
);
$setup->endSetup();
}
}
Como salvar o estado das imagens verificadas no back-end? E como filtrar essas imagens no frontend? Você pode me ajudar nisso?
ATUALIZAR:
o seguinte observador (no evento catalog_product_save_after
) para imagens existentes está funcionando, mas para novas imagens não estão funcionando.
<?php
namespace Dcw\Vm\Observer;
use Magento\Framework\Event\ObserverInterface;
class Productsaveafter implements ObserverInterface {
protected $request;
protected $resource;
/**
*
* @param \Magento\Framework\App\RequestInterface $request
* @param \Magento\Framework\App\ResourceConnection $resource\
*/
public function __construct(
\Magento\Framework\App\RequestInterface $request, \Magento\Framework\App\ResourceConnection $resource
) {
$this->request = $request;
$this->resource = $resource;
}
public function execute(\Magento\Framework\Event\Observer $observer) {
$vm = array();
$data = $this->request->getPostValue();
if (isset($data['product']['media_gallery']['images'])) {
$images = $data['product']['media_gallery']['images'];
foreach ($images as $image) {
if (isset($image['vm']) && $image['vm'] == 1) {
$vm[$image['value_id']] = 1;
} else {
$vm[$image['value_id']] = 0;
}
}
// print_r($images);exit;
$connection = $this->resource->getConnection();
$tableName = 'catalog_product_entity_media_gallery'; //gives table name with prefix
$product = $observer->getProduct();
$mediaGallery = $product->getMediaGallery();
if (isset($mediaGallery['images'])) {
foreach ($mediaGallery['images'] as $image) {
if (isset($vm[$image['value_id']])) {
//Update Data into table
$sql = "Update " . $tableName . " Set vm = " . $vm[$image['value_id']] . " where value_id = " . $image['value_id'];
$connection->query($sql);
}
}
}
}
}
}
ChangeTemplateObserver
?