Skip to content

Commit 5d629d6

Browse files
author
Arūnas Stonis
committed
Minor bugfixes and improvements
1 parent c66241f commit 5d629d6

9 files changed

Lines changed: 136 additions & 70 deletions

File tree

Block/Trustbox.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ public function loadTrustboxes()
6161
}
6262
else if ($this->_registry->registry('current_category')) {
6363
$loadedTrustboxes = array_merge((array)$this->loadPageTrustboxes($settings, 'category'), (array)$loadedTrustboxes);
64-
$trustboxSettings->categoryProductsData = $this->_helper->loadCategoryProductInfo($settings);
64+
if ($this->repeatData($loadedTrustboxes)) {
65+
$trustboxSettings->categoryProductsData = $this->loadCategoryProductInfo($scope, $storeId);
66+
}
6567
}
6668
if ($this->_request->getFullActionName() == 'cms_index_index') {
6769
$loadedTrustboxes = array_merge((array)$this->loadPageTrustboxes($settings, 'landing'), (array)$loadedTrustboxes);
@@ -76,6 +78,15 @@ public function loadTrustboxes()
7678
return '{"trustboxes":[]}';
7779
}
7880

81+
private function repeatData($trustBoxes) {
82+
foreach ($trustBoxes as $trustbox) {
83+
if (isset($trustbox->repeat) && $trustbox->repeat || true) {
84+
return true;
85+
}
86+
}
87+
return false;
88+
}
89+
7990
private function loadPageTrustboxes($settings, $page)
8091
{
8192
$data = [];
@@ -116,4 +127,16 @@ private function checkCustomPage($tbPage, $page) {
116127
$tbPage == strtolower(base64_encode(rtrim($page, '/')))
117128
);
118129
}
130+
131+
public function loadCategoryProductInfo($scope, $storeId) {
132+
try {
133+
$block = $this->getLayout()->getBlock('category.products.list');
134+
$products = $block->getLoadedProductCollection();
135+
return $this->_helper->loadCategoryProductInfo($products, $scope, $storeId);
136+
} catch(\Throwable $e) {
137+
return array();
138+
} catch(\Exception $e) {
139+
return array();
140+
}
141+
}
119142
}

Controller/Adminhtml/Index/Index.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ public function execute()
8383
$this->getResponse()->setBody(json_encode($result));
8484
break;
8585
case 'get_category_product_info':
86-
$result = $this->_helper->loadCategoryProductInfo();
86+
$result = array(
87+
'categoryProductsData' => $this->_helper->loadDefaultCategoryProductInfo($scope, $scopeId)
88+
);
8789
$this->getResponse()->setBody(json_encode($result));
8890
break;
8991
}

Helper/Data.php

Lines changed: 77 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -249,14 +249,7 @@ public function getPageUrl($page, $scope, $storeId)
249249
case 'trustpilot_trustbox_homepage':
250250
return $this->_storeManager->getStore($storeId)->getBaseUrl().'?___store='.$storeCode;
251251
case 'trustpilot_trustbox_category':
252-
$collection = $this->_categoryCollectionFactory->create();
253-
$collection->addAttributeToSelect('*');
254-
$collection->setStore($storeId);
255-
$collection->addAttributeToFilter('is_active', 1);
256-
$collection->addAttributeToFilter('children_count', 0);
257-
$collection->addUrlRewriteToResult();
258-
$collection->setPageSize(1);
259-
$category = $collection->getFirstItem();
252+
$category = $this->getFirstCategory($storeId);
260253
$productUrl = strtok($category->getUrl(),'?').'?___store='.$storeCode;
261254
return $productUrl;
262255
case 'trustpilot_trustbox_product':
@@ -281,6 +274,17 @@ public function getPageUrl($page, $scope, $storeId)
281274
}
282275
}
283276

277+
public function getFirstCategory($storeId) {
278+
$collection = $this->_categoryCollectionFactory->create();
279+
$collection->addAttributeToSelect('*');
280+
$collection->setStore($storeId);
281+
$collection->addAttributeToFilter('is_active', 1);
282+
$collection->addAttributeToFilter('children_count', 0);
283+
$collection->addUrlRewriteToResult();
284+
$collection->setPageSize(1);
285+
return $collection->getFirstItem();
286+
}
287+
284288
public function getProductIdentificationOptions()
285289
{
286290
$fields = array('none', 'sku', 'id');
@@ -411,38 +415,73 @@ public function getBusinessInformation($scope, $scopeId) {
411415
);
412416
}
413417

