Skip to content

Commit

Permalink
Releasing new version
Browse files Browse the repository at this point in the history
  • Loading branch information
Arunas Stonis committed Apr 2, 2019
1 parent 82fc9a6 commit 7b79a98
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 4 deletions.
20 changes: 19 additions & 1 deletion Block/Trustbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Trustpilot\Reviews\Helper\Data;
use Magento\Framework\App\Request\Http;
use Magento\Framework\App\ObjectManager;
use Magento\ConfigurableProduct\Api\LinkManagementInterface;

class Trustbox extends Template
{
Expand All @@ -16,12 +17,14 @@ class Trustbox extends Template
protected $_request;
protected $_storeManager;
protected $_urlInterface;
protected $_linkManagement;

public function __construct(
Context $context,
Data $helper,
Registry $registry,
Http $request,
LinkManagementInterface $_linkManagement,
array $data = [])
{
$this->_helper = $helper;
Expand All @@ -30,6 +33,7 @@ public function __construct(
$this->_storeManager = $context->getStoreManager();
$this->_tbWidgetScriptUrl = \Trustpilot\Reviews\Model\Config::TRUSTPILOT_WIDGET_SCRIPT_URL;
$this->_urlInterface = ObjectManager::getInstance()->get('Magento\Framework\UrlInterface');
$this->_linkManagement = $_linkManagement;
parent::__construct($context, $data);
}

Expand Down Expand Up @@ -77,7 +81,21 @@ private function loadPageTrustboxes($settings, $page)
if ($trustbox->page == $page && $trustbox->enabled == 'enabled') {
$current_product = $this->_registry->registry('current_product');
if ($current_product) {
$trustbox->sku = $this->_helper->loadSelector($current_product, $skuSelector);
$skus = array();
$productSku = $this->_helper->loadSelector($current_product, $skuSelector);
if ($productSku) {
array_push($skus, $productSku);
}
if ($current_product->getTypeId() == 'configurable') {
$collection = $this->_linkManagement->getChildren($current_product->getSku());
foreach ($collection as $product) {
$productSku = $this->_helper->loadSelector($product, $skuSelector);
if ($productSku) {
array_push($skus, $productSku);
}
}
}
$trustbox->sku = implode(',', $skus);
$trustbox->name = $current_product->getName();
}
array_push($data, $trustbox);
Expand Down
10 changes: 9 additions & 1 deletion Controller/Adminhtml/Index/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@
use Magento\Backend\App\Action;
use Trustpilot\Reviews\Helper\Data;
use Trustpilot\Reviews\Helper\PastOrders;
use Trustpilot\Reviews\Helper\Products;

class Index extends Action
{
protected $_helper;
protected $_pastOrders;
protected $_products;

public function __construct(
Action\Context $context,
Data $helper,
PastOrders $pastOrders)
PastOrders $pastOrders,
Products $products)
{
parent::__construct($context);
$this->_helper = $helper;
$this->_pastOrders = $pastOrders;
$this->_products = $products;
}

public function execute()
Expand Down Expand Up @@ -66,6 +70,10 @@ public function execute()
break;
}
break;
case 'check_product_skus':
$result = $this->_products->checkSkus();
$this->getResponse()->setBody(json_encode($result));
break;
}
}

