Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use product title from default store in tracking of view_item and purchase_item #37

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 44 additions & 6 deletions Model/DataMapping/FieldValueRenderer/OrderItem/Name.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
<?php

/**
* @author Bloomreach
* @copyright Copyright (c) Bloomreach (https://www.bloomreach.com/)
*/

declare(strict_types=1);

namespace Bloomreach\EngagementConnector\Model\DataMapping\FieldValueRenderer\OrderItem;

use Bloomreach\EngagementConnector\Model\DataMapping\FieldValueRenderer\RenderInterface;
use Bloomreach\EngagementConnector\Model\ResourceModel\OrderItem\ChildItems;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
use Magento\Framework\Api\AbstractSimpleObject;
use Magento\Framework\Model\AbstractModel;
use Magento\Sales\Api\Data\OrderItemInterface;
use Magento\Store\Model\StoreManagerInterface;


/**
* The class is responsible for rendering the value of order item name field
Expand All @@ -24,12 +29,29 @@ class Name implements RenderInterface
*/
private $childItems;

/**
* @var ProductRepositoryInterface
*/
private $productRepository;

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @param ChildItems $childItems
* @param ProductRepositoryInterface $productRepository
* @param StoreManagerInterface $storeManager
*/
public function __construct(ChildItems $childItems)
{
public function __construct(
ChildItems $childItems,
ProductRepositoryInterface $productRepository,
StoreManagerInterface $storeManager
) {
$this->childItems = $childItems;
$this->productRepository = $productRepository;
$this->storeManager = $storeManager;
}

/**
Expand All @@ -42,7 +64,21 @@ public function __construct(ChildItems $childItems)
*/
public function render($entity, string $fieldCode)
{
$name = $entity->getName();
$productId = $entity->getProductId();
$defaultStoreId = (int) $this->storeManager->getDefaultStoreView()->getId();

if ($productId) {
try {
$product = $this->productRepository->getById($productId, false, $defaultStoreId);
} catch (NoSuchEntityException $e) {
}
}

if ($product) {
$name = $product->getName();
} else {
$name = $entity->getName();
}

if ($entity->getProductType() !== Configurable::TYPE_CODE) {
return $name;
Expand All @@ -52,13 +88,15 @@ public function render($entity, string $fieldCode)
$childrenItems = $entity->getChildrenItems();

if (!$childrenItems) {
$childName = $this->childItems->getChildName((int) $entity->getItemId());

// $childName = $this->childItems->getChildName((int) $entity->getItemId());
$childProduct = $this->productRepository->getById($entity->getProductId(), false, $defaultStoreId);
$childName = $childProduct->getName();
return $childName ?: $name;
}

foreach ($childrenItems as $childrenItem) {
return $childrenItem->getName();
$childrenItemProduct = $this->productRepository->getById($childrenItem->getProductId(), false, $defaultStoreId);
return $childrenItemProduct->getName() ?: $name;
}

return $name;
Expand Down
16 changes: 14 additions & 2 deletions Model/Tracking/Event/ProductPage/ViewItem.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

/**
* @author Bloomreach
* @copyright Copyright (c) Bloomreach (https://www.bloomreach.com/)
*/

declare(strict_types=1);

namespace Bloomreach\EngagementConnector\Model\Tracking\Event\ProductPage;
Expand All @@ -17,6 +19,7 @@
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\NotFoundException;
use Magento\Framework\View\Element\Block\ArgumentInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
* The class is responsible for generating data for view item event
Expand Down Expand Up @@ -45,22 +48,30 @@ class ViewItem implements ArgumentInterface, EventsInterface
*/
private $eventBuilderFactory;

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @param DataMapperResolver $dataMapperResolver
* @param RequestInterface $request
* @param ProductRepositoryInterface $productRepository
* @param EventBuilderFactory $eventBuilderFactory
* @param StoreManagerInterface $storeManager
*/
public function __construct(
DataMapperResolver $dataMapperResolver,
RequestInterface $request,
ProductRepositoryInterface $productRepository,
EventBuilderFactory $eventBuilderFactory
EventBuilderFactory $eventBuilderFactory,
StoreManagerInterface $storeManager
) {
$this->dataMapperResolver = $dataMapperResolver;
$this->request = $request;
$this->productRepository = $productRepository;
$this->eventBuilderFactory = $eventBuilderFactory;
$this->storeManager = $storeManager;
}

/**
Expand Down Expand Up @@ -92,10 +103,11 @@ private function getCurrentProduct(): ?ProductInterface
{
$product = null;
$productId = (int) $this->request->getParam('id');
$defaultStoreId = (int) $this->storeManager->getDefaultStoreView()->getId();

if ($productId) {
try {
$product = $this->productRepository->getById($productId);
$product = $this->productRepository->getById($productId, false, $defaultStoreId);
} catch (NoSuchEntityException $e) {
return null;
}
Expand Down