Skip to content

Commit

Permalink
Merge pull request #197 from alma/feature/ecom-2038-ac-remove-insuran…
Browse files Browse the repository at this point in the history
…ce-in-cart-when-deactivated

Remove insurance products from active carts when insurance is disabled
  • Loading branch information
Francois-Gomis authored Sep 12, 2024
2 parents 2facd94 + 2bdae3a commit d6fd6c5
Show file tree
Hide file tree
Showing 9 changed files with 488 additions and 30 deletions.
101 changes: 94 additions & 7 deletions Helpers/InsuranceProductHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
namespace Alma\MonthlyPayments\Helpers;

use Alma\MonthlyPayments\Model\Exceptions\AlmaInsuranceProductException;
use Magento\Catalog\Model\Product;
use Alma\MonthlyPayments\Model\Exceptions\AlmaProductException;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Catalog\Model\Product\Visibility;
use Magento\Catalog\Model\ProductFactory;
Expand All @@ -12,18 +13,43 @@
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\Io\File;
use Magento\Framework\Module\Dir;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\Store;

class InsuranceProductHelper extends AbstractHelper
{
/**
* @var ProductFactory
*/
private $productFactory;
/**
* @var File
*/
private $fileProcessor;
/**
* @var Filesystem
*/
private $filesystem;
/**
* @var Logger
*/
private $logger;
/**
* @var Dir
*/
private $directory;
/**
* @var ProductRepository
*/
private $productRepository;
/**
* @var ProductHelper
*/
private $productHelper;

/**
* @param Context $context
Expand All @@ -33,6 +59,7 @@ class InsuranceProductHelper extends AbstractHelper
* @param File $fileProcessor
* @param Filesystem $filesystem
* @param ProductRepository $productRepository
* @param ProductHelper $productHelper
*/
public function __construct(
Context $context,
Expand All @@ -41,26 +68,86 @@ public function __construct(
Dir $directory,
File $fileProcessor,
Filesystem $filesystem,
ProductRepository $productRepository

ProductRepository $productRepository,
ProductHelper $productHelper
) {

parent::__construct($context);
$this->productFactory = $productFactory;
$this->fileProcessor = $fileProcessor;
$this->filesystem = $filesystem;
$this->logger = $logger;
$this->directory = $directory;
$this->productRepository = $productRepository;
$this->productHelper = $productHelper;
}

/**
* Get insurance product
*
* @return ProductInterface
* @throws NoSuchEntityException
*/
public function getInsuranceProduct(): ProductInterface
{
return $this->productRepository->get(InsuranceHelper::ALMA_INSURANCE_SKU, true, Store::DEFAULT_STORE_ID);
}

/**
* Disable insurance product if exist
*
* @return void
* @throws AlmaInsuranceProductException
*/
public function disableInsuranceProductIfExist(): void
{
try {
$insuranceProduct = $this->getInsuranceProduct();
} catch (NoSuchEntityException $e) {
$this->logger->info('Alma insurance product not found no disable needed', [$e->getMessage()]);
return;
}

if ((int)$insuranceProduct->getStatus() === Status::STATUS_DISABLED) {
return;
}

try {
$this->productHelper->disableProduct($insuranceProduct);
} catch (AlmaProductException $e) {
$this->logger->error('Disable insurance product failed with message : ', [$e->getMessage()]);
throw new AlmaInsuranceProductException($e->getMessage(), $e->getCode(), $e);
}
}

public function enableInsuranceProductIfExist(): void
{
try {
$insuranceProduct = $this->getInsuranceProduct();
} catch (NoSuchEntityException $e) {
$this->logger->error('Insurance product not exist', [$e->getMessage()]);
throw new AlmaInsuranceProductException($e->getMessage(), $e->getCode(), $e);
}

if ((int)$insuranceProduct->getStatus() === Status::STATUS_ENABLED) {
return;
}

try {
$this->productHelper->enableProduct($insuranceProduct);

} catch (AlmaProductException $e) {
$this->logger->error('Enable insurance product failed with message : ', [$e->getMessage()]);
}
}

/**
* Create Insurance product in merchant catalogue
*
* @return void
*/
public function createInsuranceProduct(): void
{
// Create a new product with the insurance SKU
/** @var Product $insuranceProduct */
$insuranceProduct = $this->productFactory->create();
$insuranceProduct->setSku(InsuranceHelper::ALMA_INSURANCE_SKU);
$insuranceProduct->setName('Alma Insurance');
Expand All @@ -75,9 +162,9 @@ public function createInsuranceProduct(): void
"manage_stock" => 0,
"use_config_notify_stock_qty" => 0
]);
// Not visible individualy ID 1
// Not visible individually ID 1
$insuranceProduct->setVisibility(Visibility::VISIBILITY_NOT_VISIBLE);
// ID de la classe de taxe (0 pour non applicable)
// Tax class ID 0 for not applicable
$insuranceProduct->setTaxClassId(0);
$insuranceProduct->setDescription('Alma Insurance product');
$insuranceProduct->setUrlKey('alma-insurance');
Expand Down
71 changes: 66 additions & 5 deletions Helpers/ProductHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@

namespace Alma\MonthlyPayments\Helpers;

use Alma\MonthlyPayments\Model\Exceptions\AlmaProductException;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Catalog\Model\ProductRepository;
use Magento\Catalog\Model\ResourceModel\Category\Collection as CategoryCollection;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\StateException;

class ProductHelper
{
Expand All @@ -22,20 +30,27 @@ class ProductHelper
* @var CategoryCollection
*/
private $categoryCollectionFactory;
/**
* @var ProductRepository
*/
private $productRepository;

/**
* @param Logger $logger
* @param CollectionFactory $productCollectionFactory
* @param CategoryCollectionFactory $categoryCollectionFactory
* @param ProductRepository $productRepository
*/
public function __construct(
Logger $logger,
CollectionFactory $productCollectionFactory,
CategoryCollectionFactory $categoryCollectionFactory
Logger $logger,
CollectionFactory $productCollectionFactory,
CategoryCollectionFactory $categoryCollectionFactory,
ProductRepository $productRepository
) {
$this->productCollectionFactory = $productCollectionFactory;
$this->logger = $logger;
$this->categoryCollectionFactory = $categoryCollectionFactory;
$this->productRepository = $productRepository;
}

/**
Expand All @@ -49,7 +64,7 @@ public function getProductsItems(array $productIds): Collection
/** @var Collection $collection */
$collection = $this->productCollectionFactory->create();
$collection->addAttributeToSelect('*');
return $collection->addAttributeToFilter('entity_id', ['in'=>$productIds]);
return $collection->addAttributeToFilter('entity_id', ['in' => $productIds]);
}

/**
Expand All @@ -68,16 +83,62 @@ public function getProductsCategoriesIds(Collection $products): array

return array_values($categoriesId);
}

/**
* Generate categories collection with product a collection
*
* @param Collection $products
* @return CategoryCollection
* @throws LocalizedException
*/
public function getProductsCategories(Collection $products): CategoryCollection
{
$categoriesId = $this->getProductsCategoriesIds($products);

return $this->categoryCollectionFactory->create()->addAttributeToSelect('*')->addFieldToFilter('entity_id', ['in'=>$categoriesId]);
return $this->categoryCollectionFactory->create()->addAttributeToSelect('*')->addFieldToFilter('entity_id', ['in' => $categoriesId]);
}

/**
* Disable a product
*
* @param Product $product
* @return void
* @throws AlmaProductException
*/
public function disableProduct(ProductInterface $product): void
{
$product->setStatus(Status::STATUS_DISABLED);
try {
$this->productRepository->save($product);

} catch (CouldNotSaveException|InputException|StateException $e) {
$this->logger->warning(
"Impossible to disable Product",
['productId' => $product->getId(), 'message' => $e->getMessage()]
);
throw new AlmaProductException($e->getMessage(), $e->getCode(), $e);
}
}

/**
* Enable a product
*
* @param Product $product
* @return void
* @throws AlmaProductException
*/
public function enableProduct(ProductInterface $product): void
{
$product->setStatus(Status::STATUS_ENABLED);
try {
$this->productRepository->save($product);

} catch (CouldNotSaveException|InputException|StateException $e) {
$this->logger->warning(
"Impossible to enable Product",
['productId' => $product->getId(), 'message' => $e->getMessage()]
);
throw new AlmaProductException($e->getMessage(), $e->getCode(), $e);
}
}
}
48 changes: 47 additions & 1 deletion Helpers/QuoteHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@

