Ich muss ein paar Bilder des Produkts im Frontend anzeigen, basierend auf der Bedingung: Die Verwendung für den virtuellen Spiegel sollte überprüft werden.
<?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');
}
}
Vorlage:
<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>
Skript installieren:
<?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();
}
}
Wie speichere ich den Status der überprüften Bilder im Backend? Und wie filtert man diese Bilder im Frontend? Kannst du mir dabei helfen?
AKTUALISIEREN:
Der folgende Beobachter (bei Ereignis catalog_product_save_after
) für vorhandene Bilder funktioniert, für neue Bilder jedoch nicht.
<?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
?