-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c1c4c09
commit d43cdbf
Showing
23 changed files
with
678 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.