Monday 13 August 2018

Magento 2 Hide Payment Method from Front-end.

I have created module to Disable Payment Method from Front-end. So that payment method will be active for admin users only.

For that I have also give enable/disable configuration field in admin.

Frist, let's create module.. This is very basic and very simple module. Just create the module and install it on your store.


1) Create 1. registrations.php file in the root of your module..
So, create app/code/Gopal/DisablePaymentInFront/registration.php

 <?php  
 \Magento\Framework\Component\ComponentRegistrar::register(  
   \Magento\Framework\Component\ComponentRegistrar::MODULE,  
   'Gopal_DisablePaymentInFront',  
   __DIR__  
 );  
 ?>  

2) Create module.xml file to enable module and add dependency to the payment module.
So, create app/code/Gopal/DisablePaymentInFront/etc/module.xml

 <?xml version="1.0"?>  
 <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">  
   <module name="Gopal_DisablePaymentInFront" setup_version="2.0.1">  
     <sequence>  
       <module name="Magento_Payment"/>  
     </sequence>  
   </module>  
 </config>  

3) Now create di.xml to add dependency.
We are creating Plugin method of the magento2. Plugin is very good feature included in the Magento 2.
So, create, app/code/Gopal/DisablePaymentInFront/etc/di.xml


 <?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="Magento\Payment\Model\Method\AbstractMethod">  
     <plugin name="disable_payment_method_in_frontend" type="Gopal\DisablePaymentInFront\Plugin\DisablePaymentInFront" />  
   </type>  
 </config>  

4) We are adding system configuration field to enable or disable this feature for particular payment method.
There will be Disable in Frontend option to disable payment method in frontend.
So Create, app/code/Gopal/DisablePaymentInFront/etc/adminhtml/system.xml
 <?xml version="1.0"?>  
 <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">  
   <system>  
     <section id="payment" translate="label" type="text" sortOrder="400" showInDefault="1" showInWebsite="1" showInStore="1">  
       <group id="checkmo" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">  
         <field id="disable_in_front" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0" canRestore="1">  
           <label>Disable in Frontend</label>  
           <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>  
         </field>  
       </group>  
       <group id="purchaseorder" translate="label" type="text" sortOrder="32" showInDefault="1" showInWebsite="1" showInStore="1">  
         <field id="disable_in_front" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0" canRestore="1">  
           <label>Disable in Frontend</label>  
           <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>  
         </field>  
       </group>  
       <group id="banktransfer" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">  
         <field id="disable_in_front" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0" canRestore="1">  
           <label>Disable in Frontend</label>  
           <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>  
         </field>  
       </group>  
       <group id="cashondelivery" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">  
         <field id="disable_in_front" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0" canRestore="1">  
           <label>Disable in Frontend</label>  
           <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>  
         </field>  
       </group>  
     </section>  
   </system>  
 </config>  

5) Now Create Plugin to add logic to disable any payment method for frontend as per configured in system configuration.

app/code/Gopal/DisablePaymentInFront/Plugin/DisablePaymentInFront.php
 <?php  
 namespace Gopal\DisablePaymentInFront\Plugin;  
 class DisablePaymentInFront  
 {  
      const CHECKMO = \Magento\OfflinePayments\Model\Checkmo::PAYMENT_METHOD_CHECKMO_CODE;  
      const CASH_ON_DELIVERY = \Magento\OfflinePayments\Model\Cashondelivery::PAYMENT_METHOD_CASHONDELIVERY_CODE;  
      const PURCHASE_ORDER = \Magento\OfflinePayments\Model\Purchaseorder::PAYMENT_METHOD_PURCHASEORDER_CODE;  
      const BANK_TRANSFER = \Magento\OfflinePayments\Model\Banktransfer::PAYMENT_METHOD_BANKTRANSFER_CODE;  
   /**  
    * @var \Magento\Framework\App\State  
    */  
   private $appState;  
   /**  
    * @var \Magento\Framework\App\Config\ScopeConfigInterface  
    */  
   private $scopeConfig;  
   /**  
    * @param \Magento\Framework\App\State $appState  
    */  
   public function __construct(  
     \Magento\Framework\App\State $appState,  
     \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig  
   )  
   {  
     $this->appState = $appState;  
     $this->scopeConfig = $scopeConfig;  
   }  
   /**  
    * @param \Magento\Payment\Model\Method\AbstractMethod $subject  
    * @param $result  
    * @return bool  
    * @throws \Magento\Framework\Exception\LocalizedException  
    */  
   public function afterIsAvailable(\Magento\Payment\Model\Method\AbstractMethod $subject, $result)  
   {  
     $area_code = $this->appState->getAreaCode();  
     if ($area_code != \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE) {  
       if ($this->checkForCheckMo($subject, self::CHECKMO)  
         || $this->checkForCheckMo($subject, self::CASH_ON_DELIVERY)  
         || $this->checkForCheckMo($subject, self::PURCHASE_ORDER)  
         || $this->checkForCheckMo($subject, self::BANK_TRANSFER)) {  
         return false;  
       }  
     }  
     return $result;  
   }  
   /**  
    * @param $subject  
    * @param $method  
    * @return bool|mixed  
    */  
   private function checkForCheckMo($subject, $method)  
   {  
     if ($subject->getCode() == $method) {  
       $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;  
       return $this->scopeConfig->getValue('payment/' . $method . '/disable_in_front', $storeScope);  
     }  
     return false;  
   }  
 }  


