Advertisement
Guest User

Untitled

a guest
Sep 26th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.08 KB | None | 0 0
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6.  
  7. namespace DcwVmObserver;
  8.  
  9. use MagentoFrameworkEventObserverInterface;
  10.  
  11. class ChangeTemplateObserver extends MagentoProductVideoObserverChangeTemplateObserver
  12. {
  13. /**
  14. * @param mixed $observer
  15. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  16. * @return void
  17. */
  18. public function execute(MagentoFrameworkEventObserver $observer)
  19. {
  20. $observer->getBlock()->setTemplate('Dcw_Vm::helper/gallery.phtml');
  21. }
  22. }
  23.  
  24. <div class="admin__field field-image-vm">
  25. <div class="admin__field-control">
  26. <div class="admin__field admin__field-option">
  27. <input type="checkbox"
  28. id="use-for-vm"
  29. data-role="vm-save"
  30. data-form-part="<?php /* @escapeNotVerified */ echo $formName ?>"
  31. value="1"
  32. class="admin__control-checkbox"
  33. name="<?php /* @escapeNotVerified */ echo $elementName ?>[<%- data.file_id %>][vm]"
  34. <% if (data.useforvm == 1) { %>checked="checked"<% } %> />
  35.  
  36. <label for="use-for-vm" class="admin__field-label">
  37. <?php /* @escapeNotVerified */ echo __('Use for Virutal Mirror')?>
  38. </label>
  39. </div>
  40. </div>
  41. </div>
  42.  
  43. <?php
  44.  
  45. namespace DcwVmSetup;
  46.  
  47. use MagentoFrameworkSetupInstallSchemaInterface;
  48. use MagentoFrameworkSetupSchemaSetupInterface;
  49. use MagentoFrameworkSetupModuleContextInterface;
  50. use MagentoCatalogModelResourceModelProductGallery;
  51.  
  52. class InstallSchema implements InstallSchemaInterface {
  53.  
  54. public function install(SchemaSetupInterface $setup, ModuleContextInterface $context) {
  55. $setup->startSetup();
  56.  
  57. $setup->getConnection()->addColumn(
  58. $setup->getTable(Gallery::GALLERY_TABLE), 'vm', [
  59. 'type' => MagentoFrameworkDBDdlTable::TYPE_SMALLINT,
  60. 'unsigned' => true,
  61. 'nullable' => false,
  62. 'default' => 0,
  63. 'comment' => 'use for Vm' ]
  64. );
  65.  
  66. $setup->endSetup();
  67. }
  68.  
  69. }
  70.  
  71. <?php
  72.  
  73. namespace DcwVmObserver;
  74.  
  75. use MagentoFrameworkEventObserverInterface;
  76.  
  77. class Productsaveafter implements ObserverInterface {
  78.  
  79. protected $request;
  80. protected $resource;
  81.  
  82. /**
  83. *
  84. * @param MagentoFrameworkAppRequestInterface $request
  85. * @param MagentoFrameworkAppResourceConnection $resource
  86. */
  87. public function __construct(
  88. MagentoFrameworkAppRequestInterface $request, MagentoFrameworkAppResourceConnection $resource
  89. ) {
  90. $this->request = $request;
  91. $this->resource = $resource;
  92. }
  93.  
  94. public function execute(MagentoFrameworkEventObserver $observer) {
  95.  
  96. $vm = array();
  97. $data = $this->request->getPostValue();
  98.  
  99. if (isset($data['product']['media_gallery']['images'])) {
  100. $images = $data['product']['media_gallery']['images'];
  101.  
  102. foreach ($images as $image) {
  103. if (isset($image['vm']) && $image['vm'] == 1) {
  104. $vm[$image['value_id']] = 1;
  105. } else {
  106. $vm[$image['value_id']] = 0;
  107. }
  108. }
  109. // print_r($images);exit;
  110. $connection = $this->resource->getConnection();
  111. $tableName = 'catalog_product_entity_media_gallery'; //gives table name with prefix
  112. $product = $observer->getProduct();
  113. $mediaGallery = $product->getMediaGallery();
  114.  
  115. if (isset($mediaGallery['images'])) {
  116. foreach ($mediaGallery['images'] as $image) {
  117. if (isset($vm[$image['value_id']])) {
  118. //Update Data into table
  119. $sql = "Update " . $tableName . " Set vm = " . $vm[$image['value_id']] . " where value_id = " . $image['value_id'];
  120. $connection->query($sql);
  121. }
  122. }
  123. }
  124. }
  125. }
  126.  
  127. }
  128.  
  129. <?php
  130.  
  131. namespace DcwVmObserver;
  132.  
  133. use MagentoFrameworkEventObserverInterface;
  134.  
  135. class ProductSaveAfter implements ObserverInterface {
  136.  
  137. protected $request;
  138. protected $resource;
  139.  
  140. /**
  141. *
  142. * @param MagentoFrameworkAppRequestInterface $request
  143. * @param MagentoFrameworkAppResourceConnection $resource
  144. */
  145. public function __construct(
  146. MagentoFrameworkAppRequestInterface $request, MagentoFrameworkAppResourceConnection $resource
  147. ) {
  148. $this->request = $request;
  149. $this->resource = $resource;
  150. }
  151.  
  152. public function execute(MagentoFrameworkEventObserver $observer)
  153. {
  154. $data = $this->request->getPostValue();
  155.  
  156. if (isset($data['product']['media_gallery']['images'])) {
  157. // print_r($images);exit;
  158. $connection = $this->resource->getConnection();
  159. $tableName = 'catalog_product_entity_media_gallery'; //gives table name with prefix
  160. $product = $observer->getProduct();
  161. $mediaGallery = $product->getMediaGallery();
  162.  
  163. if (isset($mediaGallery['images'])) {
  164. foreach ($mediaGallery['images'] as $image) {
  165. //Update Data into table
  166. $vmValue = !empty($image['vm']) ? (int)$image['vm'] : 0;
  167. $sql = "UPDATE " . $tableName . " SET vm = " . $vmValue . " WHERE value_id = " . $image['value_id'];
  168. $connection->query($sql);
  169. }
  170. }
  171. }
  172. }
  173.  
  174. }
  175.  
  176. <?xml version="1.0"?>
  177. <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
  178. <type name="MagentoCatalogModelResourceModelProductGallery">
  179. <plugin name="afterCreateBatchBaseSelect" type="DcwVmPluginProductGallery" sortOrder="10" disabled="false"/>
  180. </type>
  181. </config>
  182.  
  183. <?php
  184.  
  185. namespace DcwVmPluginProduct;
  186.  
  187. class Gallery
  188. {
  189. public function afterCreateBatchBaseSelect(
  190. MagentoCatalogModelResourceModelProductGallery $subject,
  191. MagentoFrameworkDBSelect $select
  192. ) {
  193. $select->columns('vm');
  194.  
  195. return $select;
  196. }
  197. }
  198.  
  199. <?xml version="1.0"?>
  200. <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
  201. <type name="MagentoCatalogModelProduct">
  202. <plugin name="afterGetMediaGalleryImages" type="DcwVmPluginProduct" sortOrder="10" disabled="false"/>
  203. </type>
  204. </config>
  205.  
  206. <?php
  207.  
  208. namespace DcwVmPlugin;
  209.  
  210. class Product
  211. {
  212. /**
  213. * @param MagentoCatalogModelProduct $subject
  214. * @param MagentoFrameworkDataCollection $result
  215. * @return mixed
  216. */
  217. public function afterGetMediaGalleryImages(MagentoCatalogModelProduct $subject, $result)
  218. {
  219. foreach ($result as $key => $image) {
  220. if ($image['vm']) {
  221. $result->removeItemByKey($key);
  222. }
  223. }
  224.  
  225. return $result;
  226. }
  227. }
  228.  
  229. $images = [];
  230. foreach ($product->getMediaGalleryImages() as $image) {
  231. if ($image->getVm()) {
  232. $images[] = $image;
  233. }
  234. }
  235.  
  236. $images = [];
  237. foreach ($product->getMediaGalleryImages() as $image) {
  238. if ($image->getVm()) {
  239. $images[] = $image;
  240. }
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement