init
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Cart\Block\Cart;
|
||||
|
||||
use Magento\Checkout\Block\Cart\AbstractCart;
|
||||
|
||||
class Coupon extends AbstractCart
|
||||
{
|
||||
public function getCouponCode(): string
|
||||
{
|
||||
return (string)$this->getQuote()->getCouponCode();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Cart\Block\Cart;
|
||||
|
||||
use Esun\Catalog\Block\Product\RecommendedProducts as ProductRecommendedProducts;
|
||||
use Esun\Catalog\Model\ProductRecommendation\Resolver;
|
||||
use Magento\Catalog\Block\Product\Context;
|
||||
use Magento\Catalog\Model\Product;
|
||||
use Magento\Catalog\Model\Product\Attribute\Source\Status;
|
||||
use Magento\Catalog\Model\Product\Visibility as ProductVisibility;
|
||||
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
|
||||
use Magento\Framework\App\ActionInterface;
|
||||
use Magento\Framework\App\Config\ScopeConfigInterface;
|
||||
use Magento\Framework\Pricing\PriceCurrencyInterface;
|
||||
use Magento\Framework\Url\Helper\Data as UrlHelper;
|
||||
use Magento\Store\Model\ScopeInterface;
|
||||
|
||||
/**
|
||||
* Supplies the website-scoped product list rendered below the shopping cart.
|
||||
*/
|
||||
class RecommendedProducts extends ProductRecommendedProducts
|
||||
{
|
||||
private const CONFIG_PATH = 'esun_cart/recommendations/product_skus';
|
||||
|
||||
/** @var ProductVisibility */
|
||||
private readonly ProductVisibility $cartProductVisibility;
|
||||
|
||||
/** @var CollectionFactory */
|
||||
private readonly CollectionFactory $cartProductCollectionFactory;
|
||||
|
||||
/**
|
||||
* @param Context $context
|
||||
* @param ProductVisibility $catalogProductVisibility
|
||||
* @param PriceCurrencyInterface $priceCurrency
|
||||
* @param Resolver $recommendationResolver
|
||||
* @param CollectionFactory $productCollectionFactory
|
||||
* @param ScopeConfigInterface $scopeConfig
|
||||
* @param UrlHelper $urlHelper
|
||||
* @param array $data
|
||||
*/
|
||||
public function __construct(
|
||||
Context $context,
|
||||
ProductVisibility $catalogProductVisibility,
|
||||
PriceCurrencyInterface $priceCurrency,
|
||||
Resolver $recommendationResolver,
|
||||
CollectionFactory $productCollectionFactory,
|
||||
private readonly ScopeConfigInterface $scopeConfig,
|
||||
private readonly UrlHelper $urlHelper,
|
||||
array $data = []
|
||||
) {
|
||||
$this->cartProductVisibility = $catalogProductVisibility;
|
||||
$this->cartProductCollectionFactory = $productCollectionFactory;
|
||||
parent::__construct(
|
||||
$context,
|
||||
$catalogProductVisibility,
|
||||
$priceCurrency,
|
||||
$recommendationResolver,
|
||||
$productCollectionFactory,
|
||||
$data
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return configured products in the same order as their SKUs.
|
||||
*
|
||||
* @return Product[]
|
||||
*/
|
||||
public function getRecommendedItems(): array
|
||||
{
|
||||
$store = $this->_storeManager->getStore();
|
||||
$value = (string)$this->scopeConfig->getValue(
|
||||
self::CONFIG_PATH,
|
||||
ScopeInterface::SCOPE_STORE,
|
||||
(int)$store->getId()
|
||||
);
|
||||
$skus = array_values(array_unique(array_filter(
|
||||
array_map('trim', preg_split('/[\r\n,]+/', $value) ?: []),
|
||||
static fn(string $sku): bool => $sku !== ''
|
||||
)));
|
||||
if ($skus === []) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$collection = $this->cartProductCollectionFactory->create();
|
||||
$collection->setStoreId((int)$store->getId());
|
||||
$collection->addAttributeToSelect(['name', 'small_image', 'thumbnail', 'price', 'special_price']);
|
||||
$collection->addAttributeToFilter('sku', ['in' => $skus]);
|
||||
$collection->addWebsiteFilter((int)$store->getWebsiteId());
|
||||
$collection->addAttributeToFilter('status', Status::STATUS_ENABLED);
|
||||
$collection->addAttributeToFilter(
|
||||
'visibility',
|
||||
['in' => $this->cartProductVisibility->getVisibleInCatalogIds()]
|
||||
);
|
||||
|
||||
$itemsBySku = [];
|
||||
foreach ($collection as $item) {
|
||||
$itemsBySku[(string)$item->getSku()] = $item;
|
||||
}
|
||||
|
||||
$items = [];
|
||||
foreach ($skus as $sku) {
|
||||
if (isset($itemsBySku[$sku])) {
|
||||
$items[] = $itemsBySku[$sku];
|
||||
}
|
||||
}
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether Magento can add the product without selecting required options.
|
||||
*
|
||||
* @param Product $product
|
||||
* @return bool
|
||||
*/
|
||||
public function canAddDirectly(Product $product): bool
|
||||
{
|
||||
return $product->isSaleable() && $product->getTypeInstance()->isPossibleBuyFromList($product);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the standard Magento cart POST fields.
|
||||
*
|
||||
* @param Product $product
|
||||
* @return array
|
||||
*/
|
||||
public function getAddToCartPostParams(Product $product): array
|
||||
{
|
||||
$url = $this->getAddToCartUrl($product, ['_escape' => false]);
|
||||
|
||||
return [
|
||||
'action' => $url,
|
||||
'data' => [
|
||||
'product' => (int)$product->getId(),
|
||||
ActionInterface::PARAM_NAME_URL_ENCODED => $this->urlHelper->getEncodedUrl($url),
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Cart\Block;
|
||||
|
||||
use Esun\Cart\Model\TestModeConfig;
|
||||
use Magento\Framework\Serialize\Serializer\Json;
|
||||
use Magento\Framework\View\Element\Template;
|
||||
|
||||
class TestMode extends Template
|
||||
{
|
||||
public function __construct(
|
||||
Template\Context $context,
|
||||
private readonly TestModeConfig $testModeConfig,
|
||||
private readonly Json $json,
|
||||
array $data = []
|
||||
) {
|
||||
parent::__construct($context, $data);
|
||||
}
|
||||
|
||||
public function isRestricted(): bool
|
||||
{
|
||||
return $this->testModeConfig->isRestricted();
|
||||
}
|
||||
|
||||
public function getMageInitJson(): string
|
||||
{
|
||||
return $this->json->serialize([
|
||||
'Esun_Cart/js/test-mode' => [
|
||||
'title' => $this->testModeConfig->getPopupTitle(),
|
||||
'message' => $this->testModeConfig->getPopupMessage(),
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user