Go to Admin -> Stores -> Configuration -> Sales -> Payment Method.
Then go to particular payment method and
Select "Disable in Frontend" =>  "Yes"
Then Clear cache and generation as par Magento2.
Check admin and fronend, You can see that payment method should be disabled in frontend and it is showing in admin.

This method will be useful for some payment method which is only needed to admin user.

Wednesday 1 August 2018

Enable Price field of configurable product in admin in Magento 2.

As we can see Magento2 is much more depends on KO(Knockout JS).

Magento 2 don't allow to update price from admin for the configurable product.

Once configurable product is created from admin, price field is disabled.

To enable price field, I have created module and changed small code.

I don't know this is the proper method or not, but that is working for me.


1. Create a PHP file: app/code/Gopal/ConfigurablePrice/registration.php
 <?php  
 \Magento\Framework\Component\ComponentRegistrar::register(  
   \Magento\Framework\Component\ComponentRegistrar::MODULE,  
   'Gopal_ConfigurablePrice',  
   __DIR__  
 );  

2. Create a XML file: app/code/Gopal/ConfigurablePrice/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Gopal_ConfigurablePrice" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Catalog" />
            <module name="Magento_ConfigurableProduct" />
        </sequence>
    </module>
</config>


3. Create a XML file: app/code/Gopal/ConfigurablePrice/etc/di.xml
<?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="Magento\ConfigurableProduct\Ui\DataProvider\Product\Form\Modifier\ConfigurablePrice">
        <plugin name="enable_price_configurable_product" type="Gopal\ConfigurablePrice\Plugin\DataProvider\Product\Form\Modifier\ConfigurablePrice" sortOrder="20" disabled="false"/>
    </type>
</config>


4. Create a file: app/code/Gopal/ConfigurablePrice/Plugin/DataProvider/Product/Form/Modifier/ConfigurablePrice.php
<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Gopal\ConfigurablePrice\Plugin\DataProvider\Product\Form\Modifier;

use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AbstractModifier;
use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Catalog\Model\Locator\LocatorInterface;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable as ConfigurableType;
use Magento\ConfigurableProduct\Ui\DataProvider\Product\Form\Modifier\ConfigurablePrice as ConfigurablePriceCore;
/**
 * Data provider for price in the Configurable products
 */
class ConfigurablePrice extends ConfigurablePriceCore
{
    /**
     * {@inheritdoc}
     *
     * Added after listener for make price field editable in configurable produc edit page.
     */
    public function afterModifyMeta($subject, $result)
    {
        $groupCode = $this->getGroupCodeByField($result, ProductAttributeInterface::CODE_PRICE)
            ?: $this->getGroupCodeByField($result, ConfigurablePriceCore::CODE_GROUP_PRICE);

        if (!empty($result[$groupCode]['children'][ConfigurablePriceCore::CODE_GROUP_PRICE])) {
            $result[$groupCode]['children'][ConfigurablePriceCore::CODE_GROUP_PRICE]['children']['price']['arguments']['data']['config']['imports']['disabled'] = 0;
        }

        return $result;
    }
}


Now, please run below commands
php bin/magento module:enable Gopal_ConfigurablePrice
php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f


Now price field is must be enabled: Check screenshot of the configuable product page in admin.


If above link is useful to you then please accept answer at: https://magento.stackexchange.com/questions/171452/magento-2-edit-price-field-in-configurable-product-in-admin-panel



Thursday 4 May 2017

SQLSTATE [HY000]: General error: 145 Table catalogsearch_fulltext is marked as crashed in Magento

I was working on magento 1 project and when I tried to save product from admin, I faced one issue while save.

I got error:

SQLSTATE[HY000]: General error: 145 Table catalogsearch_fulltext is marked as crashed and should be repaired.


This error was because of database table crash and it can be solve by table repair.

Database table can be repair by 2 methods:
1. Repair table from phpmyadmin.
-> Log in to cpanel.
-> Select checkbox before table catalogsearch_fulltext.
-> Go to bottom of the page, there will be drop down. select "Repair Table".
-> You are done.

2. You can repair table by ssh command:
REPAIR TABLE catalogsearch_fulltext;


After table I have checked product save from admin. it works smoothly.

Sunday 8 July 2012

How to set different layout in product page for specific category in Magento

In magento by default there is same layout in all product page for all category.

But if you want to give different layout to product view page for specific category then here is code for it.

But you have to create view.phtml with different layout design which you want in specific ategory.
Suppose i  created file with name viewbulkflowers.phtml

You have to write this code in admin side. So first open your admin.
First go to
Catalog / Manage Category
=>Select specefic category
=>Select Custom Design tab from right side
=>Make Yes to Apply to Products  
=>Add below xml code to Custom Layout Update.

=>XML Code

<reference name="product.info">
    <action method="setTemplate">
        <template>catalog/product/viewbulkflowers.phtml</template>
    </action>
    <block type="catalog/product_view" name="product.info" template="catalog/product/viewbulkflowers.phtml" />
</reference>

Now save category.