Monday 30 December 2019

How to solve product changes not reflecting on frontend after updating from admin in Magento 2

Please execute below queries if you are not able to fix the issue using bin/magento indexer:reindex command

Please note : this is only tested for single stores

DELETE FROM catalog_product_entity_text where store_id != 0;
DELETE FROM catalog_product_entity_datetime where store_id != 0;
DELETE FROM catalog_product_entity_decimal where store_id != 0;
DELETE FROM catalog_product_entity_int where store_id != 0;
DELETE FROM catalog_product_entity_varchar where store_id != 0;

Wednesday 25 December 2019

How to get product Review star in phtml in magento 2

<?php

namespace Custom\Cmenu\Block;

use Magento\Catalog\Api\CategoryRepositoryInterface;

class Category extends \Magento\Framework\View\Element\Template
{

    protected $_productRepository;
    protected $_reviewFactory;
    protected $_ratingCollection;


    public function __construct(
        \Magento\Catalog\Model\ProductRepository $productRepository,
        \Magento\Review\Model\ReviewFactory $reviewFactory,
        \Magento\Review\Model\ResourceModel\Review\Summary\Collection $ratingCollection,
        \Magento\Framework\View\Element\Template\Context $context,
        array $data = []
    ) {
        $this->_productRepository = $productRepository;
        $this->_reviewFactory = $reviewFactory;
        $this->_ratingCollection = $ratingCollection;
        parent::__construct($context);
    }


    public function getRatingSummary($product)
    {

       
        //$product = $this->_productloader->create()->load($id); // follow the link for this
       
        $this->_reviewFactory->create()->getEntitySummary($product, 1);
       
        $ratingSummary = $product->getRatingSummary()->getRatingSummary();

        return $ratingSummary;
    }
}

----------------- PHTML page --------------------------------------

    <?php
    $category = $block->getLayout()->createBlock('Custom\Cmenu\Block\Category'); ?>

 <?php
        //pass $product object as argument...
        $_ratingSummary = $block->getRatingSummary($product);
       
    ?>
    <?php if($_ratingSummary){ ?>
    <div class="product-reviews-summary short">
        <div class="rating-summary">
            <div title="<?php echo (int)$_ratingSummary; ?>%" class="rating-result">
                <span style="width:<?php echo (int)$_ratingSummary; ?>%"><span><?php echo (int)$_ratingSummary; ?>%</span></span>
            </div>
        </div>
     
    </div>
    <?php } ?>

Monday 23 December 2019

How to create Multi Website/Stores simlinks in magento 2



1) Create a new folder in magento root and copy the index.php and .htaccess files from magento root to new folder.

2) Edit the index.php which is in new folder

Replace:

$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);

/** @var \Magento\Framework\App\Http $app */

$app = $bootstrap->createApplication('Magento\Framework\App\Http');

$bootstrap->run($app);

Created multi website in magento, Steps to create a multistore in admin panel is same as like in magento1.x. Don't forget to change the base url and secure url for new website/store. Once made changes in admin panel follow the below steps,

1) Create a new folder in magento root and copy the index.php and .htaccess files from magento root to new folder.

2) Edit the index.php which is in new folder

Replace:

$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);

/** @var \Magento\Framework\App\Http $app */

$app = $bootstrap->createApplication('Magento\Framework\App\Http');

$bootstrap->run($app);
With:

$params = $_SERVER;

$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'newstore'; //Webite code as same in admin panel

$params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'website';

$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);

/** @var \Magento\Framework\App\Http $app */

$app = $bootstrap->createApplication('Magento\Framework\App\Http');

$bootstrap->run($app);

And also update bootstrap.php include path as below,

Replace:

require __DIR__ . '/app/bootstrap.php';
With:

require __DIR__ . '/../app/bootstrap.php';


3) Create a simlinks inside the new folder

ln -s from path/app to path/app

ln -s from path/lib to path/lib

ln -s from path/pub to path/pub

ln -s from path/var to path/var

For example /var/www/html/app cnsv/app

Saturday 21 December 2019

Coupon is always re-added during checkout in magento 1.9


I encountered the following bug

Add a product to cart
Apply a coupon code
Remove coupon code
Coupon code is not shown anymore / remove successful
Go in checkout
Continue billing and shipping step
Go back in cart
Coupon code of step #2 is active again
magento magento-1.9


I solved it by event/observer

<events>
        <controller_action_predispatch_checkout_cart_couponPost>
            <observers>
                <remove_session_coupon_code>
                    <type>singleton</type>
                    <class>yourmodule/observer</class>
                    <method>removeCoupon</method>
                </remove_session_coupon_code>
            </observers>
        </controller_action_predispatch_checkout_cart_couponPost>
    </events>


public function removeCoupon(Varien_Event_Observer $observer)
{
    $controller = $observer->getControllerAction();
    if ($controller->getRequest()->getParam('remove') == 1) {
        Mage::getSingleton("checkout/session")->unsetData('cart_coupon_code');
    }
    return $this;
}

Tuesday 17 December 2019

how to connect ssh by pem file aws server

1. Requirement : .ppk or .pem key file
2. If you have .ppk file. It is required to change it to .pem file as it is the key format supported by Linux.
You need putty tools for generating .pem file from .ppk.
Install putty tools : sudo apt-get install putty-tools
Go to the directory of the .ppk file and run the following command : puttygen [filename].ppk -O private-openssh -o [filename].pem
3. Now you have the .pem file. It is needed to be added to the linux known hosts.
Run the following command : ssh-add [filename].pem
4. Now connect to AWS using linux in built 'Connect to Server' -> Type(SSH). You don't need to add password.

Friday 6 December 2019

How to show address field in registration form in magento 2?

Hi first find magento root folder then vendor\magento\module-customer\view\frontend\layout customer_account_create.xml.
Then copy the file and related theme folder. For example path /app/design/frontend/vendor name /Theme Name/Magento_Customer/layout/customer_account_create.xml
then go to line no 13 check this code and put billow section on line no


 <referenceContainer name="content">
            <block class="Magento\Customer\Block\Form\Register" name="customer_form_register" template="form/register.phtml">
                <container name="form.additional.info" as="form_additional_info"/>
                <container name="customer.form.register.fields.before" as="form_fields_before" label="Form Fields Before" htmlTag="div" htmlClass="customer-form-before"/>
                <action method="setShowAddressFields">
                    <argument name="show.address.fields" xsi:type="boolean">true</argument>
                </action>
            </block>
            <block class="Magento\Cookie\Block\RequireCookie" name="require-cookie" template="Magento_Cookie::require_cookie.phtml">
                <arguments>
                    <argument name="triggers" xsi:type="array">
                        <item name="registerSubmitButton" xsi:type="string">.action.submit</item>
                    </argument>
                </arguments>
            </block>
        </referenceContainer>

Monday 2 December 2019

Function mcrypt_module_open() is deprecated in Magento 2

Function mcrypt_module_open() is deprecated from PHP version 7.1. If you are using PHP 7.1 or higher for your Magento 2 website then you might face this error.
NOTE: This error is already solved by Magento in 2.2 and higher version.

Solution: Add @ sign before all below functions.

Below is one example.
$this->_handle = mcrypt_module_open($cipher, '', $mode, '');
Becomes
$this->_handle = @mcrypt_module_open($cipher, '', $mode, '');