Skip to content

Commit

Permalink
Merge pull request #5 from mageplaza/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
haitv282 authored Mar 9, 2020
2 parents 15e4812 + ea7209a commit 5587cac
Show file tree
Hide file tree
Showing 39 changed files with 1,848 additions and 1,205 deletions.
48 changes: 48 additions & 0 deletions Api/ShareCartRepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* Mageplaza
*
* NOTICE OF LICENSE
*
* This source file is subject to the mageplaza.com license that is
* available through the world-wide-web at this URL:
* https://www.mageplaza.com/LICENSE.txt
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category Mageplaza
* @package Mageplaza_ShareCart
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
* @license https://www.mageplaza.com/LICENSE.txt
*/

namespace Mageplaza\ShareCart\Api;

/**
* Interface ShareCartRepositoryInterface
* @api
*/
interface ShareCartRepositoryInterface
{
/**
* Required($mpShareCartToken)
*
* @param string|null $mpShareCartToken
*
* @return \Magento\Quote\Api\Data\CartInterface
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function share($mpShareCartToken);

/**
* @param string $token
* @return string
* @throws \Magento\Framework\Exception\FileSystemException
* @throws \Mpdf\MpdfException
* @throws \Exception
*/
public function downloadPdf($token);
}
113 changes: 77 additions & 36 deletions Block/Cart/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@
use Magento\Checkout\Model\Session;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
use Magento\Directory\Model\Currency;
use Magento\Eav\Model\Entity\Collection\AbstractCollection;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\Framework\UrlInterface;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Magento\Quote\Model\Quote;
use Magento\Quote\Model\Quote\Item;
use Mageplaza\ShareCart\Helper\Data;

/**
* Class Button
Expand Down Expand Up @@ -57,11 +61,6 @@ class Button extends Template
*/
protected $configurable;

/**
* @var UrlInterface
*/
protected $_urlBuilder;

/**
* @var PriceCurrencyInterface
*/
Expand All @@ -70,17 +69,23 @@ class Button extends Template
/**
* @var Quote
*/
protected $_quote;
protected $quote;

/**
* @var Data
*/
protected $helper;

/**
* Button constructor.
*
* @param Context $context
* @param Session $checkoutSession
* @param Currency $currency
* @param ProductRepository $productRepository
* @param Configurable $configurable
* @param UrlInterface $urlBuilder
* @param PriceCurrencyInterface $priceCurrency
* @param Data $helper
* @param array $data
*/
public function __construct(
Expand All @@ -89,17 +94,16 @@ public function __construct(
Currency $currency,
ProductRepository $productRepository,
Configurable $configurable,
UrlInterface $urlBuilder,
PriceCurrencyInterface $priceCurrency,
Data $helper,
array $data = []
)
{
) {
$this->_currency = $currency;
$this->checkoutSession = $checkoutSession;
$this->_productRepository = $productRepository;
$this->configurable = $configurable;
$this->_urlBuilder = $urlBuilder;
$this->priceCurrency = $priceCurrency;
$this->helper = $helper;

parent::__construct($context, $data);
}
Expand All @@ -115,15 +119,26 @@ public function getCurrentCurrencySymbol()
}

/**
* @return \Magento\Eav\Model\Entity\Collection\AbstractCollection
* @param Quote|null $quote
*
* @return AbstractCollection
*/
public function getItems()
public function getItems($quote = null)
{
return $this->checkoutSession->getQuote()->getItemsCollection();
try {
$quote = $quote ?: $this->checkoutSession->getQuote();
} catch (NoSuchEntityException $e) {
return null;
} catch (LocalizedException $e) {
return null;
}

return $quote->getItemsCollection();
}

/**
* @param $item
* @param Item $item
*
* @return array
*/
public function checkConfigurableProduct($item)
Expand All @@ -132,17 +147,26 @@ public function checkConfigurableProduct($item)
}

/**
* @param $item
* @param Item $item
*
* @return null|string
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getNameConfigurable($item)
{
return $this->_productRepository->get($item->getSku())->getName();
try {
if ($product = $this->_productRepository->get($item->getSku())) {
return $product->getName();
}
} catch (NoSuchEntityException $e) {
return null;
}

return null;
}

/**
* @param $price
* @param float $price
*
* @return float
*/
public function formatPrice($price)
Expand All @@ -151,34 +175,43 @@ public function formatPrice($price)
}

