Skip to main content

Magento2

Power your online business with the most flexible and scalable ecommerce platform

Magento2 caching a blocks in a page

Magento2 Block Cache Caching plays an essential role in improving the page load time. In this article, I want to show you five blocks cache in Magento 2, or five approaches to caching, and when and how to use them. Interestingly, this article will cover both full page and block cache. Let’s explore each type of block cache together! In case, cache_lifetime of the block is set to a number greater than 0, the block is cacheable

Magento2 Indexing Change Mode programmatically

Magento2 Indexing Change Mode programmatically: Indexing is how Magento transforms data such as products and categories, to improve the performance of your storefront. As data changes, the transformed data must be updated or reindexed. Magento has a very sophisticated architecture that stores lots of merchant data (including catalog data, prices, users, and stores) in many database tables. To optimize storefront performance, Magento accumulates data into special tables using indexers.

How to fix the «Invalid template file» / «require_js.phtml» failure of Magento 2.3.x in Windows?

go to this file


/Magento/Framework/View/Element/Template/File/Validator.php

replace this part


foreach ($directories as $directory) {
    if (0 === strpos($realPath, $directory)) {

with the following one:

$isWin = 'WIN' === strtoupper(substr(PHP_OS, 0, 3)); 

/** @var bool $isWin */ 
foreach ($directories as $directory) { 
    if (0 === strpos($realPath, !$isWin ? $directory : $this->fileDriver->getRealPath($directory)) ) {

 

Original article 

How do I clear the cache in Magento 2 for a single product_id

the best way to do do this is using the below method


$objectManager =   \Magento\Framework\App\ObjectManager::getInstance();
$productR =      $objectManager ->create('\Magento\Catalog\Api\ProductRepositoryInterface');
$product = $productR->get('product_sku');
$product->cleanCache();
$this->_eventManager->dispatch('clean_cache_by_tags', ['object' => $product]);


you need of course to follow the DI to implement this code, I just used this quick way to make it easy to understand

 

Programmatically update customer data in Magento 2

Using Object Manager

$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$websiteId = $storeManager->getStore()->getWebsiteId();
 
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerRepository = $objectManager->create('Magento\Customer\Api\CustomerRepositoryInterface');
 
// load customer info by email
$customer = $customerRepository->get('[email protected]', $websiteId);
 
// load customer info by id    
$customer = $customerRepository->getById(1);
 
// Update customer Info
$cust