Monday, January 25, 2016

Add external lib to vendor Magento 2

Magento 2 use autoload implement Code style PSR-4


You can add any custom source lib much easier by add require autoload

1. Upload lib files to vendor dir

2. Add file autoload.php in /vendor/lib-folder/autoload.php with content
define('FACEBOOK_SDK_V4_SRC_DIR', '/src/Facebook/');
require __DIR__ . '/src/Facebook/autoload.php';
3. Save and now you can call lib from anywhere
like this
---- Injection from constructor and call instance in class
\Facebook\Facebook

Saturday, January 23, 2016

How to load Javascript scripts in Magento 2

All assets file relate in frontend will be store in view/web of module structure



Require lib from requirejs-config.js file

var config = {
    map: {
        "*":{
            "lightbox": "VES_Sociallogin/js/lightbox/lightbox.min" //This related to file lightbox/lightbox.min.js
        }
    }

};

Order to call light box js


In any js files you can call to like this. You should use define instead of require in stituation

define([
    "jquery",
    "jquery/ui",
    "lightbox",
    'Magento_Ui/js/modal/alert',
    'uiComponent'
], function($, lightbox, alert, uiComponent){
    "use strict";
    $(window).ready(function(){
        //console.log('hello magento2! File js edited');
        DO ANY CODE YOU WANT HERE

    });

});

Execute js in template mangeto can be done with script tag
script tag with type="text/x-magento-init"
    {
        "*":{
            "VES_Sociallogin/js/lightbox/lightbox":{ //This way like that way do by requireJS-config

            }
        }
    }


    require([
        'PopupLogin',
        'prototype'
    ], function(LoginPopup, prototype){
        Event.observe(window, 'load', function() {
           
         return 
        });
    });

To apply changed. You should delete static file in pub/static//Magento//en_US//




Thursday, January 7, 2016

Snippets for Magento 2 Developers

1 - Get ObjectManager Instance
$objectManager      = \Magento\Framework\App\ObjectManager::getInstance();
$customerCollection = $objectManager->create('Magento\Customer\Model\CollectionFactory')->getCollection();

2 - Get BaseUrl
//inject \Magento\Store\Model\StoreManagerInterface $storeManager in constructor
$this->_storeManager = $storeManager;

$url = $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA); //url in media directory

3 - Get Session Customer
//inject \Magento\Customer\Model\Session $customerSession in constructor
$this->_session = $customerSession;


4 - Get Featured Products Collection
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); //dirty way
$productCollection = $objectManager->create('Magento\Catalog\Model\ProductFactory')->getCollection();
//filters
$featuredProducts = $productCollection
                ->addAttributeToSelect('name')
                ->addAttributeToSelect('image')
                ->addAttributeToSelect('price')
                ->addAttributeToFilter('is_featured', 1)
                ->addAttributeToFilter('status', 1)
                ->setPageSize($productCollection->getSize())
                ->setCurPage(1)
                ->load();


5 - Insert Image in Static block or cms page
{{view url="images/image-1.jpg"}} //Url: app/design/VendorName/ThemeName/web/images/image-1.jpg

6 - Call Block in template
$block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('block_id')->toHtml();

7 - Call Block in CMS Page
{{block class="Magento\Cms\Block\Block" block_id="block_id"}}

8 - View media
{{view url=Magento_Catalog::images/logo.png}} //in this case, path: pub/static/frontend/Magento/blank/en_US/Magento_Catalog/images/logo.png 

{{media url=test/logo.svg}}" // path: pub/media/test/logo.svg

{{store url="translation/ajax/index"}} // this tag similar like in Magento1x. Url from your current store

9 - Get Media URL
$mediaDirectory = $this->_objectManager->get('Magento\Framework\Filesystem')->getDirectoryRead(DirectoryList::MEDIA);  
$fileAbsolutePath = $this->filesystem->getDirectoryRead(DirectoryList::ROOT)->getAbsolutePath($filePath);

10 - Get/Set Config Value from system
$val = $this->scopeConfig->getValue('rss/config/active', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)
$val = Magento\Framework\App\Config\MutableScopeConfigInterface->setValue($path,$value,$scopeType = \Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT,$scopeCode = null);


