Skip to content

Commit

Permalink
init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
milindsingh committed Mar 6, 2021
1 parent c1c4c09 commit d43cdbf
Show file tree
Hide file tree
Showing 23 changed files with 678 additions and 3 deletions.
8 changes: 8 additions & 0 deletions Api/Data/ProductInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Adapttive\Catalog\Api\Data;

interface ProductInterface
{
const RELEASE_DATE_TIME = "release_date_time";
}
63 changes: 63 additions & 0 deletions Helper/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
*
* Adapttive/Catalog: Module for Magento 2 to manage catalog release by a pre-specified date.
*
* @category PHP
* @package Adapttive
* @subpackage Ui
* @author Milind Singh <[email protected]>
* @copyright 2020 Milind Singh
* @license MIT
* @version 1.0.0
*/

namespace Adapttive\Catalog\Helper;


use Exception;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;

class Config
{
const CONFIG_PATH_IS_ENABLED = "adapttive/catalog/enabled";

/**
* @var ScopeConfigInterface
*/
private $config;

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

public function __construct(
ScopeConfigInterface $config,
StoreManagerInterface $storeManager
) {
$this->config = $config;
$this->storeManager = $storeManager;
}

/**
* Is module enabled
* @return bool
*/
public function isEnabled()
{
try {
$websiteId = $this->storeManager->getWebsite()->getId();
$flag = (bool)$this->config->getValue(
self::CONFIG_PATH_IS_ENABLED,
ScopeInterface::SCOPE_WEBSITES,
$websiteId
);
} catch (Exception $e) {
$flag = false;
}
return $flag;
}
}
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021 adapttive
Copyright (c) 2021 Milind Singh <hello@adapttive.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
47 changes: 47 additions & 0 deletions Model/ReleaseValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Adapttive\Catalog\Model;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Stdlib\DateTime;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;

class ReleaseValidator
{
/**
* @var TimezoneInterface
*/
private $timezone;

public function __construct(
TimezoneInterface $timezone
) {
$this->timezone = $timezone;
}

/**
* Validate if product is released
* @param $product
* @return bool
* @throws LocalizedException
*/
public function validate($product)
{
if ($product && $product->getReleaseDateTime()) {
// if product not released throw exception.
$releaseDateTime = $this->timezone->date((string)$product->getReleaseDateTime());
$current = $this->timezone->date();
if ($releaseDateTime && $releaseDateTime > $current) {
throw new LocalizedException(
__(
"The product is not available for purchase. Please retry after %1.",
$releaseDateTime->format(DateTime::DATETIME_PHP_FORMAT
)
)
);
}
}

return true;
}
}
57 changes: 57 additions & 0 deletions Observer/ReleaseObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Adapttive\Catalog\Observer;

use Adapttive\Catalog\Helper\Config;
use Adapttive\Catalog\Model\ReleaseValidator;
use Magento\Framework\Event\Observer;
use Magento\Framework\Exception\LocalizedException;
use Magento\Quote\Model\Quote\Item;
use Magento\Framework\Event\ObserverInterface;

/**
* Class ReleaseObserver
* Trigger the release date validation..
* Reference: catalog inventory validation
*/
class ReleaseObserver implements ObserverInterface
{
/**
* @var ReleaseValidator
*/
private $validator;

/**
* @var Config
*/
private $config;

public function __construct(
Config $config,
ReleaseValidator $validator
) {
$this->validator = $validator;
$this->config = $config;
}

/**
* @param Observer $observer
* @return void
* @throws LocalizedException
*/
public function execute(Observer $observer)
{
/* @var $quoteItem Item */
$quoteItem = $observer->getEvent()->getItem();
if (!$quoteItem ||
!$quoteItem->getProductId() ||
!$quoteItem->getQuote() ||
!$this->config->isEnabled()
) {
return;
}
$product = $quoteItem->getProduct();

$this->validator->validate($product);
}
}
113 changes: 113 additions & 0 deletions Plugin/Checkout/ValidateAddToCart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

namespace Adapttive\Catalog\Plugin\Checkout;

