Skip to content

Commit

Permalink
Minor bugfixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Arūnas Stonis committed Jan 13, 2020
1 parent c66241f commit 5d629d6
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 70 deletions.
25 changes: 24 additions & 1 deletion Block/Trustbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ public function loadTrustboxes()
}
else if ($this->_registry->registry('current_category')) {
$loadedTrustboxes = array_merge((array)$this->loadPageTrustboxes($settings, 'category'), (array)$loadedTrustboxes);
$trustboxSettings->categoryProductsData = $this->_helper->loadCategoryProductInfo($settings);
if ($this->repeatData($loadedTrustboxes)) {
$trustboxSettings->categoryProductsData = $this->loadCategoryProductInfo($scope, $storeId);
}
}
if ($this->_request->getFullActionName() == 'cms_index_index') {
$loadedTrustboxes = array_merge((array)$this->loadPageTrustboxes($settings, 'landing'), (array)$loadedTrustboxes);
Expand All @@ -76,6 +78,15 @@ public function loadTrustboxes()
return '{"trustboxes":[]}';
}

private function repeatData($trustBoxes) {
foreach ($trustBoxes as $trustbox) {
if (isset($trustbox->repeat) && $trustbox->repeat || true) {
return true;
}
}
return false;
}

private function loadPageTrustboxes($settings, $page)
{
$data = [];
Expand Down Expand Up @@ -116,4 +127,16 @@ private function checkCustomPage($tbPage, $page) {
$tbPage == strtolower(base64_encode(rtrim($page, '/')))
);
}

public function loadCategoryProductInfo($scope, $storeId) {
try {
$block = $this->getLayout()->getBlock('category.products.list');
$products = $block->getLoadedProductCollection();
return $this->_helper->loadCategoryProductInfo($products, $scope, $storeId);
} catch(\Throwable $e) {
return array();
} catch(\Exception $e) {
return array();
}
}
}
4 changes: 3 additions & 1 deletion Controller/Adminhtml/Index/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ public function execute()
$this->getResponse()->setBody(json_encode($result));
break;
case 'get_category_product_info':
$result = $this->_helper->loadCategoryProductInfo();
$result = array(
'categoryProductsData' => $this->_helper->loadDefaultCategoryProductInfo($scope, $scopeId)
);
$this->getResponse()->setBody(json_encode($result));
break;
}
Expand Down
115 changes: 77 additions & 38 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,7 @@ public function getPageUrl($page, $scope, $storeId)
case 'trustpilot_trustbox_homepage':
return $this->_storeManager->getStore($storeId)->getBaseUrl().'?___store='.$storeCode;
case 'trustpilot_trustbox_category':
$collection = $this->_categoryCollectionFactory->create();
$collection->addAttributeToSelect('*');
$collection->setStore($storeId);
$collection->addAttributeToFilter('is_active', 1);
$collection->addAttributeToFilter('children_count', 0);
$collection->addUrlRewriteToResult();
$collection->setPageSize(1);
$category = $collection->getFirstItem();
$category = $this->getFirstCategory($storeId);
$productUrl = strtok($category->getUrl(),'?').'?___store='.$storeCode;
return $productUrl;
case 'trustpilot_trustbox_product':
Expand All @@ -281,6 +274,17 @@ public function getPageUrl($page, $scope, $storeId)
}
}

public function getFirstCategory($storeId) {
$collection = $this->_categoryCollectionFactory->create();
$collection->addAttributeToSelect('*');
$collection->setStore($storeId);
$collection->addAttributeToFilter('is_active', 1);
$collection->addAttributeToFilter('children_count', 0);
$collection->addUrlRewriteToResult();
$collection->setPageSize(1);
return $collection->getFirstItem();
}

public function getProductIdentificationOptions()
{
$fields = array('none', 'sku', 'id');
Expand Down Expand Up @@ -411,38 +415,73 @@ public function getBusinessInformation($scope, $scopeId) {
);
}