/**
* @return float
* @param null $quote
*
* @return float|null
*/
public function getBaseSubtotal()
public function getBaseSubtotal($quote = null)
{
return $this->formatPrice($this->checkoutSession->getQuote()->getBaseSubtotal());
$quote = $this->getQuote($quote);

return $quote ? $this->formatPrice($quote->getBaseSubtotal()) : null;
}

/**
* @codeCoverageIgnore
* @return int
* @param Quote|null $quote
*
* @return Quote|null
*/
public function getItemsCount()
public function getQuote($quote = null)
{
return $this->getQuote()->getItemsCount();
try {
$quote = $quote ?: $this->checkoutSession->getQuote();
} catch (NoSuchEntityException $e) {
return null;
} catch (LocalizedException $e) {
return null;
}

return $quote;
}

/**
* Get active quote
*
* @return Quote
* @return int|mixed|null
*/
public function getQuote()
public function getItemsCount()
{
if (null === $this->_quote) {
$this->_quote = $this->checkoutSession->getQuote();
}
$quote = $this->getQuote();

return $this->_quote;
return $quote ? $quote->getItemsCount() : null;
}

/**
Expand All @@ -188,4 +221,12 @@ public function getLinkDownload()
{
return $this->_urlBuilder->getUrl('sharecart/index/download');
}
}

/**
* @return bool
*/
public function isEnable()
{
return $this->helper->isEnabled();
}
}
62 changes: 50 additions & 12 deletions Block/Cart/Items.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@
use Magento\Checkout\Model\Session;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
use Magento\Directory\Model\Currency;
use Magento\Eav\Model\Entity\Collection\AbstractCollection;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Magento\Quote\Model\Quote;
use Magento\Quote\Model\Quote\Item;

/**
* Class Items
Expand Down Expand Up @@ -56,6 +61,7 @@ class Items extends Template

/**
* Items constructor.
*
* @param Context $context
* @param Session $checkoutSession
* @param Currency $currency
Expand All @@ -70,8 +76,7 @@ public function __construct(
ProductRepository $productRepository,
Configurable $configurable,
array $data = []
)
{
) {
$this->_currency = $currency;
$this->checkoutSession = $checkoutSession;
$this->_productRepository = $productRepository;
Expand All @@ -91,15 +96,38 @@ public function getCurrentCurrencySymbol()
}

/**
* @return \Magento\Eav\Model\Entity\Collection\AbstractCollection
* @param Quote|null $quote
*
* @return AbstractCollection|null
*/
public function getItems($quote = null)
{
$quote = $this->getQuote($quote);

return $quote ? $quote->getItemsCollection() : null;
}

/**
* @param Quote|null $quote
*
* @return Quote|null
*/
public function getItems()
public function getQuote($quote = null)
{
return $this->checkoutSession->getQuote()->getItemsCollection();
try {
$quote = $quote ?: $this->checkoutSession->getQuote();
} catch (NoSuchEntityException $e) {
return null;
} catch (LocalizedException $e) {
return null;
}

return $quote;
}

/**
* @param $item
* @param Item $item
*
* @return array
*/
public function checkConfigurableProduct($item)
Expand All @@ -108,20 +136,30 @@ public function checkConfigurableProduct($item)
}

/**
* @param $item
* @param Item $item
*
* @return null|string
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getNameConfigurable($item)
{
return $this->_productRepository->get($item->getSku())->getName();
try {
$product = $this->_productRepository->get($item->getSku());
} catch (NoSuchEntityException $e) {
return null;
}

return $product;
}

/**
* @param Quote|null $quote
*
* @return float
*/
public function getBaseSubtotal()
public function getBaseSubtotal($quote = null)
{
return $this->checkoutSession->getQuote()->getBaseSubtotal();
$quote = $this->getQuote($quote);

return $quote ? $quote->getBaseSubtotal() : null;
}
}
}
Loading

0 comments on commit 5587cac

Please sign in to comment.