use Adapttive\Catalog\Helper\Config;
use Adapttive\Catalog\Model\ReleaseValidator;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product;
use Magento\Checkout\Model\Cart;
use Magento\Framework\DataObject;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Model\StoreManagerInterface;

/**
* Class ValidateAddToCart
* This validation plugin is added to fix the redirection while add to cart
* from product listing page
*/
class ValidateAddToCart
{
/**
* @var ReleaseValidator
*/
private $validator;

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

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

/**
* @var Config
*/
private $config;

public function __construct(
Config $config,
StoreManagerInterface $storeManager,
ProductRepositoryInterface $productRepository,
ReleaseValidator $validator
) {
$this->config = $config;
$this->storeManager = $storeManager;
$this->productRepository = $productRepository;
$this->validator = $validator;
}

/**
* @param Cart $subject
* @param int|Product $productInfo
* @param DataObject|int|array $requestInfo
* @return array
* @throws LocalizedException
*/
public function beforeAddProduct(
Cart $subject,
$productInfo,
$requestInfo = null
) {
if ($this->config->isEnabled()) {
$product = $this->getProduct($productInfo);
$this->validator->validate($product);
}
return [$productInfo, $requestInfo];
}

/**
* Get product object based on requested product information
* copied from: \Magento\Checkout\Model\Cart::_getProduct
* @param Product|int|string $productInfo
* @return Product
* @throws LocalizedException
*/
protected function getProduct($productInfo)
{
$product = null;
if ($productInfo instanceof Product) {
$product = $productInfo;
if (!$product->getId()) {
throw new LocalizedException(
__("The product wasn't found. Verify the product and try again.")
);
}
} elseif (is_int($productInfo) || is_string($productInfo)) {
$storeId = $this->storeManager->getStore()->getId();
try {
$product = $this->productRepository->getById($productInfo, false, $storeId);
} catch (NoSuchEntityException $e) {
throw new LocalizedException(
__("The product wasn't found. Verify the product and try again."),
$e
);
}
} else {
throw new LocalizedException(
__("The product wasn't found. Verify the product and try again.")
);
}
$currentWebsiteId = $this->storeManager->getStore()->getWebsiteId();
if (!is_array($product->getWebsiteIds()) || !in_array($currentWebsiteId, $product->getWebsiteIds())) {
throw new LocalizedException(
__("The product wasn't found. Verify the product and try again.")
);
}
return $product;
}
}
73 changes: 73 additions & 0 deletions Plugin/Quote/Item/Repository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
namespace Adapttive\Catalog\Plugin\Quote\Item;

use Adapttive\Catalog\Model\ReleaseValidator;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Quote\Api\CartItemRepositoryInterface;
use Magento\Quote\Api\Data\CartItemInterface;

/**
* Class Repository
* Add validation is api request for add/update item in cart
*/
class Repository
{
/**
* @var ProductRepositoryInterface
*/
private $productRepository;

/**
* @var ReleaseValidator
*/
private $validator;

/**
* Item Repository Plugin constructor.
* @param ReleaseValidator $validator
* @param ProductRepositoryInterface $productRepository
*/
public function __construct(
ReleaseValidator $validator,
ProductRepositoryInterface $productRepository
) {
$this->validator = $validator;
$this->productRepository = $productRepository;
}

/**
* Validate release date time on product add to cart
* @param CartItemRepositoryInterface $subject
* @param $cartItem
* @return array
* @throws LocalizedException
*/
public function beforeSave(CartItemRepositoryInterface $subject, $cartItem)
{
$product = $this->getProduct($cartItem);
$this->validator->validate($product);
return [$cartItem];
}

/**
* Get Product for Cart Item
* @param CartItemInterface $item
* @return false|ProductInterface
*/
private function getProduct(CartItemInterface $item)
{
$product = false;
try {
if ($item->getProductId()) {
$product = $item->getProduct();
} elseif ($item->getSku()) {
$product = $this->productRepository->get($item->getSku(), false, $item->getStoreId());
}
} catch (\Exception $e) {
$product = false;
}
return $product;
}
}
Loading

0 comments on commit d43cdbf

Please sign in to comment.