Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace DcwVmObserver;
- use MagentoFrameworkEventObserverInterface;
- class ChangeTemplateObserver extends MagentoProductVideoObserverChangeTemplateObserver
- {
- /**
- * @param mixed $observer
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
- * @return void
- */
- public function execute(MagentoFrameworkEventObserver $observer)
- {
- $observer->getBlock()->setTemplate('Dcw_Vm::helper/gallery.phtml');
- }
- }
- <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>
- <?php
- namespace DcwVmSetup;
- use MagentoFrameworkSetupInstallSchemaInterface;
- use MagentoFrameworkSetupSchemaSetupInterface;
- use MagentoFrameworkSetupModuleContextInterface;
- use MagentoCatalogModelResourceModelProductGallery;
- class InstallSchema implements InstallSchemaInterface {
- public function install(SchemaSetupInterface $setup, ModuleContextInterface $context) {
- $setup->startSetup();
- $setup->getConnection()->addColumn(
- $setup->getTable(Gallery::GALLERY_TABLE), 'vm', [
- 'type' => MagentoFrameworkDBDdlTable::TYPE_SMALLINT,
- 'unsigned' => true,
- 'nullable' => false,
- 'default' => 0,
- 'comment' => 'use for Vm' ]
- );
- $setup->endSetup();
- }
- }
- <?php
- namespace DcwVmObserver;
- use MagentoFrameworkEventObserverInterface;
- class Productsaveafter implements ObserverInterface {
- protected $request;
- protected $resource;
- /**
- *
- * @param MagentoFrameworkAppRequestInterface $request
- * @param MagentoFrameworkAppResourceConnection $resource
- */
- public function __construct(
- MagentoFrameworkAppRequestInterface $request, MagentoFrameworkAppResourceConnection $resource
- ) {
- $this->request = $request;
- $this->resource = $resource;
- }
- public function execute(MagentoFrameworkEventObserver $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);
- }
- }
- }
- }
- }
- }
- <?php
- namespace DcwVmObserver;
- use MagentoFrameworkEventObserverInterface;
- class ProductSaveAfter implements ObserverInterface {
- protected $request;
- protected $resource;
- /**
- *
- * @param MagentoFrameworkAppRequestInterface $request
- * @param MagentoFrameworkAppResourceConnection $resource
- */
- public function __construct(
- MagentoFrameworkAppRequestInterface $request, MagentoFrameworkAppResourceConnection $resource
- ) {
- $this->request = $request;
- $this->resource = $resource;
- }
- public function execute(MagentoFrameworkEventObserver $observer)
- {
- $data = $this->request->getPostValue();
- if (isset($data['product']['media_gallery']['images'])) {
- // 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) {
- //Update Data into table
- $vmValue = !empty($image['vm']) ? (int)$image['vm'] : 0;
- $sql = "UPDATE " . $tableName . " SET vm = " . $vmValue . " WHERE value_id = " . $image['value_id'];
- $connection->query($sql);
- }
- }
- }
- }
- }
- <?xml version="1.0"?>
- <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
- <type name="MagentoCatalogModelResourceModelProductGallery">
- <plugin name="afterCreateBatchBaseSelect" type="DcwVmPluginProductGallery" sortOrder="10" disabled="false"/>
- </type>
- </config>
- <?php
- namespace DcwVmPluginProduct;
- class Gallery
- {
- public function afterCreateBatchBaseSelect(
- MagentoCatalogModelResourceModelProductGallery $subject,
- MagentoFrameworkDBSelect $select
- ) {
- $select->columns('vm');
- return $select;
- }
- }
- <?xml version="1.0"?>
- <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
- <type name="MagentoCatalogModelProduct">
- <plugin name="afterGetMediaGalleryImages" type="DcwVmPluginProduct" sortOrder="10" disabled="false"/>
- </type>
- </config>
- <?php
- namespace DcwVmPlugin;
- class Product
- {
- /**
- * @param MagentoCatalogModelProduct $subject
- * @param MagentoFrameworkDataCollection $result
- * @return mixed
- */
- public function afterGetMediaGalleryImages(MagentoCatalogModelProduct $subject, $result)
- {
- foreach ($result as $key => $image) {
- if ($image['vm']) {
- $result->removeItemByKey($key);
- }
- }
- return $result;
- }
- }
- $images = [];
- foreach ($product->getMediaGalleryImages() as $image) {
- if ($image->getVm()) {
- $images[] = $image;
- }
- }
- $images = [];
- foreach ($product->getMediaGalleryImages() as $image) {
- if ($image->getVm()) {
- $images[] = $image;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement