问题
Preconditions Magento CE 2.2.0 PHP 7.0.23-1
Database migrated from 1.9.2 to 2.2.0
Checked with different instances and various versions. Configured highest possible server configuration.
-We have one 'classicrings' category, there are only 12 configurable products. Each configurable product containing minimum 1100 simple products, some of them have more than 1500 simple products and total more than 13k products (including with configurable and simple variations) under one 'classicrings' category.
one configurable product have 5 attributes 'color, material,finishing,thickness,width' and we displaying the each product attributes on listing page, including with its all swatch options (like color :red,green,yellow.., material: 10k,14k,18k.., width:2mm,4mm to 12mm.. etc.)
Steps to reproduce
When we click on classicrings category (i.e. listing page in frontend) to display the all 12 configurable products in frontend then server going to time out Or loading page some times after 10 to 15 min.
-So, we debugged the list.phtml code and commented the below code, through from which all swatch attributes and its options is coming on listing page :
//echo $block->getProductDetailsHtml($_product);
After commenting above line of code, listing page start loading quickly within 2 sec. But this is not the solution. As client want to display the all attributes with its options, which is taking too much time to load and going server time out most of the time.
Expected result
category Product listing page should be load with all attribute and options, withing at least 2 to 5 seconds (Including with showing all attributes and options).
Actual result
Request Timeout
This request takes too long to process, it is timed out by the server. If it should not be timed out. Sometime system crashed.
For smaller product variations, it is loading good, but its also taking more than 40 seconds.
回答1:
To improve the listing page performance :
I have tried to implement seperate ajax call for this page by loading each product's swatch options one by one through ajax call then I can able to increase page speed, But still page is loading much more time to load and user experience is not good.
Also, I have tried to fetch and display the swatch data by another method , using the concept of filter only one swatch attribute (in my case "Width" attribute). For the same, I have overridden the "SwatchAttributesAsArray" function of class 'Magento\Swatches\Helper\Data' by using plugin. But still its taking min 5 to 8 min to load the page.
Have implemented the code in below way: //di.xml -- type name="Magento\Swatches\Helper\Data" //plugin name="RestrictProductsOptions" type="Synapsemage\Import\Plugin\SwatchData"
public function afterGetSwatchAttributesAsArray($subject, $attributesData)
{
// die('mritu');
$actions = ['catalog_category_view','catalogsearch_result_index','catalogsearch_advanced_result','import_index_listswatch'];
if (in_array($this->getFullAction(), $actions))
{
$objectManager = ObjectManager::getInstance();
//get current category
$categoryId = $parentId = 0;
$category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');
if($category!=null){
$categoryId = $category->getId();
$parentId = $category->getParentCategory()->getId();
}
if($categoryId==34){
$newAttributesData = array();
$newAttributesData[182] = $attributesData[182];
// print_r($newAttributesData);die;
return $newAttributesData;
}
foreach($attributesData as $attr)
{
$attrcode = $attr['attribute_code'];
if(!empty($attrcode))
{
if($categoryId==44){
if($attrcode=='stone'){
unset($attributesData[$attr['attribute_id']]);
}
}elseif($categoryId==31 || $parentId==31 || $categoryId==28){
if($attrcode=='material'){
unset($attributesData[$attr['attribute_id']]);
}
if($categoryId==31 || $parentId==31){
if($attrcode=='color'){
unset($attributesData[$attr['attribute_id']]);
}
}
if($categoryId==33 ){
if($attrcode=='stone'){
unset($attributesData[$attr['attribute_id']]);
}
}
}elseif($categoryId==21 || $parentId==21){
if($attrcode=='stone'){
unset($attributesData[$attr['attribute_id']]);
}
}
}
}
}
return $attributesData;
}
}
来源:https://stackoverflow.com/questions/51173490/magento2-configurable-product-get-swatch-attributes-in-list-phtml-takes-too-mu