Skip to content

Commit

Permalink
Merge pull request #353 from mageplaza/2.4-develop
Browse files Browse the repository at this point in the history
2.4 develop
  • Loading branch information
Victor-Mageplaza authored Dec 28, 2021
2 parents 49d82c7 + 14733e1 commit 6043fca
Show file tree
Hide file tree
Showing 7 changed files with 565 additions and 22 deletions.
174 changes: 153 additions & 21 deletions Helper/EmailMarketing.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,14 @@
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Helper\Data as CatalogHelper;
use Magento\Catalog\Helper\Product\Configuration as CatalogConfiguration;
use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\CategoryFactory;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Type;
use Magento\Catalog\Model\Product\Visibility;
use Magento\Catalog\Model\ProductRepository;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
use Magento\Bundle\Model\Product\Type as Bundle;
use Magento\Customer\Model\Address\Config;
use Magento\Customer\Model\Attribute;
use Magento\Customer\Model\Customer;
Expand All @@ -51,9 +57,11 @@
use Magento\Framework\HTTP\Client\CurlFactory;
use Magento\Framework\HTTP\Client\Curl;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Phrase;
use Magento\Framework\Stdlib\DateTime;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
use Magento\Framework\UrlInterface;
use Magento\GroupedProduct\Model\Product\Type\Grouped;
use Magento\Newsletter\Model\Subscriber;
use Magento\Newsletter\Model\SubscriberFactory;
use Magento\Quote\Model\Quote;
Expand Down Expand Up @@ -291,6 +299,26 @@ class EmailMarketing extends Data
*/
protected $smtpVersion = '';

/**
* @var CategoryFactory
*/
protected $categoryFactory;

/**
* @var Configurable
*/
protected $configurable;

/**
* @var Grouped
*/
protected $grouped;

/**
* @var Bundle
*/
protected $bundle;

