dbm 5 anos atrás
pai
commit
153d4c2e15

+ 19 - 15
ShippingNotifications/Block/ShippingCategory.php

@@ -3,40 +3,34 @@
 namespace Cpcommerce\ShippingNotifications\Block;
 
 use Magento\Catalog\Block\Product\View\Description;
-use Magento\Catalog\Model\Product\Attribute\Repository;
 use Magento\Framework\View\Element\Template;
 use Magento\Framework\View\Element\Template\Context;
-use Cpcommerce\ShippingNotifications\Model\ShippingCategory AS DelayedShippingCategory;
-use Cpcommerce\ShippingNotifications\Model\ResourceModel\ShippingCategory\Collection;
-use Magento\Framework\App\ResourceConnection;
 use \Magento\Framework\App\Config\ScopeConfigInterface;
 
 class ShippingCategory extends Template
 {
-    protected $resource;
+    private $description;
+    private $context;
+    private $data;
+    private $scopeConfig;
 
     public function __construct(
         Description $description,
-        Repository $attribute,
         Context $context,
         array $data = [],
-        DelayedShippingCategory $delayedShippingCategory,
-        Collection $delayedCollection,
-        ResourceConnection $resource,
         ScopeConfigInterface $scopeConfig
     ) {
         $this->description = $description;
-        $this->attribute = $attribute;
-        $this->delayedShippingCategory = $delayedShippingCategory;
-        $this->delayedCollection = $delayedCollection;
-        $this->resource = $resource;
         $this->scopeConfig = $scopeConfig;
         return parent::__construct($context, $data);
     }
 
     public function getShippingCategory(){
         $_product = $this->description->getProduct();
-        $option_id = $_product->getShippingCategory();
+
+        $attributeCode = $this->scopeConfig->getValue('cpcommerce_shipping_notifications/general/attribute',\Magento\Store\Model\ScopeInterface::SCOPE_STORE);
+
+        $option_id = $_product->getData($attributeCode);
 
         return $option_id;
     }
@@ -49,8 +43,18 @@ class ShippingCategory extends Template
             $delayedShipCategsArray = explode(",",$delayedShipCategs);
             return $delayedShipCategsArray;
         }
-        
+
         return null;
     }
 
+    public function getBlock(){
+        $notifBlock = $this->scopeConfig->getValue('cpcommerce_shipping_notifications/general/shipping_notification_block',\Magento\Store\Model\ScopeInterface::SCOPE_STORE);
+
+        if(!isset($notifBlock)){
+            return null;
+        }
+
+        return $notifBlock;
+    }
+
 }

+ 39 - 0
ShippingNotifications/Model/Config/Source/Attributes.php

@@ -0,0 +1,39 @@
+<?php
+
+namespace Cpcommerce\ShippingNotifications\Model\Config\Source;
+
+use Magento\Framework\Data\OptionSourceInterface;
+use Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory;
+
+class Attributes implements OptionSourceInterface{
+    
+    public function __construct(
+        CollectionFactory $collectionFactory
+    ){
+        $this->collectionFactory = $collectionFactory;
+    }
+    
+    public function toOptionArray()
+    {
+        $options = [];
+        foreach($this->getOptions() as $optionValue => $optionLabel){
+            $options[] = ['value' => $optionValue, 'label' => $optionLabel];
+        }
+
+        return $options;
+    }
+
+    public function getOptions()
+    {
+        $collection = $this->collectionFactory->create();
+        $collection->addFieldToFilter('main_table.frontend_input', 'select');
+        $collection->addOrder('attribute_code', 'asc');
+
+        $options = ['' => _('-- Empty --')];
+        foreach($collection->getItems() as $attribute){
+            $options[$attribute->getAttributeCode()] = $attribute->getAttributeCode();
+        }
+
+        return $options;
+    }
+}

+ 10 - 2
ShippingNotifications/Model/Config/Source/ShippingCategories.php

@@ -4,20 +4,28 @@ namespace Cpcommerce\ShippingNotifications\Model\Config\Source;
 
 use Magento\Framework\Data\OptionSourceInterface;
 use Magento\Catalog\Model\Product\Attribute\Repository;
