Skip to content

Commit

Permalink
Added error checking for order data collection
Browse files Browse the repository at this point in the history
  • Loading branch information
Arunas Stonis committed May 14, 2018
1 parent 943ae53 commit 79370f4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 33 deletions.
75 changes: 43 additions & 32 deletions Block/Success.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,43 +48,54 @@ public function __construct(
$this->_helper = $helper;
$this->_notifications = $notifications;
$this->_customer = $customer;
$this->_version = '1.0.79.1';
$this->_version = '1.0.82';
parent::__construct($context, $data);
}

public function getOrder()
{
$orderId = $this->_checkoutSession->getLastOrderId();
$order = $this->_salesFactory->load($orderId);
$items = $order->getAllItems();
$products = [];

foreach ($items as $i) {
$product = $this->_product->load($i->getProductId());
$brand = $product->getAttributeText('manufacturer');
array_push(
$products,
[
'productUrl' => $product->getProductUrl(),
'name' => $product->getName(),
'brand' => $brand ? $brand : '',
'sku' => $product->getSku(),
'imageUrl' => $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . 'catalog/product' . $product->getImage()
]
);
}

$data = [
'recipientEmail' => trim($this->getEmail($order)),
'recipientName' => $order->getCustomerName(),
'referenceId' => $order->getRealOrderId(),
'productSkus' => $this->getSkus($products),
'source' => 'Magento-'.$this->_productMetadata->getVersion(),
'pluginVersion' => $this->_version,
'products' => $products,
];

return json_encode($data, JSON_HEX_APOS);
try {
$orderId = $this->_checkoutSession->getLastOrderId();
$order = $this->_salesFactory->load($orderId);

$products = [];
try {
$items = $order->getAllItems();
foreach ($items as $i) {
$product = $this->_product->load($i->getProductId());
$brand = $product->getAttributeText('manufacturer');
array_push(
$products,
[
'productUrl' => $product->getProductUrl(),
'name' => $product->getName(),
'brand' => $brand ? $brand : '',
'sku' => $product->getSku(),
'imageUrl' => $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA)
. 'catalog/product' . $product->getImage()
]
);
}
} catch (Exception $e) {
// Just skipping products data if we are not able to collect it
}

$data = [
'recipientEmail' => trim($this->getEmail($order)),
'recipientName' => $order->getCustomerName(),
'referenceId' => $order->getRealOrderId(),
'productSkus' => $this->getSkus($products),
'source' => 'Magento-'.$this->_productMetadata->getVersion(),
'pluginVersion' => $this->_version,
'products' => $products,
];

return json_encode($data, JSON_HEX_APOS);
} catch (Exception $e) {
$error = ['message' => $e->getMessage()];
$data = ['error' => $error];
return json_encode($data, JSON_HEX_APOS);
}
}

public function getSkus($products)
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": "1.0.79.1",
"version": "1.0.82",
"license": [
"OSL-3.0"
],
Expand Down

0 comments on commit 79370f4

Please sign in to comment.