Skip to main content

Posts

Magento 2 Get Current CMS Page Id

Today, we will discuss how we can get current CMS page id using code. Below is an example. protected $_page ; public function __construct ( ... \Magento\Cms\Model\Page $page , ... array $data = [] ) { parent :: __construct ( $context , $data ); ... $this -> _page = $page ; ... } if ( $this -> _page -> getId ()) { $pageId = $this -> _page -> getId (); } Thank You.!
Recent posts

Magento 2 Keep Product Image Aspect Ratio

Below is an example. <? php $_imagehelper = $this -> helper ( 'Magento\Catalog\Helper\Image' ); $productImage = $_imagehelper -> init ( $_product , $image )-> constrainOnly ( FALSE )-> keepAspectRatio ( TRUE )-> keepFrame ( FALSE )-> resize ( 400 )-> getUrl (); ?> <img src=" <? php echo $productImage ; ?> " /> Thank You.!

Magento 2 Login programmatically

Using Object Manager $objectManager = \Magento\Framework\App\ObjectManager :: getInstance (); // Load customer $customer = $objectManager -> create ( 'Magento\Customer\Model\Customer' )-> load ( 2 ); //2 is Customer ID // Load customer session $customerSession = $objectManager -> create ( 'Magento\Customer\Model\Session' ); $customerSession -> setCustomerAsLoggedIn ( $customer ); if ( $customerSession -> isLoggedIn ()) { echo "Customer Logged in" ; } else { echo "customer is Not Logged in" ; }   Using dependency in construct method protected $_customerFactory ; protected $_sessionFactory ; public function __construct ( ... \Magento\Customer\Model\CustomerFactory $customerFactory , \Magento\Customer\Model\SessionFactory $sessionFactory , ... ) { ... $this -> _customerFactory = $customerFactory ; $this -> _sessionFactory = $sessionFactory ; ... } public function execute () {

Magento 2 How To Get Product Description On List Page ?

In eCommerce Description of the product is most important part as customer always judge purchase based on good description. We will check how we can get description on product list page in Magento 2. Goto Stores > Attributes > Product > Description > Storefront Properties  set Visible on Catalog Pages on Storefront and Used in Product Listing to TRUE. In your code you can edit list.phtml and add code as below. <? php echo $this -> helper ( 'Magento\Catalog\Helper\Output' )-> productAttribute ( $_product , $_product -> getDescription (), 'description' ) ?> or <? php echo $_product -> getDescription () ?> Thank You.!

Magento 2 Check Is Customer Subscribed To Newsletter ?

Magento 2 comes with many features and functionalities towards eCommerce. Newsletter is efficient way to aware for products and use brings trust to your business for your customers. We will discuss how we can check customer is subscribed or not in newsletter functionality. First of all we will inject the \Magento\Newsletter\Model\Subscriber class in constructor as below. protected $_subscriber ; public function __construct ( ... \Magento\Newsletter\Model\Subscriber $subscriber ... ){ ... $this -> _subscriber = $subscriber ; ... } There are two possible cases which we can use to check customer is subscribed to newsletter or not. Using Customer Email Address. $checkSubscriber = $this -> _subscriber -> loadByEmail ( $customerEmail ); if ( $checkSubscriber -> isSubscribed ()) { // Customer is subscribed } else { // Customer is not subscribed }   Using Customer Id. $checkSubscriber = $this -> _subscriber -> loadByCustomerId ( $cust

Magento 2 How To Add Custom Css In Head Section ?

Magento 2 comes with new features and functionality. Every business has different needs and different strategies to achieve goals. Magento 2 development has many standards to follow. We will discuss today regarding how to add custom css file in head section. We use layout xml files to add css below is an example. <head> <css src="Namespace_YourModule::css/styles.css"/> </head> Now, add css file to path, clear cache and deploy static content below commands can help you. php bin/magento cache:clean php bin/magento setup:static-content:deploy Thank You.!

Magento 2 How To Get custom table data ?

magento 2 How to get my custom table data ? To get custom table data we have to use custom module. First of all need to created Model and Collection file associated with that tables. As a second step, We need to add Block PHP file and inject model factory class into constructor as below. public function __construct ( Context $context , \Namespace\Modulename\Model\ModelNameFactory $modelNameFactory , array $data = array () ) { $this -> _modelFactory = $modelFactory ; parent :: __construct ( $context , $data ); } Now 3rd step we need to Prepare a public method in your block to access collection like below code. public function getCollectionData (){ return $this -> _modelFactory -> create ()-> getCollection (); } Now, you need to call $this->getCollectionData() in phtml will return collection. you can use in loop. Thank You.!