public function loadCategoryProductInfo($settings) {
$skuSelector = $settings->skuSelector;
$productList = $variationSkus = $variationIds = array();

$params = $this->_request->getParams();
$category = $params['id'];
$limit = array_key_exists('limit', $params) ? $params['limit'] : $this->scopeConfig->getValue('catalog/frontend/grid_per_page');
$page = array_key_exists('limit', $params) ? $params['p'] : 1;

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$layerResolver = $objectManager->getInstance()->get(\Magento\Catalog\Model\Layer\Resolver::class);
$layer = $layerResolver->get();
$layer->setCurrentCategory($category);

$products = $layer->getProductCollection()->setPage($page, $limit);
foreach ($products->getItems() as $product) {
if ($product->getTypeId() == 'configurable') {
$childProducts = $this->_linkManagement->getChildren($product->getSku());
$variationSkus = $skuSelector != 'id' ? $this->loadSelector($product, $skuSelector, $childProducts) : array();
$variationIds = $this->loadSelector($product, 'id', $childProducts);
public function loadCategoryProductInfo($products, $scope, $scopeId) {
try {
$settings = json_decode(self::getConfig('master_settings_field', $scopeId, $scope));
$skuSelector = empty($settings->skuSelector) || $settings->skuSelector == 'none' ? 'sku' : $settings->skuSelector;
$productList = $variationSkus = $variationIds = array();

foreach ($products->getItems() as $product) {
if ($product->getTypeId() == 'configurable') {
$childProducts = $this->_linkManagement->getChildren($product->getSku());
$variationSkus = $skuSelector != 'id' ? $this->loadSelector($product, $skuSelector, $childProducts) : array();
$variationIds = $this->loadSelector($product, 'id', $childProducts);
}
$sku = $skuSelector != 'id' ? $this->loadSelector($product, $skuSelector) : '';
$id = $this->loadSelector($product, 'id');
array_push($productList, array(
"sku" => $sku,
"id" => $id,
"variationIds" => $variationIds,
"variationSkus" => $variationSkus,
"productUrl" => $product->getProductUrl() ?: '',
"name" => $product->getName(),
));
}
$sku = $skuSelector != 'id' ? $this->loadSelector($product, $skuSelector) : '';
$id = $this->loadSelector($product, 'id');
array_push($productList, array(
"sku" => $sku,
"id" => $id,
"variationIds" => $variationIds,
"variationSkus" => $variationSkus,
"productUrl" => $product->getProductUrl() ?: '',
"name" => $product->getName(),
return $productList;
} catch(\Throwable $e) {
$description = 'Unable to load category product info ';
$this->_trustpilotLog->error($e, $description, array(
'scope' => $scope,
'scopeId' => $scopeId
));
return array();
} catch(\Exception $e) {
$description = 'Unable to load category product info ';
$this->_trustpilotLog->error($e, $description, array(
'scope' => $scope,
'scopeId' => $scopeId
));
return array();
}
}

public function loadDefaultCategoryProductInfo($scope, $scopeId) {
try {
$category = $this->getFirstCategory($scopeId);
$limit = $this->scopeConfig->getValue('catalog/frontend/grid_per_page');
$page = 1;

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$layerResolver = $objectManager->get(\Magento\Catalog\Model\Layer\Resolver::class);
$layer = $layerResolver->get();
$layer->setCurrentCategory($category);
$products = $layer->getProductCollection()->setPage($page, $limit);
return $this->loadCategoryProductInfo($products, $scope, $scopeId);
} catch(\Throwable $e) {
$description = 'Unable to load category product info ';
$this->_trustpilotLog->error($e, $description, array(
'scope' => $scope,
'scopeId' => $scopeId
));
return array();
} catch(\Exception $e) {
$description = 'Unable to load category product info ';
$this->_trustpilotLog->error($e, $description, array(
'scope' => $scope,
'scopeId' => $scopeId
));
return array();
}
return $productList;
}
}
6 changes: 3 additions & 3 deletions Helper/OrderData.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function getEmail($order)
}

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

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

try {
if (!($this->is_empty($order->getCustomerId())))
if (!($this->is_empty($order->getCustomerId())) && !($this->is_empty($order->getCustomerId())))
return $this->_customer->load($order->getCustomerId())->getEmail();
} catch (\Throwable $e) {
$description = 'Unable to get customer email from customer data';
Expand Down
2 changes: 1 addition & 1 deletion Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Config
const TRUSTPILOT_GENERAL_CONFIGURATION = 'general';
const TRUSTPILOT_TRUSTBOX_CONFIGURATION = 'trustbox';
const TRUSTPILOT_INTEGRATION_KEY = 'key';
const TRUSTPILOT_PLUGIN_VERSION = '2.6.531';
const TRUSTPILOT_PLUGIN_VERSION = '2.6.541';
const TRUSTPILOT_SCRIPT = 'TrustpilotScriptUrl';
const TRUSTPILOT_INTEGRATION_APP = 'IntegrationAppUrl';
const TRUSTPILOT_WIDGET_SCRIPT = 'WidgetScriptUrl';
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "trustpilot/module-reviews",
"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.",
"type": "magento2-module",
"version": "2.6.531",
"version": "2.6.541",
"license": [
"OSL-3.0"
],
Expand Down
48 changes: 25 additions & 23 deletions view/adminhtml/web/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,31 +87,33 @@ function receiveInternalData(e) {
}

function requestCategoryInfo() {
const data = {
action: 'get_category_product_info',
form_key: window.FORM_KEY,
};
// TODO: It brake's existing category list page filtering therefore commented until solution will be found
// const data = {
// action: 'get_category_product_info',
// form_key: window.FORM_KEY,
// scope, scopeId,
// };

if (typeof websiteId !== 'undefined') {
data.website_id = websiteId;
}
if (typeof storeId !== 'undefined') {
data.store_id = storeId;
}
// if (typeof websiteId !== 'undefined') {
// data.website_id = websiteId;
// }
// if (typeof storeId !== 'undefined') {
// data.store_id = storeId;
// }

const xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if (xhr.status >= 400) {
console.log(`callback error: ${xhr.response} ${xhr.status}`);
} else {
window.postMessage(JSON.stringify(xhr.response), window.origin);
}
}
}
xhr.open('POST', `${ajaxUrl}?isAjax=true`, true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send(encodeSettings(data));
// const xhr = new XMLHttpRequest();
// xhr.onreadystatechange = function() {
// if (xhr.readyState === 4) {
// if (xhr.status >= 400) {
// console.log(`callback error: ${xhr.response} ${xhr.status}`);
// } else {
// window.postMessage(JSON.stringify(xhr.response), window.origin);
// }
// }
// }
// xhr.open('POST', `${ajaxUrl}?isAjax=true`, true);
// xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
// xhr.send(encodeSettings(data));
}

function submitPastOrdersCommand(data) {
Expand Down
Loading

0 comments on commit 5d629d6

Please sign in to comment.