namespace Alma\MonthlyPayments\Helpers;

use Magento\Framework\DataObject;
use Magento\Quote\Model\ResourceModel\Quote\Item\Collection;
use Magento\Quote\Model\ResourceModel\Quote\Item\CollectionFactory as QuoteItemCollectionFactory;
use Magento\Authorization\Model\UserContextInterface;
use Magento\Checkout\Model\Session;
use Magento\Framework\Exception\NoSuchEntityException;
Expand Down Expand Up @@ -60,6 +63,11 @@ class QuoteHelper
*/
private $quoteId;

/**
* @var QuoteItemCollectionFactory
*/
private $quoteItemCollectionFactory;

/**
* @param Logger $logger
* @param UserContextInterface $userContext
Expand All @@ -70,13 +78,15 @@ public function __construct(
Logger $logger,
UserContextInterface $userContext,
QuoteRepository $quoteRepository,
Session $checkoutSession
Session $checkoutSession,
QuoteItemCollectionFactory $quoteItemCollectionFactory
) {
$this->logger = $logger;
$this->userContext = $userContext;
$this->quoteRepository = $quoteRepository;
$this->checkoutSession = $checkoutSession;
$this->quoteId=null;
$this->quoteItemCollectionFactory = $quoteItemCollectionFactory;
}

/**
Expand Down Expand Up @@ -191,4 +201,40 @@ private function getQuoteIdFromSession():?int
{
return $this->checkoutSession->getQuoteId();
}


/**
* Get quote items with insurance data for quote without reserved order id and delete insurance data
*
* @return void
*/
public function deleteInsuranceDataFromQuoteItemForNotConvertedQuote(): void
{
$quoteCollection = $this->getQuoteItemsWithInsuranceData();
$quoteItems = $quoteCollection->getItems();
foreach ($quoteItems as $quoteItem) {
$quoteItem->setData(InsuranceHelper::ALMA_INSURANCE_DB_KEY);
}
$quoteCollection->save();
}


/**
* Get All quote items with insurance data for quote without reserved order id
*
* @return Collection
*/
private function getQuoteItemsWithInsuranceData(): Collection
{
$quoteItemCollection = $this->quoteItemCollectionFactory->create();
$quoteItemCollection->getSelect()->joinLeft(
['quote' => $quoteItemCollection->getTable('quote')],
'main_table.quote_id = quote.entity_id',
[]
);
$quoteItemCollection->addFieldToFilter('quote.reserved_order_id', ['null' => true]);
$quoteItemCollection->addFieldToFilter('main_table.alma_insurance', ['notnull' => true]);
return $quoteItemCollection;
}

}
Loading

0 comments on commit d6fd6c5

Please sign in to comment.