Skip to content

Commit

Permalink
Send basic product data if extra fields failed to fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
dukeimg committed Aug 20, 2019
1 parent 7de52ef commit 31b3247
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 30 deletions.
72 changes: 44 additions & 28 deletions Helper/OrderData.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,25 @@
use Trustpilot\Reviews\Helper\Data;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
use Magento\Store\Model\ScopeInterface as StoreScopeInterface;
use \Psr\Log\LoggerInterface;

class OrderData extends AbstractHelper
{
protected $_storeManager;
protected $_helper;
protected $_categoryCollectionFactory;
protected $_logger;

public function __construct(
StoreManagerInterface $storeManager,
Data $helper,
CategoryCollectionFactory $categoryCollectionFactory)
CategoryCollectionFactory $categoryCollectionFactory,
LoggerInterface $logger)
{
$this->_storeManager = $storeManager;
$this->_helper = $helper;
$this->_categoryCollectionFactory = $categoryCollectionFactory;
$this->_logger = $logger;
}

public function getInvitation($order, $hook, $collect_product_data = \Trustpilot\Reviews\Model\Config::WITH_PRODUCT_DATA)
Expand Down Expand Up @@ -138,45 +142,57 @@ public function getProducts($order)
array_push($childProducts, $cpItem->getProduct());
}
}

$manufacturer = $this->_helper->loadSelector($product, 'manufacturer', $childProducts);

$sku = $this->_helper->loadSelector($product, $skuSelector, $childProducts);
$mpn = $this->_helper->loadSelector($product, $mpnSelector, $childProducts);
$gtin = $this->_helper->loadSelector($product, $gtinSelector, $childProducts);
array_push(
$products,
array(
'price' => $product->getFinalPrice(),
'currency' => $order->getOrderCurrencyCode(),
'description' => strip_tags($product->getDescription()),
'meta' => array(
'title' => $product->getMetaTitle() ? $product->getMetaTitle() : $product->getName(),
'keywords' => $product->getMetaKeyword() ? $product->getMetaKeyword() : $product->getName(),
'description' => $product->getMetaDescription() ? $product->getMetaDescription() : substr(strip_tags($product->getDescription()), 0, 255),
),
'manufacturer' => $manufacturer ? $manufacturer : '',
'categories' => $this->getProductCategories($product, $childProducts),
'images' => $this->getAllImages($product, $childProducts),
'videos' => $this->getAllVideos($product, $childProducts),
'tags' => null,
'productUrl' => $product->getProductUrl(),
'name' => $product->getName(),
'brand' => $product->getBrand() ? $product->getBrand() : $manufacturer,
'sku' => $sku ? $sku : '',
'mpn' => $mpn ? $mpn : '',
'gtin' => $gtin ? $gtin : '',
'imageUrl' => $this->_storeManager->getStore($order->getStoreId())->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA)
. 'catalog/product' . $product->getImage()
)

$productData = array(
'productUrl' => $product->getProductUrl(),
'name' => $product->getName(),
'sku' => $sku ? $sku : '',
'mpn' => $mpn ? $mpn : '',
'gtin' => $gtin ? $gtin : '',
'imageUrl' => $this->_storeManager->getStore($order->getStoreId())->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA)
. 'catalog/product' . $product->getImage()
);

$productData = $this->getProductExtraFields($productData, $product, $childProducts, $order);

array_push($products, $productData);
}
} catch (\Exception $e) {
// Just skipping products data if we are not able to collect it
$this->_logger->error('Unable to get product data: ' . $e->getMessage() . 'Line ' . $e->getLine());
}

return $products;
}

function getProductExtraFields($productData, $product, $childProducts, $order) {
try {
$manufacturer = $this->_helper->loadSelector($product, 'manufacturer', $childProducts);
return array_merge($productData, array(
'price' => $product->getFinalPrice(),
'currency' => $order->getOrderCurrencyCode(),
'description' => strip_tags($product->getDescription()),
'meta' => array(
'title' => $product->getMetaTitle() ? $product->getMetaTitle() : $product->getName(),
'keywords' => $product->getMetaKeyword() ? $product->getMetaKeyword() : $product->getName(),
'description' => $product->getMetaDescription() ? $product->getMetaDescription() : substr(strip_tags($product->getDescription()), 0, 255),
),
'manufacturer' => $manufacturer ? $manufacturer : '',
'categories' => $this->getProductCategories($product, $childProducts),
'images' => $this->getAllImages($product, $childProducts),
'videos' => $this->getAllVideos($product, $childProducts),
'tags' => null,
'brand' => $product->getBrand() ? $product->getBrand() : $manufacturer,
));
} catch (\Exception $e) {
return $productData;
}
}

function getProductCategories($product, $childProducts = null) {
$categories = array();
$categoryIds = array();
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.439';
const TRUSTPILOT_PLUGIN_VERSION = '2.6.444';
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.439",
"version": "2.6.444",
"license": [
"OSL-3.0"
],
Expand Down

0 comments on commit 31b3247

Please sign in to comment.