Monday, January 30, 2017

How do we get default placeholder image url in template file in magento 2

Hi guys,

Today we are going to see how we can get the default placeholder image url in magento 2. There comes many situation where we need to get the default placeholder image of the product. So below is how we can achieve that.

If you check your store settings you will find option of Product Image place holders, you can change your default placeholder from there.

Stores > Configuration > Catalog > Catalog > Product Image Placeholders

The better approach for achieving this would be calling a function on the template file which is defined on your block and performing the operation on that block file. We can simply do that as follows:

On you block file add the code:


protected $_storeConfig;

protected $_storeManager;

public function __construct
(
    ---
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
    \Magento\Store\Model\StoreManager $storeManager
    ---

){
    ---
    $this->_storeConfig = $scopeConfig;
    $this->_storeManager = $storeManager;
    ---
}

public function getPlaceholderImage(){
//getting the media base url
 $mediaBaseUrl = $this ->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA );
 $placeholderPath = $this->_storeConfig->getValue('catalog/placeholder/image_placeholder');//Base Image
//full url of the placeholder image
 $fullUrl = $mediaBaseUrl.'catalog/product/placeholder/'.$placeholderPath;

 return $fullUrl;
    
}

Now in your template file call:


$block->getPlaceholderImage();

That's it. Above mentioned approach is the best way of getting the image URL, however you can also do it the dirty way i.e using object manager. In this method you don't have to use the dependency injection method on the constructor, All the classes could be instantiated on the template file.

Hope this will help.



Monday, March 14, 2016

Displaying all the products with special price including layered navigation and toolbar in product listing page Magento


This post provides the most easy way to get the on sale products with the design of product listing page which includes layered navigation and list toolbar too.

This is the common requirement on most of the magento stores and you may have spent lot of your time roaming around the internet for a working solution and I think you may not have found the way to achieve your requirement. Most of the tutorials gives the way to list the sale products but that doesn't include the layered navigation which looks little off design.

So don't worry you have come to the right place. Below are the easy steps which you help you get the sale products on the product listing page.

Step 1: Create a category for on sale. Let's name it "Specials". Choose is anchor to "Yes".
Step 2: Now after saving it. You can see the category id aside of category name.Note that category id we will use it later.
Step 3: Now copy /app/code/core/Mage/Catalog/Model/Layer.php to /app/code/local/Mage/Catalog/Model/Layer.php .Here we are overwriting the layer.php file.
Go to line# 97

Replace following function code

public function getProductCollection()
{
if (isset($this->_productCollections[$this->getCurrentCategory()->getId()])) {
$collection = $this->_productCollections[$this->getCurrentCategory()->getId()];
} else {
$collection = $this->getCurrentCategory()->getProductCollection();
$this->prepareProductCollection($collection);
$this->_productCollections[$this->getCurrentCategory()->getId()] = $collection;
}
return $collection;
}

with

public function getProductCollection()
    {
        if (isset($this->_productCollections[$this->getCurrentCategory()->getId()]))
        {
        $collection = $this->_productCollections[$this->getCurrentCategory()->getId()];
        }
        else
        {
            //21 is my category id replace it with your category id that you noted previously.
            if($this->getCurrentCategory()->getId() == '21')
            {
            $collection = Mage::getResourceModel('catalog/product_collection');
            $todayDate = date('m/d/y');
            $tomorrow = mktime(0, 0, 0, date('m'), date('d'), date('y'));
            $tomorrowDate = date('m/d/y', $tomorrow);

            $collection->addAttributeToFilter('special_from_date', array('date' => true, 'to' => $todayDate))
            ->addAttributeToFilter('special_to_date', array('or'=> array(
            0 => array('date' => true, 'from' => $tomorrowDate),
            1 => array('is' => new Zend_Db_Expr('null')))
            ), 'left');

            $this->prepareProductCollection($collection);
            $this->_productCollections[$this->getCurrentCategory()->getId()] = $collection;
            //custom code end
            }
            else
            {

                $collection = $this->getCurrentCategory()->getProductCollection();
                $this->prepareProductCollection($collection);
                $this->_productCollections[$this->getCurrentCategory()->getId()] = $collection;
            }

        }
         return $collection;
    }

save it. And that's all. you just need to change the category id according to your requirement.

That's it.Now you can get the listing of products with only special price.

Happy Coding!!

Monday, March 30, 2015

Integratign Fedex in your Magento store

Hello guys,
In the phase of developing Magento websites we may come to a point where we need to integrate Fedex shipping method in our magento store.

