Thursday, 3 November 2016

Magento2 Skin url replaced in magento2


 Template files
in magento 1
$this->getSkinUrl()
 
Replace with in magento2
$this->getViewFileUrl()
 
CMS page or blocks

in magento 1
{{skin url=''}}
 
Replace with in magento2
{{view url=''}}

Magento2: getting Fatal error: Allowed memory size of error when tried to do mass status change action in magento from admin catalog grid


I was surprised to see core magento error in admin when i tried to attempt to change 100 products of the current status to other
ofcourse my catalog was aroung 7lakh items.
I and My team mate got the fix for this
Error is:
Fatal error: Allowed memory size of 805306368 bytes exhausted (tried to allocate 24 bytes) in G:\xampp\htdocs\mage-ee206\vendor\magento\zendframework1\library\Zend\Db\Statement\Pdo.php on line 291


Fix we found here is 
vendor/magento/module-catalog/Controller/Adminhtml/Product/MassStatus.php

====
/**
     * Update product(s) status action
     *
     * @return \Magento\Backend\Model\View\Result\Redirect
     */
    public function execute()
    {
        $collection = $this->filter->getCollection($this->collectionFactory->create());
        $productIds = $collection->getAllIds();
        $storeId = (int) $this->getRequest()->getParam('store', 0);

        $status = (int) $this->getRequest()->getParam('status');

---------------
We changed to 
We extended the code module to our name space to extend core controller

/**
     * Update product(s) status action
     *
     * @return \Magento\Backend\Model\View\Result\Redirect
     */
    public function execute()
    {
    $productIds = $this->getRequest()->getPostValue("selected");
        $storeId = (int) $this->getRequest()->getParam('store', 0);
        $status = (int) $this->getRequest()->getParam('status');

==============
Now we could able to do Mass status change action without fail.

I hope it may help you.

Note: if you face the same error for Mass delete action also, you can try this. i have not tested that.
i will post once i tested it.

Cheers:)

Best way to loop/iterate the catalog collection in Magento2

When i was doing mass status change in magento
I was facing with memory allocation in my localshot xammp
Total products i have around 7lakh in the admin grid. i tried to change the status about to 100 products,

i got this error

Fatal error: Allowed memory size of 805306368 bytes exhausted (tried to allocate 24 bytes) in C:\xampp\htdocs\mage-ee206\vendor\magento\zendframework1\library\Zend\Db\Statement\Pdo.php on line 291

==============
After spending one day time i got the solution

In one of my third party i found a iteration of products using foreach.. when i changed foreach to while it worked fine for me
i have tested for 30,50,100,500,700 products to changes the status and it was fine for me

I hope this may help you
original code
  $Prods = $this->_productFactory->create()->getCollection()->addIdFilter(<prodids>);
            $Prods->addAttributeToSelect(['status','<status>']);          
         
            foreach ($Prods as $prod) {
           
            $prodid = $prod->getId();
            $prod_status = $prod->getData('status');
=======================
Changed to 

$Prods = $this->_productFactory->create()->getCollection()->addIdFilter(<prodids>);
            $Prods->addAttributeToSelect(['status','<status>']);
            
            while ($prod = $Prods->fetchItem()) {            
            $prodid = $prod->getId();
            $prod_status = $prod->getStatus();
============

Tuesday, 1 November 2016

Magento2: after installation logo is not loading and admin is not loading in windows + xamppp

Lot of time i faced this issue
when tried load home page after installation magento2 i cant see the magento2 logo and admin is not loading properly
the quick fix is
find this
<item name="view_preprocessed" xsi:type="object">Magento\Framework\App\View\Asset\MaterializationStrategy\Symlink</item>

replace with below and refresh the page, it should work now.

<item name="view_preprocessed" xsi:type="object">Magento\Framework\App\View\Asset\MaterializationStrategy\Copy</item>

Magento2 : getting store configured timezone date time

Hello Everyone here is the code to get store configured date time

$timezone = $this->_objectManager->create(
            '\Magento\Framework\Stdlib\DateTime\TimezoneInterface'
            );

strftime('%Y-%m-%d %H:%M:%S', $timezone->scopeTimeStamp())