This is an old revision of the document!


Can you Exclude the Price Block from public Cache for Only the Logged-in Users?

You might have a situation where pricing varies per user and is fetched via API from an external ERP system. This system only applies to logged-in users. Users who are logged out should not see pricing at all.

For non-logged-in user:

For the logged-in user:

On product page click “login” to see the discounted prices from API. Fetch the prices directly from a external API solution to have separate customer pricing. There are two customer groups: group id 1 and group id 2. You would want the API price update for only one customer group 1 and store 1, but not group 2. How?

Caching pages with this kind of setup is tricky. LiteMage is able to cache different prices per customer group, but not per individual customers. We cannot punch a hole for price block, however, we can simply make the whole page either not cache, or privately cache. For this example, we would like to make customer group 1 logged-in user and store ID 1 to be private cache.

To enable private cache, you will need to add CacheLookup private on in .htaccess after LiteMage on, looks like the following:

<IfModule Litespeed> 
LiteMage on 
CacheLookup private on
</IfModule>

Also you will need to change LiteMage Esi.php code by adding the following to app/code/community/Litespeed/Litemage/Helper/Esi.php to protected function _getDefaultEnvCookieValue() line 1160 - 1164 within if ($diffGrp = $this→_config→getConf(Litespeed_Litemage_Helper_Data::CFG_DIFFCUSTGRP))

/* customized code added by for case 1 */
        if ($currCustomerGroup == 1 && $currStoreId == 1) {
                  $this->_cacheVars['flag'] |= self::CHBM_PRIVATE;
        }
/* end of customized code change */

The final code looks like:

 protected function _getDefaultEnvCookieValue()
  {
  ...
  ...
               if ($diffGrp = $this->_config->getConf(Litespeed_Litemage_Helper_Data::CFG_DIFFCUSTGRP)) {
                      // diff cache copy per customer group
                      $currCustomerGroup = Mage::getSingleton('customer/session')->getCustomerGroupId() ;
                      if ( Mage_Customer_Model_Group::NOT_LOGGED_IN_ID != $currCustomerGroup ) {
                              if ($diffGrp == 1) // diff copy per group
                                      $default['cgrp'] = $currCustomerGroup ;
                              elseif ($diffGrp == 2)    // diff copy for logged in user
                                      $default['cgrp'] = 'in' ;
                              elseif ($diffGrp == 3) {
                                      $cgset = $this->_config->getConf(Litespeed_Litemage_Helper_Data::CFG_DIFFCUSTGRP_SET);
                                      if (isset($cgset[$currCustomerGroup]))
                                              $default['cgrp'] = $cgset[$currCustomerGroup];
                              }
              /* customized code added for case 1 */
                              if ($currCustomerGroup == 1 && $currStoreId == 1) {
                                      $this->_cacheVars['flag'] |= self::CHBM_PRIVATE;
                              //      $this->setNotCacheable();
                              }
              /* end of customized code change */
                      }
              }
...
...
}

So for the logged-in user with group 1 and store 1, it will be served from private cache and private ESI blocks. The rest users will be served from public cache with private ESI blocks.

Updated LiteMage configuration:

Separate Cache Copy per Customer Group -> Yes

After the above change, you should be able to found the pages are being served from LiteMage private cache.

You should make a backup of the file app/code/community/Litespeed/Litemage/Helper/Esi.php since it is a customized solution and will be overided by LiteMage upgrade next time. You should copy back the custommized solution when you complete your next upgrade.

  • Admin
  • Last modified: 2017/12/18 19:20
  • by Jackson Zhang