Expand Down
1 change: 1 addition & 0 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ public function getFirstProduct()
$collection = $this->_productCollectionFactory->create();
$collection->addAttributeToSelect('*');
$collection->setStore($this->getWebsiteOrStoreId());
$collection->addStoreFilter($this->getWebsiteOrStoreId());
$collection->addAttributeToFilter('status', 1);
$collection->addAttributeToFilter('visibility', array(2, 3, 4));
return $collection->getFirstItem();
Expand Down
4 changes: 4 additions & 0 deletions Helper/OrderData.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public function getInvitation($order, $hook, $collect_product_data = \Trustpilot
$invitation['hook'] = $hook;
$invitation['orderStatusId'] = $order->getState();
$invitation['orderStatusName'] = $order->getStatusLabel();
try {
$invitation['totalCost'] = $order->getGrandTotal();
$invitation['currency'] = $order->getOrderCurrencyCode();
} catch (\Exception $ex) {}
if ($collect_product_data == \Trustpilot\Reviews\Model\Config::WITH_PRODUCT_DATA) {
$products = $this->getProducts($order);
$invitation['products'] = $products;
Expand Down
82 changes: 82 additions & 0 deletions Helper/Products.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace Trustpilot\Reviews\Helper;

use Magento\Catalog\Model\Product;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\ConfigurableProduct\Api\LinkManagementInterface;
use Magento\Backend\Helper\Data as BackendHelper;
use Trustpilot\Reviews\Helper\Data;

class Products extends AbstractHelper
{
private $_product;
private $_helper;
private $_linkManagement;
private $_backendHelper;

public function __construct(
Product $product,
Data $helper,
LinkManagementInterface $linkManagement,
BackendHelper $backendHelper)
{
$this->_product = $product;
$this->_helper = $helper;
$this->_linkManagement = $linkManagement;
$this->_backendHelper = $backendHelper;
}

public function checkSkus() {
$data = array();
$page_id = 1;
$productCollection = $this->_product
->getCollection()
->addAttributeToSelect(array('name'))
// ->addAttributeToFilter('type_id', 'configurable')
->setPageSize(20);
$lastPage = $productCollection->getLastPageNumber();
$settings = json_decode($this->_helper->getConfig('master_settings_field'));
$skuSelector = empty($settings->skuSelector) || $settings->skuSelector == 'none' ? 'sku' : $settings->skuSelector;
while ($page_id < $lastPage) {
set_time_limit(30);
$collection = $productCollection->setCurPage($page_id)->load();
if (isset($collection)) {
foreach ($collection as $product) {
$sku = $this->_helper->loadSelector($product, $skuSelector);
// TODO: decide do we want to group products
// $childProducts = array();
// if ($product->getTypeId() == 'configurable') {
// $simpleProductCollection = $this->_linkManagement->getChildren($product->getSku());
// foreach ($simpleProductCollection as $childProduct) {
// $productSku = $this->_helper->loadSelector($childProduct, $skuSelector);
// if (empty($productSku)) {
// $childItem = array();
// $childItem['id'] = $childProduct->getId();
// $childItem['name'] = $childProduct->getName();
// $childItem['type'] = $childProduct->getTypeId();
// $childItem['productAdminUrl'] = $this->_backendHelper->getUrl('catalog/product/edit', array('id' => $childProduct->getId()));
// $childItem['productFrontendUrl'] = $childProduct->getProductUrl();
// array_push($childProducts, $childItem);
// }
// }
// }

if (empty($sku)) {
$item = array();
$item['id'] = $product->getId();
$item['name'] = $product->getName();
$item['type'] = $product->getTypeId();
$item['productAdminUrl'] = $this->_backendHelper->getUrl('catalog/product/edit', array('id' => $product->getId()));
$item['productFrontendUrl'] = $product->getProductUrl();
// $item['childProducts'] = $childProducts;
array_push($data, $item);
}
}
}
$collection->clear();
$page_id = $page_id + 1;
}
return $data;
}
}
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.360';
const TRUSTPILOT_PLUGIN_VERSION = '2.6.371';
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.360",
"version": "2.6.371",
"license": [
"OSL-3.0"
],
Expand Down
24 changes: 24 additions & 0 deletions view/adminhtml/web/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ function receiveSettings(e) {
action['action'] = 'handle_past_orders';
action['issynced'] = 'issynced';
this.submitPastOrdersCommand(action);
} else if (data.startsWith('check_product_skus')) {
const action = {};
action['action'] = 'check_product_skus';
this.submitCheckProductSkusCommand(action);
} else if (data === 'update') {
updateplugin();
} else if (data === 'reload') {
Expand Down Expand Up @@ -91,6 +95,26 @@ function submitPastOrdersCommand(data) {
xhr.send(encodeSettings(data));
}

function submitCheckProductSkusCommand(data) {
data['form_key'] = window.FORM_KEY;
data['scope'] = scope;
data['scopeId'] = scopeId;
const xhr = new XMLHttpRequest();
xhr.open('POST', `${ajaxUrl}?isAjax=true`, true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status >= 400) {
console.log(`callback error: ${xhr.response} ${xhr.status}`);
} else {
// TODO: send data to integration app
console.log(xhr.response);
}
}
};
xhr.send(encodeSettings(data));
}

function submitSettings(parsedData) {
const data = {
action: 'handle_save_changes',
Expand Down

0 comments on commit 7b79a98

Please sign in to comment.