/**
* EmailMarketing constructor.
*
Expand Down Expand Up @@ -324,6 +352,12 @@ class EmailMarketing extends Data
* @param Region $region
* @param Collection $abandonedCartCollection
* @param ItemFactory $quoteItemFactory
* @param ComponentRegistrarInterface $componentRegistrar
* @param ReadFactory $readFactory
* @param CategoryFactory $categoryFactory
* @param Configurable $configurable
* @param Grouped $grouped
* @param Bundle $bundle
*/
public function __construct(
Context $context,
Expand Down Expand Up @@ -357,7 +391,11 @@ public function __construct(
Collection $abandonedCartCollection,
ItemFactory $quoteItemFactory,
ComponentRegistrarInterface $componentRegistrar,
ReadFactory $readFactory
ReadFactory $readFactory,
CategoryFactory $categoryFactory,
Configurable $configurable,
Grouped $grouped,
Bundle $bundle
) {
$this->frontendUrl = $frontendUrl;
$this->escaper = $escaper;
Expand Down Expand Up @@ -386,8 +424,12 @@ public function __construct(
$this->region = $region;
$this->abandonedCartCollection = $abandonedCartCollection;
$this->quoteItemFactory = $quoteItemFactory;
$this->componentRegistrar = $componentRegistrar;
$this->readFactory = $readFactory;
$this->componentRegistrar = $componentRegistrar;
$this->readFactory = $readFactory;
$this->categoryFactory = $categoryFactory;
$this->configurable = $configurable;
$this->grouped = $grouped;
$this->bundle = $bundle;

parent::__construct($context, $objectManager, $storeManager);
}
Expand Down Expand Up @@ -430,7 +472,7 @@ public function getResourceQuote()
*/
public function getProductOptions(Item $item)
{
if ($item->getProductType() === 'bundle') {
if ($item->getProductType() === Type::TYPE_BUNDLE) {
return $this->bundleProductConfiguration->getOptions($item);
}

Expand Down Expand Up @@ -922,6 +964,7 @@ public function getShipmentOrCreditmemoItems($object)
{
$items = [];
foreach ($object->getItems() as $item) {
/** @var OrderItem $orderItem */
$orderItem = $item->getOrderItem();
$product = $this->getProductFromItem($orderItem);
$createdAt = $item->getCreatedAt();
Expand All @@ -947,7 +990,7 @@ public function getShipmentOrCreditmemoItems($object)
'is_utc' => true,
'created_at' => $this->formatDate($createdAt),
'updated_at' => $this->formatDate($updatedAt),
'categories' => $product->getCategoryIds()
'categories' => $this->getCategories($product->getCategoryIds())
];

continue;
Expand All @@ -962,25 +1005,29 @@ public function getShipmentOrCreditmemoItems($object)
continue;
}

$productType = $orderItem->getData('product_type');
$productType = $orderItem->getData('product_type');
$isBundle = $productType === Type::TYPE_BUNDLE;
$sku = $isBundle ? $product->getSku() : $item->getData('sku');
$products = $this->getProductBySku($orderItem, $sku);

$items[$orderItem->getId()] = [
'type' => $productType,
'name' => $productType === 'configurable' ?
'name' => $productType === Configurable::TYPE_CODE ?
$this->getItemOptions($orderItem) : $item->getName(),
'title' => $item->getName(),
'price' => (float) $item->getBasePrice(),
'quantity' => $item->getQty(),
'sku' => $item->getSku(),
'product_id' => $item->getProductId(),
'image' => $this->getProductImage($product),
'frontend_link' => $product->getProductUrl(),
'frontend_link' => $products->getProductUrl() ?: ($product->getProductUrl() ?: '#'),
'is_utc' => true,
'created_at' => $this->formatDate($createdAt),
'updated_at' => $this->formatDate($updatedAt),
'categories' => $product->getCategoryIds()
'categories' => $this->getCategories($products->getCategoryIds())
];

if ($productType === 'bundle') {
if ($productType === Type::TYPE_BUNDLE) {
$items[$orderItem->getId()]['bundle_items'] = [];
}
}
Expand Down Expand Up @@ -1025,7 +1072,7 @@ public function formatOptions($item, $options)
}

/**
* @param Item $orderItem
* @param OrderItem|Item $orderItem
*
* @return string
*/
Expand Down Expand Up @@ -1105,8 +1152,8 @@ public function getCartItems($object)
$productType = $item->getData('product_type');

$bundleItems = [];
$hasVariant = $productType === 'configurable';
$isBundle = $productType === 'bundle';
$hasVariant = $productType === Configurable::TYPE_CODE;
$isBundle = $productType === Type::TYPE_BUNDLE;
$name = $item->getName();
if ($hasVariant) {
if ($isQuote) {
Expand All @@ -1116,8 +1163,8 @@ public function getCartItems($object)
}
}

$sku = $isBundle ? $item->getProduct()->getSku() : $item->getData('sku');
$products = $this->productRepository->get($sku);
$sku = $isBundle ? $product->getSku() : $item->getData('sku');
$products = $this->getProductBySku($item, $sku);

if (is_object($products->getCustomAttribute($this->getDefineVendor()))) {
$vendorValue = $products->getAttributeText($this->getDefineVendor());
Expand All @@ -1130,17 +1177,18 @@ public function getCartItems($object)
'title' => $item->getName(),
'name' => $name,
'price' => (float) $item->getBasePrice(),
'tax_price' => (float) $item->getBaseTaxAmount(),
'tax_price' => (float) ($isQuote ?
$item->getBaseTaxAmount() / $item->getQty() : $item->getBaseTaxAmount() / $item->getQtyOrdered()),
'quantity' => (int) ($isQuote ? $item->getQty() : $item->getQtyOrdered()),
'sku' => $item->getSku(),
'product_id' => $item->getProductId(),
'image' => $this->getProductImage($product),
'frontend_link' => $product->getProductUrl() ?: '#',
'frontend_link' => $products->getProductUrl() ?: ($product->getProductUrl() ?: '#'),
'vendor' => $vendorValue,
'is_utc' => true,
'created_at' => $this->formatDate($createdAt),
'updated_at' => $this->formatDate($updatedAt),
'categories' => $product->getCategoryIds()
'categories' => $this->getCategories($products->getCategoryIds())
];

if ($isQuote) {
Expand Down Expand Up @@ -1170,7 +1218,7 @@ public function getCartItems($object)
'is_utc' => true,
'created_at' => $this->formatDate($createdAt),
'updated_at' => $this->formatDate($updatedAt),
'categories' => $product->getCategoryIds()
'categories' => $this->getCategories($product->getCategoryIds())
];
}
}
Expand Down Expand Up @@ -1273,7 +1321,7 @@ public function setHeaders($data, $url = '', $appID = '', $secretKey = '', $isTe
}

/**
* @return \Magento\Framework\Phrase|string|void
* @return Phrase|string|void
*/
public function getSMTPVersion()
{
Expand All @@ -1288,7 +1336,8 @@ public function getSMTPVersion()
* Get module composer version
*
* @param string $moduleName
* @return \Magento\Framework\Phrase|string|void
*
* @return Phrase|string|void
*/
public function getModuleVersion($moduleName)
{
Expand Down Expand Up @@ -1835,4 +1884,87 @@ public function sendRequestProxy($url, $data)

return $bodyData;
}

/**
* @param array|null $categoryIds
*
* @return array
*/
public function getCategories($categoryIds)
{
$categories = [];

if ($categoryIds) {
foreach ($categoryIds as $categoryId) {
/** @var Category $category */
$category = $this->categoryFactory->create()->load($categoryId);
$categories[] = [
'category_id' => $category->getId(),
'category_name' => $category->getName()
];
}
}

return $categories;
}

/**
* @param int $childId
*
* @return mixed|null
*/
public function getParentId($childId)
{
$productId = null;
/* for simple product of configurable product */
$parentIds = $this->configurable->getParentIdsByChild($childId);

if (isset($parentIds[0])) {
return $parentIds[0];
}

/* for simple product of Group product */
$parentIds = $this->grouped->getParentIdsByChild($childId);

if (isset($parentIds[0])) {
return $parentIds[0];
}

$parentIds = $this->bundle->getParentIdsByChild($childId);

if (isset($parentIds[0])) {
return $parentIds[0];
}

return $productId;
}

/**
* @param OrderItem $item
* @param string $sku
*
* @return ProductInterface|Product|DataObject|mixed|null
*/
public function getProductBySku($item, $sku)
{
try {
$products = $this->productRepository->get($sku);
$buyRequest = $item->getBuyRequest();
if ($buyRequest && (int) $products->getVisibility() === Visibility::VISIBILITY_NOT_VISIBLE) {
if ($buyRequest->getData('super_product_config')) {
$productId = $buyRequest->getData('super_product_config')['product_id'];
} elseif ($buyRequest->getData('super_attribute')) {
$productId = $buyRequest->getData('product');
} else {
$productId = $this->getParentId($products->getId());
}

$products = $this->productRepository->getById($productId);
}
} catch (Exception $e) {
$products = new DataObject([]);
}

return $products;
}
}
Loading

0 comments on commit 6043fca

Please sign in to comment.