414-
public function loadCategoryProductInfo($settings) {
415-
$skuSelector = $settings->skuSelector;
416-
$productList = $variationSkus = $variationIds = array();
417-
418-
$params = $this->_request->getParams();
419-
$category = $params['id'];
420-
$limit = array_key_exists('limit', $params) ? $params['limit'] : $this->scopeConfig->getValue('catalog/frontend/grid_per_page');
421-
$page = array_key_exists('limit', $params) ? $params['p'] : 1;
422-
423-
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
424-
$layerResolver = $objectManager->getInstance()->get(\Magento\Catalog\Model\Layer\Resolver::class);
425-
$layer = $layerResolver->get();
426-
$layer->setCurrentCategory($category);
427-
428-
$products = $layer->getProductCollection()->setPage($page, $limit);
429-
foreach ($products->getItems() as $product) {
430-
if ($product->getTypeId() == 'configurable') {
431-
$childProducts = $this->_linkManagement->getChildren($product->getSku());
432-
$variationSkus = $skuSelector != 'id' ? $this->loadSelector($product, $skuSelector, $childProducts) : array();
433-
$variationIds = $this->loadSelector($product, 'id', $childProducts);
418+
public function loadCategoryProductInfo($products, $scope, $scopeId) {
419+
try {
420+
$settings = json_decode(self::getConfig('master_settings_field', $scopeId, $scope));
421+
$skuSelector = empty($settings->skuSelector) || $settings->skuSelector == 'none' ? 'sku' : $settings->skuSelector;
422+
$productList = $variationSkus = $variationIds = array();
423+
424+
foreach ($products->getItems() as $product) {
425+
if ($product->getTypeId() == 'configurable') {
426+
$childProducts = $this->_linkManagement->getChildren($product->getSku());
427+
$variationSkus = $skuSelector != 'id' ? $this->loadSelector($product, $skuSelector, $childProducts) : array();
428+
$variationIds = $this->loadSelector($product, 'id', $childProducts);
429+
}
430+
$sku = $skuSelector != 'id' ? $this->loadSelector($product, $skuSelector) : '';
431+
$id = $this->loadSelector($product, 'id');
432+
array_push($productList, array(
433+
"sku" => $sku,
434+
"id" => $id,
435+
"variationIds" => $variationIds,
436+
"variationSkus" => $variationSkus,
437+
"productUrl" => $product->getProductUrl() ?: '',
438+
"name" => $product->getName(),
439+
));
434440
}
435-
$sku = $skuSelector != 'id' ? $this->loadSelector($product, $skuSelector) : '';
436-
$id = $this->loadSelector($product, 'id');
437-
array_push($productList, array(
438-
"sku" => $sku,
439-
"id" => $id,
440-
"variationIds" => $variationIds,
441-
"variationSkus" => $variationSkus,
442-
"productUrl" => $product->getProductUrl() ?: '',
443-
"name" => $product->getName(),
441+
return $productList;
442+
} catch(\Throwable $e) {
443+
$description = 'Unable to load category product info ';
444+
$this->_trustpilotLog->error($e, $description, array(
445+
'scope' => $scope,
446+
'scopeId' => $scopeId
447+
));
448+
return array();
449+
} catch(\Exception $e) {
450+
$description = 'Unable to load category product info ';
451+
$this->_trustpilotLog->error($e, $description, array(
452+
'scope' => $scope,
453+
'scopeId' => $scopeId
454+
));
455+
return array();
456+
}
457+
}
458+
459+
public function loadDefaultCategoryProductInfo($scope, $scopeId) {
460+
try {
461+
$category = $this->getFirstCategory($scopeId);
462+
$limit = $this->scopeConfig->getValue('catalog/frontend/grid_per_page');
463+
$page = 1;
464+
465+
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
466+
$layerResolver = $objectManager->get(\Magento\Catalog\Model\Layer\Resolver::class);
467+
$layer = $layerResolver->get();
468+
$layer->setCurrentCategory($category);
469+
$products = $layer->getProductCollection()->setPage($page, $limit);
470+
return $this->loadCategoryProductInfo($products, $scope, $scopeId);
471+
} catch(\Throwable $e) {
472+
$description = 'Unable to load category product info ';
473+
$this->_trustpilotLog->error($e, $description, array(
474+
'scope' => $scope,
475+
'scopeId' => $scopeId
476+
));
477+
return array();
478+
} catch(\Exception $e) {
479+
$description = 'Unable to load category product info ';
480+
$this->_trustpilotLog->error($e, $description, array(
481+
'scope' => $scope,
482+
'scopeId' => $scopeId
444483
));
484+
return array();
445485
}
446-
return $productList;
447486
}
448487
}

Helper/OrderData.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public function getEmail($order)
103103
}
104104

105105
try {
106-
if (!($this->is_empty($order->getShippingAddress()->getEmail())))
106+
if (!($this->is_empty($order->getShippingAddress())) && !($this->is_empty($order->getShippingAddress()->getEmail())))
107107
return $order->getShippingAddress()->getEmail();
108108
} catch (\Throwable $e) {
109109
$description = 'Unable to get customer email from a shipping address';
@@ -114,7 +114,7 @@ public function getEmail($order)
114114
}
115115

116116
try {
117-
if (!($this->is_empty($order->getBillingAddress()->getEmail())))
117+
if (!($this->is_empty($order->getBillingAddress())) && !($this->is_empty($order->getBillingAddress()->getEmail())))
118118
return $order->getBillingAddress()->getEmail();
119119
} catch (\Throwable $e) {
120120
$description = 'Unable to get customer email from a billing address';
@@ -125,7 +125,7 @@ public function getEmail($order)
125125
}
126126

127127
try {
128-
if (!($this->is_empty($order->getCustomerId())))
128+
if (!($this->is_empty($order->getCustomerId())) && !($this->is_empty($order->getCustomerId())))
129129
return $this->_customer->load($order->getCustomerId())->getEmail();
130130
} catch (\Throwable $e) {
131131
$description = 'Unable to get customer email from customer data';

Model/Config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Config
1616
const TRUSTPILOT_GENERAL_CONFIGURATION = 'general';
1717
const TRUSTPILOT_TRUSTBOX_CONFIGURATION = 'trustbox';
1818
const TRUSTPILOT_INTEGRATION_KEY = 'key';
19-
const TRUSTPILOT_PLUGIN_VERSION = '2.6.531';
19+
const TRUSTPILOT_PLUGIN_VERSION = '2.6.541';
2020
const TRUSTPILOT_SCRIPT = 'TrustpilotScriptUrl';
2121
const TRUSTPILOT_INTEGRATION_APP = 'IntegrationAppUrl';
2222
const TRUSTPILOT_WIDGET_SCRIPT = 'WidgetScriptUrl';

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "trustpilot/module-reviews",
33
"description": "The Trustpilot Review extension makes it simple and easy for merchants to collect reviews from their customers to power their marketing efforts, increase sales conversion, build their online reputation and draw business insights.",
44
"type": "magento2-module",
5-
"version": "2.6.531",
5+
"version": "2.6.541",
66
"license": [
77
"OSL-3.0"
88
],

view/adminhtml/web/js/admin.js

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -87,31 +87,33 @@ function receiveInternalData(e) {
8787
}
8888

8989
function requestCategoryInfo() {
90-
const data = {
91-
action: 'get_category_product_info',
92-
form_key: window.FORM_KEY,
93-
};
90+
// TODO: It brake's existing category list page filtering therefore commented until solution will be found
91+
// const data = {
92+
// action: 'get_category_product_info',
93+
// form_key: window.FORM_KEY,
94+
// scope, scopeId,
95+
// };
9496

95-
if (typeof websiteId !== 'undefined') {
96-
data.website_id = websiteId;
97-
}
98-
if (typeof storeId !== 'undefined') {
99-
data.store_id = storeId;
100-
}
97+
// if (typeof websiteId !== 'undefined') {
98+
// data.website_id = websiteId;
99+
// }
100+
// if (typeof storeId !== 'undefined') {
101+
// data.store_id = storeId;
102+
// }
101103

102-
const xhr = new XMLHttpRequest();
103-
xhr.onreadystatechange = function() {
104-
if (xhr.readyState === 4) {
105-
if (xhr.status >= 400) {
106-
console.log(`callback error: ${xhr.response} ${xhr.status}`);
107-
} else {
108-
window.postMessage(JSON.stringify(xhr.response), window.origin);
109-
}
110-
}
111-
}
112-
xhr.open('POST', `${ajaxUrl}?isAjax=true`, true);
113-
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
114-
xhr.send(encodeSettings(data));
104+
// const xhr = new XMLHttpRequest();
105+
// xhr.onreadystatechange = function() {
106+
// if (xhr.readyState === 4) {
107+
// if (xhr.status >= 400) {
108+
// console.log(`callback error: ${xhr.response} ${xhr.status}`);
109+
// } else {
110+
// window.postMessage(JSON.stringify(xhr.response), window.origin);
111+
// }
112+
// }
113+
// }
114+
// xhr.open('POST', `${ajaxUrl}?isAjax=true`, true);
115+
// xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
116+
// xhr.send(encodeSettings(data));
115117
}
116118

117119
function submitPastOrdersCommand(data) {

0 commit comments

Comments
 (0)