NVision-Commerce-Solutions / module-customer-price

Commerce 365 for Magento - Magento 2 Extension - Customer Price Module
https://n.vision/products/commerce-365-for-magento-b2b/
2 stars 4 forks source link

Prices loaded from ERP service when not logged in and when ajax enabled #36

Closed ldrooij closed 2 months ago

ldrooij commented 11 months ago

The Commerce365\CustomerPrice\Plugin\Frontend\ProductListPricesPreloadToCache::afterGetLoadedProductCollection method is called on any product collection load. The means the webservice is called for customer id null which (at least for our set-up) never returns any prices. Any call to the price service seem useless when customer id is null.

In addition there are several cases when prices are being fetched from the price service on initial page load for pages that are cached with FPC and where prices are loaded with ajax (when setting enabled). This is a bit harder to avoid this because some plugin are also needed in the frontend scope e.g. afterGetPrice, but it can cause a lot of unnecessary delay when there are lots of cache invalidations on your webshop. Would be nice if you can look into this.

soloma88 commented 10 months ago

Hi @ldrooij good point with customer id, but I don't fully understand second point with cached pages.

xcom-magento commented 10 months ago

@soloma88 When a 'cacheable' page containing product prices is loaded and a user is logged in the prices that are loaded and stored in pull page cache are potentially user specific, thus loaded from the webservice. The user doesn't notice since the prices are not shown, but the calls to the webservice are slowing down the ttfb a little bit when the page is not cached yet. Since the prices are loaded asyncronous anyway there is no real need to fetch them when a cacheable page is loaded. The layout object has a isCacheable method, I think this can possibly be used to skip the plugin on product price methods. Of course this only applies when ajax price loading is enabled. I hope this clarifies what I mean a little bit.

soloma88 commented 7 months ago

@xcom-magento even if we have calls to webservice during page load it means that we will not have it later on ajax call because they will be cached in database and shown to the customer immediately.

BTW I already added condition to check customer id before sending request to webservice so let me know if I can close this issue.

xcom-magento commented 7 months ago

@soloma88 I understand that loading the prices during initial page load means the prices will be loaded from cache during the ajax call later. It is not wrong or a bug, but not ideal for the initial page load. The initial page load can take quite long which is killing. We 've seen instances where 56 api calls take about 15 seconds to execute on initial load.

I'm not 100% sure if the above is still a realistic situation since you have done a lot of updates and we are not running on the latest version currently. We'll update soon and I'll check it out.

You might still want to consider my suggestion tough ;)

soloma88 commented 6 months ago

@xcom-magento I'm not sure what do you mean under "prices are being fetched from the price service on initial page load for pages that are cached with FPC" how it could be that FPC cached page make some requests on page load? FPC just return html and do not execute any code or do you mean that JS sending request to PHP to fetch prices? If yes I can tell only that this is the main idea of the module to avoid FPC show wrong cached price to the customer. In our case price is customer data.

xcom-magento commented 6 months ago

@soloma88 This is only the situation when a page is not yet available in FPC. I will try to describe what I mean more clearly.

Prequisites:

Steps to reproduce:

  1. Log in as a customer
  2. Visit a category page or product detail page

Expected result: During page rendering the Magento prices are used without using the commerce365 webservice. Page is stored in FPC. After page load the prices are fetched asynchronous calling the commerce365 webservice when prices are not yet available in commerce365_cache_price table.

Actual result: During page load the commerce365 webservice is called and prices are stored in commerce365_cached_price table. Page is stored in FPC. After page load the prices are fetched asynchronous from the cache table.

Calling the webservice during page load causes slow ttfb when a page has not yet been cached.

Hope this clears things a little bit.

soloma88 commented 6 months ago

@xcom-magento look at the ProductListPricesPreloadToCache it preloads prices for the customer with one request. So when page already loaded prices will show up faster. If you do not want to run it in this order you can try to remove that plugin and in Commerce365/CustomerPrice/Plugin/ProductPrice.php add in methods afterGetPrice and afterGetSpecialPrice conditions at the beginning if ($this->request->getModuleName() === 'catalog' && !$this->request->isXmlHttpRequest()) { return $result; }

It will prevent preload on catalog page I will check that approach and implement it if it has some benefits.