Updated in January 23
To be continue.....

Monday, November 2, 2015

Fix XML Validate of PhpStorm in magento 2

PhpStorm 9 with magento 2 dev-1.0.0beta

Follow this steps

Step1

Step2

Step3

Step4

Step5


Step6


Thanks for visit , share this post if you like

Tuesday, October 6, 2015

GetUrl in Magento 2

review file \Magento\Store\Model\Store.php

public function getBaseUrl($type = \Magento\Framework\UrlInterface::URL_TYPE_LINK, $secure = null)
    {
        $cacheKey = $type . '/' . ($secure === null ? 'null' : ($secure ? 'true' : 'false'));
        if (!isset($this->_baseUrlCache[$cacheKey])) {
            $secure = $secure === null ? $this->isCurrentlySecure() : (bool)$secure;
            switch ($type) {
                case \Magento\Framework\UrlInterface::URL_TYPE_WEB:
                    $path = $secure
                        ? self::XML_PATH_SECURE_BASE_URL
                        : self::XML_PATH_UNSECURE_BASE_URL;
                    $url = $this->getConfig($path);
                    break;

                case \Magento\Framework\UrlInterface::URL_TYPE_LINK:
                    $path = $secure ? self::XML_PATH_SECURE_BASE_LINK_URL : self::XML_PATH_UNSECURE_BASE_LINK_URL;
                    $url = $this->getConfig($path);
                    $url = $this->_updatePathUseRewrites($url);
                    $url = $this->_updatePathUseStoreView($url);
                    break;

                case \Magento\Framework\UrlInterface::URL_TYPE_DIRECT_LINK:
                    $path = $secure ? self::XML_PATH_SECURE_BASE_LINK_URL : self::XML_PATH_UNSECURE_BASE_LINK_URL;
                    $url = $this->getConfig($path);
                    $url = $this->_updatePathUseRewrites($url);
                    break;

                case \Magento\Framework\UrlInterface::URL_TYPE_STATIC:
                    $path = $secure ? self::XML_PATH_SECURE_BASE_STATIC_URL : self::XML_PATH_UNSECURE_BASE_STATIC_URL;
                    $url = $this->getConfig($path);
                    if (!$url) {
                        $url = $this->getBaseUrl(
                            \Magento\Framework\UrlInterface::URL_TYPE_WEB,
                            $secure
                        ) . $this->filesystem->getUri(
                            DirectoryList::STATIC_VIEW
                        );
                    }
                    break;

                case \Magento\Framework\UrlInterface::URL_TYPE_MEDIA:
                    $url = $this->_getMediaScriptUrl($this->filesystem, $secure);
                    if (!$url) {
                        $path = $secure ? self::XML_PATH_SECURE_BASE_MEDIA_URL : self::XML_PATH_UNSECURE_BASE_MEDIA_URL;
                        $url = $this->getConfig($path);
                        if (!$url) {
                            $url = $this->getBaseUrl(
                                \Magento\Framework\UrlInterface::URL_TYPE_WEB,
                                $secure
                            ) . $this->filesystem->getUri(
                                DirectoryList::MEDIA
                            );
                        }
                    }
                    break;

                default:
                    throw new \InvalidArgumentException('Invalid base url type');
            }

            if (false !== strpos($url, self::BASE_URL_PLACEHOLDER)) {
                $distroBaseUrl = $this->_request->getDistroBaseUrl();
                $url = str_replace(self::BASE_URL_PLACEHOLDER, $distroBaseUrl, $url);
            }

            $this->_baseUrlCache[$cacheKey] = rtrim($url, '/') . '/';
        }

        return $this->_baseUrlCache[$cacheKey];
    }






Wednesday, September 16, 2015

Magento 2.0 CLI commands

cache
 cache:clean
 cache:disable
 cache:flush
 cache:enable
 indexer:reindex
dev:css:deploy  Compile css to less
sampledata:install adminusername Install sample luma theme
setup:uninstall re-install magento , clean sampledata , clean db


Command should begin with php bin/magento

Friday, August 28, 2015

Tip Install full npm for local workflow

Use command

npm install -g node-gyp yo bower grunt-cli gulp --msvs_version=2012
npm install