diff --git a/1 b/1
index 63a2414..a312832 100644
--- a/1
+++ b/1
@@ -4,3 +4,4 @@ version 7c
# Write a message for tag:
# 0.0.7b
# Lines starting with '#' will be ignored.
+# is it even required? I guess the file can be removed.
diff --git a/Controller/Index/API.php b/Controller/Index/API.php
index 3fd889f..6499697 100644
--- a/Controller/Index/API.php
+++ b/Controller/Index/API.php
@@ -1,131 +1,199 @@
configHelper = $config;
- $this->cache = $core;
- $this->productModel = $product;
- $this->stockModel = $stockRegistryInterface;
- $this->imageHelper = $image;
- $this->storeModel = $storeManagerInterface;
- $this->productCollectionFactory = $productCollectionFactory;
- }
-
- private function getProductCollection($page, $perPage)
- {
- $collection = $this->productCollectionFactory->create();
- /* Addtional */
- $collection
- ->addMinimalPrice()
- ->addFinalPrice()
- ->addTaxPercents()
- ->addAttributeToSelect('*')
- ->addUrlRewrite()
- ->setPageSize($perPage)
- ->setCurPage($page);
- return $collection;
+ private readonly StockRegistryInterface $stockModel,
+ private readonly Image $imageHelper,
+ private readonly StoreManagerInterface $storeModel,
+ private readonly Config $configHelper,
+ private readonly CollectionFactory $productCollectionFactory,
+ private readonly Http $request,
+ private readonly CategoryCollectionFactory $categoryCollectionFactory,
+ private readonly ResultFactory $resultFactory,
+ ) {
}
+ /**
+ * @inheritDoc
+ *
+ * @throws \Magento\Framework\Exception\LocalizedException
+ * @throws \Magento\Framework\Exception\NoSuchEntityException
+ * @throws \JsonException
+ */
public function execute()
{
- ob_start();
- set_time_limit(0);
-
$store = $this->storeModel->getStore();
$productFeedEnabled = $this->configHelper->isProductFeedEnabled($store->getId());
- $auth['actual_key'] = $this->configHelper->getApiKey($store->getId());
- if ($productFeedEnabled) {
+ if (!$productFeedEnabled) {
+ $result = $this->resultFactory->create(ResultFactory::TYPE_RAW);
+ $result->setContents(
+ json_encode(
+ ['success' => false, 'message' => 'API disabled.'],
+ JSON_THROW_ON_ERROR
+ )
+ );
+
+ return $result;
+ }
+
+ if ($this->canAccessResource($store->getId())) {
+ $result = $this->resultFactory->create(ResultFactory::TYPE_RAW);
+ $result->setContents(
+ json_encode(
+ ['success' => false, 'message' => 'Unauthenticated.'],
+ JSON_THROW_ON_ERROR
+ )
+ );
+
+ return $result;
+ }
- $auth['page'] = !empty($_GET['page']) ? $_GET['page'] : '1';
- $auth['per_page'] = !empty($_GET['per_page']) ? (int) $_GET['per_page'] : 100;
- $auth['submitted_key'] = !empty($_GET['key']) ? $_GET['key'] : '';
+ ob_start();
+ set_time_limit(0);
- //Authenticate
- if(!isset($auth['submitted_key'], $auth['actual_key']) || $auth['actual_key'] != $auth['submitted_key']) {
- echo json_encode(array('success' => false, 'message' => 'Unauthenticated.'));
- die();
+ $products = $this->getProductCollection();
+ $collection = [];
+
+ foreach ($products as $product) {
+ // Load image url via helper. Refers to every occurrence in module
+ $imageUrl = $this->imageHelper->init($product, 'product_page_image_large')->getUrl();
+ # Nested ternary operator should not be used. Change to if or so.
+ $brand = $product->hasData('manufacturer')
+ ?: ($product->hasData('brand') ? $product->getAttributeText('brand') : 'Not Available');
+ $price = $product->getPrice();
+ $finalPrice = $product->getFinalPrice();
+
+ $item = [
+ 'id' => $product->getSku(),
+ 'title' => $product->getName(),
+ 'link' => $product->getProductUrl(),
+ 'price' => !empty($price)
+ ? number_format($price, 2) . " " . $store->getCurrentCurrency()->getCode() : '',
+ 'sale_price' => !empty($finalPrice)
+ ? number_format($finalPrice, 2) . " " . $store->getCurrentCurrency()->getCode() : '',
+ 'image_link' => $imageUrl,
+ 'brand' => $brand,
+ 'mpn' => $product->hasData('mpn') ?: $product->getSku(),
+ 'gtin' => $product->hasData('gtin') ?: ($product->hasData('upc') ?: ''),
+ 'product_type' => $product->getTypeID(),
+ ];
+
+ $item['category'] = [];
+
+ # Basically the same as for Feed. Moreover, it can be moved to Model or Service - Its used by Api and Feed
+ $categoryIds = $product->getCategoryIds();
+ try {
+ $categoryCollection = $this->categoryCollectionFactory->create()
+ ->addAttributeToSelect(['name'])
+ ->addAttributeToFilter('entity_id', $categoryIds);
+ } catch (\Exception $e) {
+ $categoryCollection = null;
}
- $products = $this->getProductCollection($auth['page'], $auth['per_page']);
-
- $collection = [];
-
- foreach ($products as $product) {
- // Load image url via helper.
- $imageUrl = $this->imageHelper->init($product, 'product_page_image_large')->getUrl();
- $brand = $product->hasData('manufacturer') ? $product->getAttributeText('manufacturer') : ($product->hasData('brand') ? $product->getAttributeText('brand') : 'Not Available');
- $price = $product->getPrice();
-
- $finalPrice = $product->getFinalPrice();
-
- $item = [
- 'id' => $product->getSku(),
- 'title' => $product->getName(),
- 'link' => $product->getProductUrl(),
- 'price' => (!empty($price) ? number_format($price, 2) . " " . $store->getCurrentCurrency()->getCode() : ''),
- 'sale_price' => (!empty($finalPrice) ? number_format($finalPrice, 2) . " " . $store->getCurrentCurrency()->getCode() : ''),
- 'image_link' => $imageUrl,
- 'brand' => $brand,
- 'mpn' => ($product->hasData('mpn') ? $product->getData('mpn') : $product->getSku()),
- 'gtin' => ($product->hasData('gtin') ? $product->getData('gtin') : ($product->hasData('upc') ? $product->getData('upc') : '')),
- 'product_type' => $product->getTypeID(),
- ];
-
- $item['category'] = [];
-
- $categoryCollection = $product->getCategoryCollection();
- if (count($categoryCollection) > 0) {
- foreach ($categoryCollection as $category) {
- $item['category'][] = $category->getName();
- }
+ if ($categoryCollection !== null && count($categoryCollection) > 0) {
+ foreach ($categoryCollection as $category) {
+ $item['category'][] = $category->getName();
}
+ }
- $stock = $this->stockModel->getStockItem(
- $product->getId(),
- $product->getStore()->getWebsiteId()
- );
+ # Basically the same as for Feed. Moreover, it can be moved to Model or Service - Its used by Api and Feed
+ $stock = $this->stockModel->getStockItem(
+ $product->getId(),
+ $product->getStore()->getWebsiteId()
+ );
+ $item['in_stock'] = (bool)$stock->getIsInStock();
- $item['in_stock'] = $stock->getIsInStock() ? true : false;
+ $collection[] = $item;
+ }
- $collection[] = $item;
- }
+ $result = $this->resultFactory->create(ResultFactory::TYPE_RAW);
+ $result->setContents(
+ json_encode(
+ ['success' => true, 'products' => $collection, 'total' => count($collection)],
+ JSON_THROW_ON_ERROR
+ )
+ );
- echo json_encode(array('success' => true, 'products' => $collection, 'total' => count($collection)));
- die();
+ return $result;
+ }
- } else {
- echo json_encode(array('success' => false, 'message' => 'API disabled.'));
- die();
+ /**
+ * Check if Controller can be accessed.
+ *
+ * @param int $storeId
+ *
+ * @return bool
+ */
+ private function canAccessResource(int $storeId): bool
+ {
+ $auth['actual_key'] = $this->configHelper->getApiKey($storeId);
+ $auth['submitted_key'] = $this->request->getParam('key') ?: '';
+
+ //Authenticate
+ if (!isset($auth['submitted_key'], $auth['actual_key']) || $auth['actual_key'] != $auth['submitted_key']) {
+ return false;
}
+
+ return true;
+ }
+
+ /**
+ * Provide page of product collection
+ *
+ * # Basically the same as for Feed. Can be moved to Model
+ *
+ * @return Collection
+ */
+ private function getProductCollection(): Collection
+ {
+ $page = $this->request->getParam('page') ?: 0;
+ $perPage = $this->request->getParam('per_page') ?: 100;
+
+ $collection = $this->productCollectionFactory->create();
+ $collection
+ ->addMinimalPrice()
+ ->addFinalPrice()
+ ->addTaxPercents()
+ ->addAttributeToSelect(['name', 'manufacturer', 'gtin', 'brand', 'image', 'upc', 'mpn'])
+ ->addUrlRewrite()
+ ->setPageSize($perPage)
+ ->setCurPage($page);
+
+ return $collection;
}
}
diff --git a/Controller/Index/Feed.php b/Controller/Index/Feed.php
index 9afc598..aa92f43 100755
--- a/Controller/Index/Feed.php
+++ b/Controller/Index/Feed.php
@@ -1,226 +1,272 @@
configHelper = $config;
- $this->cache = $core;
- $this->productModel = $product;
- $this->stockModel = $stockRegistryInterface;
- $this->imageHelper = $image;
- $this->storeModel = $storeManagerInterface;
- $this->productCollectionFactory = $productCollectionFactory;
- $this->configurableType = $configurableType;
- $this->curl = $curl;
- $this->resultFactory = $resultFactory;
}
- private function getProductCollection()
- {
- $collection = $this->productCollectionFactory->create();
- /* Addtional */
- $collection
- ->addMinimalPrice()
- ->addFinalPrice()
- ->addTaxPercents()
- ->addAttributeToSelect('*')
- ->addUrlRewrite();
- return $collection;
- }
-
- private function validateImageUrl($imageUrl)
+ /**
+ * @inheritDoc
+ */
+ public function execute()
{
- $options = [
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_HEADER => true,
- CURLOPT_NOBODY => true,
- ];
-
- if (strpos($imageUrl, "Magento_Catalog/images/product/placeholder/image.jpg") !== false) {
- return false;
- }
+ $store = $this->storeModel->getStore();
- return true;
- }
+ if (!$this->configHelper->isProductFeedEnabled($store->getId())) {
+ $result = $this->resultFactory->create(ResultFactory::TYPE_RAW);
+ $result->setContents("Product Feed is disabled");
- private function validateVariantUrl($url)
- {
- $options = [
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_HEADER => true,
- CURLOPT_NOBODY => true,
- ];
-
- try {
- $this->curl->get($url);
- $this->curl->setOptions($options);
-
- if ($this->curl->getStatus() == 200) {
- return true;
- }
- } catch (\Exception $e) {
- return false;
+ return $result;
}
- }
- public function execute()
- {
// Set timelimit to 0 to avoid timeouts when generating feed.
ob_start();
set_time_limit(0);
-
- $store = $this->storeModel->getStore();
-
- $productFeedEnabled = $this->configHelper->isProductFeedEnabled($store->getId());
- if ($productFeedEnabled) {
- // TODO:- Implement caching of Feed
- $productFeed = "
-
-
- getName() . "]]>
- " . $store->getBaseUrl() . "";
-
- $products = $this->getProductCollection();
-
- foreach ($products as $product) {
- $parentProductId = null;
- $groupedParentId = null;
- $configurableParentId = null;
- $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
-
- if ($objectManager->create('Magento\GroupedProduct\Model\Product\Type\Grouped')->getParentIdsByChild($product->getId())) {
- $groupedParentId = $objectManager->create('Magento\GroupedProduct\Model\Product\Type\Grouped')->getParentIdsByChild($product->getId());
- }
- if ($objectManager->create('Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable')->getParentIdsByChild($product->getId())) {
- $configurableParentId = $objectManager->create('Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable')->getParentIdsByChild($product->getId());
- }
-
- $parentId = null;
- $parentProduct = null;
-
- if (isset($groupedParentId[0])) {
- $parentId = $groupedParentId[0];
- } else if (isset($configurableParentId[0])) {
- $parentId = $configurableParentId[0];
- }
-
+ # Basically not good solution. Even with that, timeout can be thrown with many products.
+ # Consider using pagination as for API Controller and remove the ob_start and set_time_limit
+ # Eventually create cron, that would create the Feed in the background and the controller would only display it
+
+ // TODO:- Implement caching of Feed
+ $productFeed = "
+
+
+ getName() . "]]>
+ " . $store->getBaseUrl() . "";
+
+ $page = $this->request->getParam('page') ?: 0;
+ # If You will use Pages and PageSizes, then remove `do / while` block, and use it as in API Controller.
+ do {
+ $productCollection = $this->getProductCollection($page);
+
+ /** @var ProductInterface $product */
+ foreach ($productCollection as $product) {
// Load image url via helper.
$productImageUrl = $this->imageHelper->init($product, 'product_page_image_large')->getUrl();
$imageLink = $productImageUrl;
$productUrl = $product->getProductUrl();
- if (isset($parentId)) {
- $parentProduct = $objectManager->create('Magento\Catalog\Model\Product')->load($parentId);
+ /** @var ProductInterface $parentProduct */
+ $parentProduct = $this->provideParentProduct($product);
+ if ($parentProduct !== null) {
+ $parentProductImageUrl = $this->imageHelper
+ ->init($parentProduct, 'product_page_image_large')->getUrl();
- $parentProductImageUrl = $this->imageHelper->init($parentProduct, 'product_page_image_large')->getUrl();
- $validVariantImage = $this->validateImageUrl($productImageUrl);
- if (!$validVariantImage) {
+ if (!$this->validateImageUrl($productImageUrl)) {
$imageLink = $parentProductImageUrl;
}
$productUrl = $parentProduct->getProductUrl();
}
- $brand = $product->hasData('manufacturer') ? $product->getAttributeText('manufacturer') : ($product->hasData('brand') ? $product->getAttributeText('brand') : 'Not Available');
+ if ($product->hasData('brand')) {
+ $brand = $product->hasData('manufacturer') ? $product->getAttributeText('manufacturer')
+ : ($product->getAttributeText('brand'));
+ } else {
+ $brand = $product->hasData('manufacturer') ? $product->getAttributeText('manufacturer')
+ : ('Not Available');
+ }
+
$price = $product->getPrice();
$finalPrice = $product->getFinalPrice();
- $productFeed .= "
- getSku() . "]]>
- getName() . "]]>
-
- " . (!empty($price) ? number_format($price, 2) . " " . $store->getCurrentCurrency()->getCode() : '') . "
- " . (!empty($finalPrice) ? number_format($finalPrice, 2) . " " . $store->getCurrentCurrency()->getCode() : '') . "
-
- new
-
-
- hasData('mpn') ? $product->getData('mpn') : $product->getSku()) . "]]>
- hasData('gtin') ? $product->getData('gtin') : ($product->hasData('upc') ? $product->getData('upc') : '')) . "]]>
- getTypeID() . "]]>
-
- UK
- Standard Free Shipping
- 0 GBP
- ";
-
- $categoryCollection = $product->getCategoryCollection();
- if (count($categoryCollection) > 0) {
+ # I dont think, UK should be hardcoded as Shipping. The implementation of it is not very pretty.
+ $productFeed .= "
+
+ getSku() . "]]>
+ getName() . "]]>
+
+ " . (!empty($price) ? number_format($price, 2) . " " . $store->getCurrentCurrency()->getCode() : '') . "
+ " . (!empty($finalPrice) ? number_format($finalPrice, 2) . " " . $store->getCurrentCurrency()->getCode() : '') . "
+
+ new
+
+
+ hasData('mpn') ?: $product->getSku()) . "]]>
+ hasData('gtin') ?: ($product->hasData('upc') ?: '')) . "]]>
+ getTypeID() . "]]>
+
+ UK
+ Standard Free Shipping
+ 0 GBP
+ ";
+
+ # If You really need to provide also Category names, the Category collection have to be loaded as well.
+ # It is not very optimized for many products.
+ $categoryIds = $product->getCategoryIds();
+ try {
+ $categoryCollection = $this->categoryCollectionFactory->create()
+ ->addAttributeToSelect(['name'])
+ ->addAttributeToFilter('entity_id', $categoryIds);
+ } catch (\Exception $e) {
+ $categoryCollection = null;
+ }
+
+ if ($categoryCollection !== null && count($categoryCollection) > 0) {
foreach ($categoryCollection as $category) {
- $productFeed .= "getName() . "]]>";
+ $productFeed .= sprintf(
+ "\n\t\t\t",
+ $category->getName()
+ );
}
}
+ # The StockRegistryInterface is deprecated. Implemented logic will not reflect real Stock Status.
+ # See https://developer.adobe.com/commerce/php/development/components/web-api/inventory-management/
+ # For quicker resolve, consider using $product->isSalable() instead $stock->getIsInStock()
+ # Otherwise if Shop uses MSI load StockInventories and check availability there
$stock = $this->stockModel->getStockItem(
$product->getId(),
$product->getStore()->getWebsiteId()
);
if ($stock->getIsInStock()) {
- $productFeed .= "in stock";
+ #if ($product->isSalable()) {
+ $productFeed .= "\n\t\t\tin stock";
} else {
- $productFeed .= "out of stock";
+ $productFeed .= "\n\t\t\tout of stock";
}
- $productFeed .= "";
- $parentProduct = null;
+ $productFeed .= "\n\t\t";
}
- $productFeed .= "";
+ $page++;
+ } while ($productCollection->count());
- // TODO:- Implement caching of feed
+ $productFeed .= "\n\t\n";
- $result = $this->resultFactory->create(ResultFactory::TYPE_RAW);
- $result->setContents($productFeed);
+ // TODO:- Implement caching of feed
+ $result = $this->resultFactory->create(ResultFactory::TYPE_RAW);
+ $result->setContents($productFeed);
- return $result;
- // exit();
- } else {
- print "Product Feed is disabled.";
+ ob_end_clean(); #Should occur when using ob_start
+
+ return $result;
+ }
+
+ /**
+ * Provide page of product collection
+ *
+ * @param int $page
+ *
+ * @return Collection
+ */
+ private function getProductCollection(int $page): Collection
+ {
+ $perPage = $this->request->getParam('per_page') ?: 100;
+
+ $collection = $this->productCollectionFactory->create();
+ # If You want to use Collection, do not load all Attributes if not required. Especially with big collection.
+ # Add just required attributes.
+ $collection
+ ->addMinimalPrice()
+ ->addFinalPrice()
+ ->addTaxPercents()
+ ->addAttributeToSelect(['name', 'manufacturer', 'gtin', 'brand', 'image', 'upc', 'mpn'])
+ ->addUrlRewrite()
+ ->setPageSize($perPage)
+ ->setCurPage($page);
+
+ return $collection;
+ }
+
+ /**
+ * Provide Parent Product if available
+ *
+ * @param ProductInterface $product
+ *
+ * @return ProductInterface|null
+ */
+ private function provideParentProduct(ProductInterface $product): ?ProductInterface
+ {
+ $parentId = null;
+ if ($this->groupedProductModel->getParentIdsByChild($product->getId())) {
+ $groupedParentId = $this->groupedProductModel->getParentIdsByChild($product->getId());
+ if (isset($groupedParentId[0])) {
+ $parentId = $groupedParentId[0];
+ }
+ }
+ if ($this->configurableProductModel->getParentIdsByChild($product->getId())) {
+ $configurableParentId = $this->configurableProductModel->getParentIdsByChild($product->getId());
+ if (isset($configurableParentId[0])) {
+ $parentId = $configurableParentId[0];
+ }
}
+
+ $parentProduct = null;
+ if (isset($parentId)) {
+ try {
+ $parentProduct = $this->productRepositoryFactory->create()->getById($parentId);
+ } catch (\Exception $e) {
+ }
+ }
+
+ return $parentProduct;
+ }
+
+ /**
+ * Validate Image Url
+ *
+ * @param string $imageUrl
+ *
+ * @return bool
+ */
+ private function validateImageUrl(string $imageUrl): bool
+ {
+ if (str_contains($imageUrl, "Magento_Catalog/images/product/placeholder/image.jpg")) {
+ return false;
+ }
+
+ return true;
}
}
diff --git a/Observer/SendOrderDetails.php b/Observer/SendOrderDetails.php
index 45c2431..deeba20 100755
--- a/Observer/SendOrderDetails.php
+++ b/Observer/SendOrderDetails.php
@@ -1,90 +1,144 @@
configHelper = $config;
- $this->apiModel = $api;
- $this->productModel = $product;
- $this->imageHelper = $image;
- $this->configProductModel = $configurable;
}
- public function execute(Framework\Event\Observer $observer)
+ /**
+ * @inheritDoc
+ */
+ public function execute(Observer $observer)
{
$shipment = $observer->getEvent()->getShipment();
+ /** @var OrderInterface $order */
$order = $shipment->getOrder();
- $this->dispatchNotification($order);
+
+ if ($this->canSendOrderDetails($order)) {
+ $this->dispatchNotification($order);
+ }
}
- public function dispatchNotification($order)
+ /**
+ * Send Order data to Review
+ *
+ * @param OrderInterface $order
+ *
+ * @return void
+ */
+ public function dispatchNotification(OrderInterface $order): void
{
- try {
- $magento_store_id = $order->getStoreId();
+ $storeId = $order->getStoreId();
+ /** @var OrderItemInterface[] $items */
+ $orderItems = $order->getAllVisibleItems();
- if ($this->configHelper->getStoreId($magento_store_id) && $this->configHelper->getApiKey($magento_store_id) && $this->configHelper->isProductReviewsEnabled($magento_store_id)) {
- $items = $order->getAllVisibleItems();
- $p = array();
- foreach ($items as $item) {
+ $itemIds = [];
+ /** @var OrderItemInterface $orderItem */
+ foreach ($orderItems as $orderItem) {
+ $itemIds[] = $orderItem->getProductId();
+ }
+ $productCollection = $this->productCollectionFactory->create()
+ ->addAttributeToSelect(['name', 'url', 'image'])
+ ->addAttributeToFilter('entity_id', ['in' => $itemIds]);
- if ($this->configHelper->isUsingGroupSkus($magento_store_id)) {
- // If product is part of a configurable product, use the configurable product details.
- if ($item->getProduct()->getTypeId() == 'simple' || $item->getProduct()->getTypeId() == \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE) {
- $productId = $item->getProduct()->getId();
- $model = $this->productModel->create();
- $item = $model->load($productId);
- }
- }
- $imageUrl = $this->imageHelper->init($item, 'product_page_image_large')->getUrl();
- $p[] = [
- 'image' => $imageUrl,
- 'id' => $item->getId(),
- 'sku' => $item->getSku(),
- 'name' => $item->getName(),
- 'pageUrl' => $item->getProductUrl()
- ];
- }
+ $productData = [];
+ foreach ($productCollection as $item) {
+ # The whole IF makes no sense. Right now, if the isUsingGroupSkus is true, then literally:
+ # Check if Product is Simple OR Config. If yes, then load Product via repository (->getProduct())
+ # If not, use OrderItemInterface $Item from Order.
+ # Just to check its Id. Then load the same product via Model. Also the comment is not accurate at all.
+ # Especially using `$order->getAllVisibleItems()`. The child products will not be provided here.
+ # Consider removing that IF or refactor it to something useful.
+ # And for God’s sake, do not use deprecated methods like $model->load($productId)
+// if ($this->configHelper->isUsingGroupSkus($storeId)) {
+// // If product is part of a configurable product, use the configurable product details.
+// if ($item->getProduct()->getTypeId() == 'simple' || $item->getProduct()->getTypeId() == Configurable::TYPE_CODE) {
+// $productId = $item->getProduct()->getId();
+// $model = $this->productModel->create();
+// $item = $model->load($productId);
+// }
+// }
- $name = $order->getCustomerName();
+ $imageUrl = $this->imageHelper->init($item, 'product_page_image_large')->getUrl();
+ $productData[] = [
+ 'image' => $imageUrl,
+ 'id' => $item->getId(),
+ 'sku' => $item->getSku(),
+ 'name' => $item->getName(),
+ 'pageUrl' => $item->getProductUrl()
+ ];
+ }
- if ($order->getCustomerIsGuest()) {
- $name = $order->getBillingAddress()->getFirstName();
- }
+ $name = $order->getCustomerName();
- $productResponse = $this->apiModel->apiPost('/invitation', [
- 'source' => 'magento',
- 'name' => $name,
- 'email' => $order->getCustomerEmail(),
- 'order_id' => $order->getRealOrderId(),
- 'country_code' => $order->getShippingAddress()->getCountryId(),
- 'products' => $p
- ], $magento_store_id);
+ if ($order->getCustomerIsGuest()) {
+ $name = $order->getBillingAddress()->getFirstName();
+ }
- $this->apiModel->addStatusMessage($productResponse, "Product Review Invitation");
+ try {
+ $productResponse = $this->apiModel->apiPost('/invitation', [
+ 'source' => 'magento',
+ 'name' => $name,
+ 'email' => $order->getCustomerEmail(),
+ 'order_id' => $order->getRealOrderId(),
+ 'country_code' => $order->getShippingAddress()->getCountryId(),
+ 'products' => $productData
+ ], $storeId);
- }
+ $this->apiModel->addStatusMessage($productResponse, "Product Review Invitation");
} catch (\Exception $e) {
}
}
+
+ /**
+ * Check if Order can be sent to Review
+ *
+ * @param OrderInterface $order
+ *
+ * @return bool
+ */
+ private function canSendOrderDetails(OrderInterface $order): bool
+ {
+ if ($this->configHelper->getStoreId($order->getStoreId())
+ && $this->configHelper->getApiKey($order->getStoreId())
+ && $this->configHelper->isProductReviewsEnabled($order->getStoreId())
+ ) {
+ return true;
+ }
+ return false;
+ }
}
diff --git a/composer.json b/composer.json
index 53b7392..04d71fc 100755
--- a/composer.json
+++ b/composer.json
@@ -8,6 +8,12 @@
"registration.php"
]
},
+ "require": {
+ "magento/module-sales": "^102.0.0 || ^103.0.0",
+ "magento/module-catalog": "^102.0.0 || ^103.0.0",
+ "magento/framework": "^102.0.0 || ^103.0.0",
+ "php": "~8.1"
+ },
"description": "Reviews.co.uk Magento 2 Integration",
"type": "magento2-module",
"authors": [
diff --git a/etc/events.xml b/etc/events.xml
index 2c489d1..1e170b4 100755
--- a/etc/events.xml
+++ b/etc/events.xml
@@ -1,6 +1,11 @@
+
diff --git a/etc/module.xml b/etc/module.xml
index 2803951..d31d8b8 100755
--- a/etc/module.xml
+++ b/etc/module.xml
@@ -4,6 +4,8 @@
+
+
-
\ No newline at end of file
+
diff --git a/modman b/modman
index 715b768..a86ae1c 100755
--- a/modman
+++ b/modman
@@ -1,3 +1,4 @@
# Modman configuration file
+# is MOdman still in use? I gues it may be removed
-/ app/code/Reviewscouk/Reviews
\ No newline at end of file
+/ app/code/Reviewscouk/Reviews
diff --git a/view/frontend/templates/product/rating_snippet_element.phtml b/view/frontend/templates/product/rating_snippet_element.phtml
index ede7a6a..493db98 100644
--- a/view/frontend/templates/product/rating_snippet_element.phtml
+++ b/view/frontend/templates/product/rating_snippet_element.phtml
@@ -1,8 +1,10 @@
isProductRatingSnippetWidgetEnabled()) {
+# add copyright block.
+# Add definition of Block.
+# Use escaper instead of $block for escaping
+if ($block->isProductRatingSnippetWidgetEnabled()):
$_data = $block->getSettings();
$_skus = $block->getProductSkus();
?>
-
diff --git a/view/frontend/templates/product/review_widget.phtml b/view/frontend/templates/product/review_widget.phtml
index b6325fc..0436be3 100755
--- a/view/frontend/templates/product/review_widget.phtml
+++ b/view/frontend/templates/product/review_widget.phtml
@@ -1,15 +1,38 @@
isProductWidgetEnabled() && $block->isTabMode()) {
- if ($block->isIframeWidget()) {
- $_data = $block->getSettings();
- $_skus = $block->getProductSkus();
+/**
+ * Add Your COPPYRIGHTS here
+ *
+ * See COPYING.txt for license details.
+ */
+
+/**
+ * @var $block \Reviewscouk\Reviews\Block\Product\Reviewwidget
+ * @var $escaper \Magento\Framework\Escaper
+ */
+if (!isset($escaper)) {
+ $escaper = $block;
+}
+
+# In phtml templates do not use normal `if () {}` statements. Also, reduce the length of ifs and complicity as much as possible.
+# It would be much better to prepare the Child Templates and call them inside IFS, to have separated templates per different Widget
+# Also be consistent in using it. Some templates use `if():` statements, some `if(){}`...
+if (!($block->isProductWidgetEnabled() && $block->isTabMode())):
+ print $block->getStaticWidget();
+else:
+ $_data = $block->getSettings();
+ $_skus = $block->getProductSkus();
+
+ if ($block->isIframeWidget()):
?>
+
+
- isPolarisWidget()) {
- $_data = $block->getSettings();
- $_skus = $block->getProductSkus();
-
- ?>
+ isPolarisWidget()): ?>
+
-
-
-getStaticWidget();
- }
-}
-?>
\ No newline at end of file
+
+