But this may be very irritating sometimes if it doesn't work as we expected.Here i will discuss some points that are not discussed elsewhere but is very important and pretty common mistakes that we do.

There are few documentation and support on this topic, and most of them doesn't discuss the problems that occured during this integration. I too had an hair pulling experience while integrating Fedex due to the lack of proper documentation and support.I hope this will not be the case for you.

The main problem you may be facing while integrating fedex is the Fedex shipping method doesn't shows up  during checkout or while getting the shipping qoutes.

Below is the overall steps discussed point wise:
  • First thing you need to do is to create a developer account for fedex to get the test credentials. There are many tutorial you can get where there is detail discussion on this topic, you can follow this link.
  • There are four thing you get after registering for test credentials and is required they are:
    • Access key
    • Account Id
    • Password
    • Meter number
  • Enter these values in your Fedex shipping setting tab in your admin panel of your magento store.
  • The other things that should be considered is enabling the sandbox mode by selecting Sandbox Mode to Yes and Select the weight to Pounds.
These are the default setup check whether the shipping method appears on the checkout process.If it shows up than believe me you are lucky that it works at first shot.

If it doesn't shows up don't worry, now you need to verify other things.
 Before proceeding i want to explain the basic process of Fedex. What does actually fedex do to get the shipping rates? While magento collects all the shipping methods, the Fedex module sends some information to the fedex to get back the rates. These information that fedex module sends are
  • the keys that i mentioned above
  • Shipping origin address i.e the sellers address
  • Customer address i.e the address of the customer or the shipping address that customer enters while checkout process.
  • This address consist of country code and postcode. 
Now by knowing this much of information, you know what things are necessary to get the fedex rates.Most of us are making mistakes is entering the invalid address and some of us even forget to add the shipping origin address in our magento store.
  • So check whether the shipping origin address is entered correctly in your admin configuration.
If these are set correctly but you still didn't see the shipping method than you may have done one mistake while getting the test credential.

If you are trying to use domestic shipping outside United States, for example inside UK i.e your shipping origin address and customer shipping address both are of same coutry outside of US.If this is your case than you have get seperate credential for these case.While getting the credentials you may have to select the countries option where you may have choose US and Global in default. Here you have to select the particular country to get the credentials. For international shipping and domestic inside US selecting US and Global  is fine.

You may be thinking this is very tedious process but this is only for test purpose, when you move to production environment single credential work for all the cases.So don't worry about it.

After verifying this check once again you may be good to go.

Hope this will help you understand the process of integrating Fedex with magento. I will discuss on ho to move from test environment to production environment on my next post. So keep following and at last Happy Coding!! 

Saturday, November 22, 2014

Easy tips if overriding the core magento Blocks, Models or Controller through rewrite doesnot work.

In this tutorial i will go through the process of overwriting the magneto core and community files to your local folder.
As we all know it is the first thing that we all learn while working in magento is "Don't Ever Modify The Core Files". This statement is very true because it creates problem if we need to upgrade to another version and also to find out the modified files. That's why we override the core modules to our local files.Some times we need to override the modules from Community folder too.

We can override the modules block or models or controller by rewriting it config.xml file in our local module and then apply our changes there. But overriding the files using rewrite is not always easy.There may occur some problems and you may not be able to override those files.So what could we possibly do in these cases?
I also have faced this problems sometimes, when you think all your configuration is correct but the files in not overridden.
The solution i am going to tell is not the best way of doing it but it works and doesn't create other issues.
The solution is to make the same directory structure in your "local" folder as the files that you want to override and paste that file there. For example if you want to override "app/code/core/mage/adminhtml/block/customer/grid.php", you just need to create "app/code/local/mage/adminhtml/block/customer/grid.php".

And in this file you can make the changes.No need to do any other changes.

Now after that i have told the solution i want to tell what is going on. Actually we aren't overriding the file we are overwriting it.To understand this first you need to understand how magento reads the file.I will not explain the whole flow but for now it is that at first it searches file at "local" folder than "community" and at last in "core" folder.So if you create the same folder structure magento first look to your local folder and it finds it and execute it.
The problem with overwriting is you need to copy the whole file because inside it there may be many functions and if you delete any one function from there than magento will not look for that function in core and will display error. But overriding is a different case if you override the file than you can only add or modify those functions in your overridden file and no need to copy other unrelated functions in your file.

Now from above tutorial you will be able to overwrite the magento files and another thing is you may have understand the difference between overriding and overwriting.