+use \Magento\Framework\App\Config\ScopeConfigInterface;
 
 class ShippingCategories implements OptionSourceInterface
 {
+    private $attribute;
+    private $scopeConfig;
 
     public function __construct(
-        Repository $attribute
+        Repository $attribute,
+        ScopeConfigInterface $scopeConfig
     ){
         $this->attribute = $attribute;
+        $this->scopeConfig = $scopeConfig;
     }
 
     public function toOptionArray()
     {
         $options = [];
-        $shippingCategories = $this->attribute->get('shipping_category')->getOptions();
+
+        $attributeCode = $this->scopeConfig->getValue('cpcommerce_shipping_notifications/general/attribute',\Magento\Store\Model\ScopeInterface::SCOPE_STORE);
+
+        $shippingCategories = $this->attribute->get($attributeCode)->getOptions();
 
         foreach($shippingCategories AS $shippingCategory){
             $options[] = [

+ 12 - 3
ShippingNotifications/etc/adminhtml/system.xml

@@ -6,9 +6,18 @@
             <label>Shipping Notification</label>
             <tab>cpcommerce</tab>
             <resource>Cpcommerce_ShippingNotifications::config</resource>
-            <group id="general" translate="label" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
-                <field id="shipping_categories" translate="label" type="multiselect" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
-                    <label>Shipping Categories</label>
+            <group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">              
+                <field id="attribute" translate="label" type="select" sortOrder="0" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
+                    <label>Attribute</label>
+                    <source_model>Cpcommerce\ShippingNotifications\Model\Config\Source\Attributes</source_model>
+                    <can_be_empty>0</can_be_empty>
+                </field>
+                <field id="shipping_notification_block" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
+                    <label>Notification Block</label>
+                    <can_be_empty>0</can_be_empty>
+                </field>
+                <field id="shipping_categories" translate="label" type="multiselect" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
+                    <label>Delayed Shipping Categories</label>
                     <source_model>Cpcommerce\ShippingNotifications\Model\Config\Source\ShippingCategories</source_model>
                     <can_be_empty>1</can_be_empty>
                 </field>

+ 14 - 0
ShippingNotifications/view/frontend/layout/catalog_product_view_type_configurable.xml

@@ -0,0 +1,14 @@
+<?xml version="1.0"?>
+<!--
+/**
+ * Copyright © Magento, Inc. All rights reserved.
+ * See COPYING.txt for license details.
+ */
+-->
+<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
+    <body>
+        <referenceContainer name="product.info.options.wrapper">
+            <block class="Cpcommerce\ShippingNotifications\Block\ShippingCategory" name="shipping_notification" before="addtocart" template="Cpcommerce_ShippingNotifications::shipping_notification.phtml"/>
+        </referenceContainer>
+    </body>
+</page>

+ 14 - 0
ShippingNotifications/view/frontend/layout/catalog_product_view_type_grouped.xml

@@ -0,0 +1,14 @@
+<?xml version="1.0"?>
+<!--
+/**
+ * Copyright © Magento, Inc. All rights reserved.
+ * See COPYING.txt for license details.
+ */
+-->
+<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
+    <body>
+        <referenceContainer name="product.info.form.content">
+            <block class="Cpcommerce\ShippingNotifications\Block\ShippingCategory" name="shipping_notification" before="addtocart" template="Cpcommerce_ShippingNotifications::shipping_notification.phtml"/>
+        </referenceContainer>
+    </body>
+</page>

+ 0 - 8
ShippingNotifications/view/frontend/layout/catalog_product_view_type_simple.xml

@@ -4,13 +4,5 @@
     <referenceContainer name="product.info.form.content">
         <block class="Cpcommerce\ShippingNotifications\Block\ShippingCategory" name="shipping_notification" before="addtocart" template="Cpcommerce_ShippingNotifications::shipping_notification.phtml"/>
     </referenceContainer>
-
-    <referenceBlock name="shipping_notification">
-        <block class="Magento\Cms\Block\Block" name="cpcommerce_shipping_notification">
-            <arguments>
-                <argument name="block_id" xsi:type="string">cpcommerce_shipping_notification</argument>
-            </arguments>
-        </block>
-    </referenceBlock>
 </body>
 </page>

+ 4 - 1
ShippingNotifications/view/frontend/templates/shipping_notification.phtml

@@ -1,11 +1,14 @@
 <?php
     $shipping_category = $block->getShippingCategory();
     $delayedCategs = $block->getDelayedShippingCategories();  
+    $finalBlock = $this->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId($block->getBlock());
 
     if($delayedCategs){
         foreach($delayedCategs as $categ){
             if($shipping_category == $categ){
-                echo $block->getChildHtml('cpcommerce_shipping_notification');
+                if($finalBlock){
+                    echo $finalBlock->toHtml();
+                }
                 break;
             }
         }