From 5483e9dd9031c83e1e3851b1b9dde3c51d1222f0 Mon Sep 17 00:00:00 2001 From: Ngo Doan Luong Date: Wed, 26 Feb 2020 14:45:12 +0700 Subject: [PATCH 1/9] API feature --- Api/Data/ErrorResponseInterface.php | 42 +++++++ Api/ShareCartRepositoryInterface.php | 47 ++++++++ Block/Cart/Button.php | 66 ++++------- Controller/Index/Download.php | 62 ++++------ Controller/Index/Index.php | 68 ++++------- Helper/PrintProcess.php | 92 ++++++++++++--- Model/ShareCartRepository.php | 142 +++++++++++++++++++++++ Observer/QuoteSaveAfter.php | 73 ++++++++++++ Plugin/Api/CartGet.php | 86 ++++++++++++++ Plugin/Api/CartManagementGetCustomer.php | 86 ++++++++++++++ Plugin/Api/GuestCartGet.php | 86 ++++++++++++++ Plugin/CustomerData/Cart.php | 31 ++--- Setup/UpgradeData.php | 70 +++++++++++ etc/di.xml | 34 ++++++ etc/events.xml | 27 +++++ etc/extension_attributes.xml | 27 +++++ etc/module.xml | 2 +- etc/webapi.xml | 36 ++++++ view/base/templates/template.html | 2 +- view/frontend/templates/cart/items.phtml | 44 +++---- 20 files changed, 935 insertions(+), 188 deletions(-) create mode 100644 Api/Data/ErrorResponseInterface.php create mode 100644 Api/ShareCartRepositoryInterface.php create mode 100644 Model/ShareCartRepository.php create mode 100644 Observer/QuoteSaveAfter.php create mode 100644 Plugin/Api/CartGet.php create mode 100644 Plugin/Api/CartManagementGetCustomer.php create mode 100644 Plugin/Api/GuestCartGet.php create mode 100644 Setup/UpgradeData.php create mode 100644 etc/di.xml create mode 100644 etc/events.xml create mode 100644 etc/extension_attributes.xml create mode 100644 etc/webapi.xml diff --git a/Api/Data/ErrorResponseInterface.php b/Api/Data/ErrorResponseInterface.php new file mode 100644 index 0000000..dd6308a --- /dev/null +++ b/Api/Data/ErrorResponseInterface.php @@ -0,0 +1,42 @@ +_currency = $currency; $this->checkoutSession = $checkoutSession; $this->_productRepository = $productRepository; $this->configurable = $configurable; - $this->_urlBuilder = $urlBuilder; $this->priceCurrency = $priceCurrency; parent::__construct($context, $data); @@ -115,15 +107,18 @@ public function getCurrentCurrencySymbol() } /** - * @return \Magento\Eav\Model\Entity\Collection\AbstractCollection + * @param Quote $quote + * + * @return mixed */ - public function getItems() + public function getItems($quote) { - return $this->checkoutSession->getQuote()->getItemsCollection(); + return $quote->getItemsCollection(); } /** * @param $item + * * @return array */ public function checkConfigurableProduct($item) @@ -133,16 +128,22 @@ public function checkConfigurableProduct($item) /** * @param $item + * * @return null|string - * @throws \Magento\Framework\Exception\NoSuchEntityException + * @throws NoSuchEntityException */ public function getNameConfigurable($item) { - return $this->_productRepository->get($item->getSku())->getName(); + if ($product = $this->_productRepository->get($item->getSku())) { + return $product->getName(); + } + + return null; } /** * @param $price + * * @return float */ public function formatPrice($price) @@ -151,34 +152,13 @@ public function formatPrice($price) } /** - * @return float - */ - public function getBaseSubtotal() - { - return $this->formatPrice($this->checkoutSession->getQuote()->getBaseSubtotal()); - } - - /** - * @codeCoverageIgnore - * @return int - */ - public function getItemsCount() - { - return $this->getQuote()->getItemsCount(); - } - - /** - * Get active quote + * @param Quote $quote * - * @return Quote + * @return float */ - public function getQuote() + public function getBaseSubtotal($quote) { - if (null === $this->_quote) { - $this->_quote = $this->checkoutSession->getQuote(); - } - - return $this->_quote; + return $this->formatPrice($quote->getBaseSubtotal()); } /** @@ -188,4 +168,4 @@ public function getLinkDownload() { return $this->_urlBuilder->getUrl('sharecart/index/download'); } -} \ No newline at end of file +} diff --git a/Controller/Index/Download.php b/Controller/Index/Download.php index 3297ff4..69d9f5b 100644 --- a/Controller/Index/Download.php +++ b/Controller/Index/Download.php @@ -25,9 +25,13 @@ use Magento\Checkout\Model\Session; use Magento\Framework\App\Action\Action; use Magento\Framework\App\Action\Context; -use Mageplaza\ShareCart\Helper\PrintProcess; -use Mageplaza\ShareCart\Model\Template\Processor; -use Mpdf\Mpdf; +use Magento\Framework\App\ResponseInterface; +use Magento\Framework\Controller\ResultInterface; +use Magento\Framework\Exception\FileSystemException; +use Magento\Framework\Exception\LocalizedException; +use Magento\Framework\Exception\NoSuchEntityException; +use Mageplaza\ShareCart\Api\ShareCartRepositoryInterface; +use Mpdf\MpdfException; /** * Class Download @@ -36,59 +40,43 @@ class Download extends Action { /** - * @var Processor + * @var ShareCartRepositoryInterface */ - protected $templateProcessor; - + protected $printProcess; /** - * @var \Magento\Checkout\Model\Session + * @var Session */ protected $checkoutSession; - /** - * @var PrintProcess - */ - protected $printProcess; - /** * Download constructor. + * * @param Context $context + * @param ShareCartRepositoryInterface $printProcess * @param Session $checkoutSession - * @param Processor $templateProcessor - * @param PrintProcess $printProcess */ public function __construct( Context $context, - Session $checkoutSession, - Processor $templateProcessor, - PrintProcess $printProcess - ) - { - $this->checkoutSession = $checkoutSession; - $this->templateProcessor = $templateProcessor; - $this->printProcess = $printProcess; + ShareCartRepositoryInterface $printProcess, + Session $checkoutSession + ) { + $this->printProcess = $printProcess; + $this->checkoutSession = $checkoutSession; - return parent::__construct($context); + parent::__construct($context); } /** - * @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface|void - * @throws \Magento\Framework\Exception\FileSystemException - * @throws \Mpdf\MpdfException + * @return ResponseInterface|ResultInterface|void + * @throws FileSystemException + * @throws LocalizedException + * @throws MpdfException + * @throws NoSuchEntityException */ public function execute() { - $html = $this->printProcess->readFile($this->printProcess->getBaseTemplatePath() . 'template.html'); - $mpdf = new Mpdf(['tempDir' => BP . '/var/tmp']); + $mpShareCartToken = $this->checkoutSession->getQuote()->getMpShareCartToken(); - $storeId = $this->checkoutSession->getQuote()->getStoreId(); - $processor = $this->templateProcessor->setVariable( - $this->printProcess->addCustomTemplateVars($storeId) - ); - $processor->setTemplateHtml($html); - $processor->setStore($storeId); - $html = $processor->processTemplate(); - $mpdf->WriteHTML($html); - $mpdf->Output($this->printProcess->getFileName(), 'D'); + $this->printProcess->downloadPdf($mpShareCartToken); } } \ No newline at end of file diff --git a/Controller/Index/Index.php b/Controller/Index/Index.php index d85e325..9001f9b 100644 --- a/Controller/Index/Index.php +++ b/Controller/Index/Index.php @@ -26,10 +26,13 @@ use Magento\Checkout\Model\Cart; use Magento\Framework\App\Action\Action; use Magento\Framework\App\Action\Context; -use Magento\Framework\Exception\NoSuchEntityException; +use Magento\Framework\App\ResponseInterface; +use Magento\Framework\Controller\Result\Redirect; +use Magento\Framework\Controller\ResultInterface; use Magento\Quote\Api\CartRepositoryInterface; use Magento\Store\Model\StoreManagerInterface; use Mageplaza\ShareCart\Helper\Data; +use Mageplaza\ShareCart\Api\ShareCartRepositoryInterface; /** * Class Index @@ -61,15 +64,21 @@ class Index extends Action * @var Data */ protected $helper; + /** + * @var ShareCartRepositoryInterface + */ + private $shareCartRepository; /** * Index constructor. + * * @param Context $context * @param CartRepositoryInterface $cartRepository * @param Cart $cart * @param ProductRepository $productRepository * @param StoreManagerInterface $storeManager * @param Data $helper + * @param ShareCartRepositoryInterface $shareCartRepository */ public function __construct( Context $context, @@ -77,58 +86,29 @@ public function __construct( Cart $cart, ProductRepository $productRepository, StoreManagerInterface $storeManager, - Data $helper - ) - { - $this->cartRepository = $cartRepository; - $this->cart = $cart; - $this->_productRepository = $productRepository; - $this->_storeManager = $storeManager; - $this->helper = $helper; + Data $helper, + ShareCartRepositoryInterface $shareCartRepository + ) { + $this->cartRepository = $cartRepository; + $this->cart = $cart; + $this->_productRepository = $productRepository; + $this->_storeManager = $storeManager; + $this->helper = $helper; + $this->shareCartRepository = $shareCartRepository; - return parent::__construct($context); + parent::__construct($context); } /** - * @return $this|\Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface - * @throws \Magento\Framework\Exception\LocalizedException - * @throws \Magento\Framework\Exception\NoSuchEntityException + * @return ResponseInterface|Redirect|ResultInterface */ public function execute() { - /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */ + /** @var Redirect $resultRedirect */ $resultRedirect = $this->resultRedirectFactory->create(); if ($this->helper->isEnabled()) { - $quoteId = base64_decode($this->getRequest()->getParam('key'), true); - if ($quoteId) { - $quote = $this->cartRepository->get($quoteId); - - $items = $quote->getItemsCollection(); - foreach ($items as $item) { - if (!$item->getParentItemId()) { - $storeId = $this->_storeManager->getStore()->getId(); - try { - /** - * We need to reload product in this place, because products - * with the same id may have different sets of order attributes. - */ - $product = $this->_productRepository->getById($item->getProductId(), false, $storeId, true); - if ($product) { - $options = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct()); - $info = $options['info_buyRequest']; - if ($item->getProductType() == 'configurable' || $item->getProductType() == 'bundle') { - $this->cart->addProduct($product, $info); - } else { - $this->cart->addProduct($item->getProduct(), $item->getQty()); - } - } - } catch (NoSuchEntityException $e) { - return $this; - } - } - } - $this->cart->save(); - } + $mpShareCartToken = $this->getRequest()->getParam('key'); + $this->shareCartRepository->share($mpShareCartToken); } return $resultRedirect->setPath('checkout/cart'); diff --git a/Helper/PrintProcess.php b/Helper/PrintProcess.php index d51e5db..c84d9d2 100644 --- a/Helper/PrintProcess.php +++ b/Helper/PrintProcess.php @@ -21,14 +21,21 @@ namespace Mageplaza\ShareCart\Helper; +use Exception; use Magento\Checkout\Model\Session; use Magento\Framework\App\Filesystem\DirectoryList; use Magento\Framework\App\Helper\Context; +use Magento\Framework\Exception\FileSystemException; use Magento\Framework\Filesystem; use Magento\Framework\ObjectManagerInterface; use Magento\Framework\Stdlib\DateTime\DateTime; use Magento\Framework\Stdlib\DateTime\TimeZone; +use Magento\Quote\Model\QuoteFactory; use Magento\Store\Model\StoreManagerInterface; +use Mageplaza\ShareCart\Model\Template\Processor; +use Mpdf\Mpdf; +use Mpdf\MpdfException; +use Magento\Quote\Model\Quote; /** * Class PrintProcess @@ -47,7 +54,7 @@ class PrintProcess extends Data protected $directoryList; /** - * @var \Magento\Framework\Filesystem + * @var Filesystem */ protected $fileSystem; @@ -71,8 +78,19 @@ class PrintProcess extends Data */ protected $checkoutSession; + /** + * @var Processor + */ + protected $templateProcessor; + + /** + * @var QuoteFactory + */ + protected $quoteFactory; + /** * PrintProcess constructor. + * * @param Context $context * @param Filesystem $fileSystem * @param DirectoryList $directoryList @@ -82,6 +100,8 @@ class PrintProcess extends Data * @param Data $helper * @param DateTime $dateTime * @param TimeZone $timezone + * @param Processor $templateProcessor + * @param QuoteFactory $quoteFactory */ public function __construct( Context $context, @@ -92,23 +112,27 @@ public function __construct( Session $checkoutSession, Data $helper, DateTime $dateTime, - TimeZone $timezone - ) - { - $this->checkoutSession = $checkoutSession; - $this->fileSystem = $fileSystem; - $this->directoryList = $directoryList; - $this->helper = $helper; - $this->dateTime = $dateTime; - $this->timezone = $timezone; + TimeZone $timezone, + Processor $templateProcessor, + QuoteFactory $quoteFactory + ) { + $this->checkoutSession = $checkoutSession; + $this->fileSystem = $fileSystem; + $this->directoryList = $directoryList; + $this->helper = $helper; + $this->dateTime = $dateTime; + $this->timezone = $timezone; + $this->templateProcessor = $templateProcessor; + $this->quoteFactory = $quoteFactory; parent::__construct($context, $objectManager, $storeManager); } /** * @param $relativePath + * * @return string - * @throws \Magento\Framework\Exception\FileSystemException + * @throws FileSystemException */ public function readFile($relativePath) { @@ -131,14 +155,17 @@ public function getFileName() } /** - * @param $storeId + * @param Quote $quote + * * @return mixed - * @throws \Exception + * @throws Exception */ - public function addCustomTemplateVars($storeId) + public function addCustomTemplateVars($quote) { - $templateVars['quote'] = $this->checkoutSession->getQuote(); - $templateVars['store'] = $this->checkoutSession->getQuote()->getStore(); + $storeId = $quote->getStoreId(); + + $templateVars['quote'] = $quote; + $templateVars['store'] = $quote->getStore(); $templateVars['vat_number'] = $this->helper->getVATNumber($storeId); $templateVars['phone'] = $this->helper->getPhone($storeId); $templateVars['contact'] = $this->helper->getEmail($storeId); @@ -153,8 +180,9 @@ public function addCustomTemplateVars($storeId) /** * @param $date + * * @return string - * @throws \Exception + * @throws Exception */ public function formatDate($date) { @@ -170,7 +198,7 @@ public function formatDate($date) $currentDate = (new \DateTime($dateTime)); return $currentDate->format('Y-m-d H:i'); - } catch (\Exception $e) { + } catch (Exception $e) { return $dateTime; } } @@ -198,10 +226,36 @@ public function getBaseTemplatePath() } $basePath = ''; - for ($i = count($rootPathArr); $i < count($currentDirArr) - 1; $i++) { + $rootPathArrCount = count($rootPathArr); + $currentDirArrCount = count($currentDirArr); + for ($i = $rootPathArrCount; $i < $currentDirArrCount - 1; $i++) { $basePath .= $currentDirArr[$i] . '/'; } return $basePath . 'view/base/templates/'; } + + /** + * @param string $mpShareCartToken + * + * @throws FileSystemException + * @throws MpdfException + * @throws Exception + */ + public function downloadPdf($mpShareCartToken) + { + if ($quote = $this->quoteFactory->create()->load($mpShareCartToken, 'mp_share_cart_token')) { + $html = $this->readFile($this->getBaseTemplatePath() . 'template.html'); + $mpdf = new Mpdf(['tempDir' => BP . '/var/tmp']); + + $processor = $this->templateProcessor->setVariable( + $this->addCustomTemplateVars($quote) + ); + $processor->setTemplateHtml($html); + $processor->setStore($quote->getStoreId()); + $html = $processor->processTemplate(); + $mpdf->WriteHTML($html); + $mpdf->Output($this->getFileName(), 'D'); + } + } } diff --git a/Model/ShareCartRepository.php b/Model/ShareCartRepository.php new file mode 100644 index 0000000..95b30ed --- /dev/null +++ b/Model/ShareCartRepository.php @@ -0,0 +1,142 @@ +quoteFactory = $quoteFactory; + $this->storeManager = $storeManager; + $this->productRepository = $productRepository; + $this->cart = $cart; + $this->cartRepository = $cartRepository; + $this->printProcess = $printProcess; + } + + /** + * {@inheritDoc} + */ + public function share($mpShareCartToken) + { + /** @var Quote $quote */ + if ($quote = $this->quoteFactory->create()->load($mpShareCartToken, 'mp_share_cart_token')) { + $items = $quote->getItemsCollection(); + foreach ($items as $item) { + if (!$item->getParentItemId()) { + $storeId = $quote->getStoreId(); + try { + /** + * We need to reload product in this place, because products + * with the same id may have different sets of order attributes. + */ + $product = $this->productRepository->getById($item->getProductId(), false, $storeId, true); + if ($product) { + $options = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct()); + $info = $options['info_buyRequest']; + $productType = $item->getProductType(); + if ($productType === 'configurable' || $productType === 'bundle') { + $this->cart->addProduct($product, $info); + } else { + $this->cart->addProduct($item->getProduct(), $item->getQty()); + } + } + } catch (NoSuchEntityException $e) { + return $this; + } + } + } + $this->cart->save(); + + return $this->cartRepository->get($this->cart->getQuote()->getId()); + } + + return false; + } + + /** + * {@inheritDoc} + */ + public function downloadPdf($mpShareCartToken) + { + $this->printProcess->downloadPdf($mpShareCartToken); + } +} diff --git a/Observer/QuoteSaveAfter.php b/Observer/QuoteSaveAfter.php new file mode 100644 index 0000000..b11eeeb --- /dev/null +++ b/Observer/QuoteSaveAfter.php @@ -0,0 +1,73 @@ +randomDataGenerator = $randomDataGenerator; + $this->cartRepository = $cartRepository; + } + + /** + * @param Observer $observer + * + * @throws LocalizedException + */ + public function execute(Observer $observer) + { + $quote = $observer->getEvent()->getQuote(); + + if (!$quote->getMpShareCartToken()) { + $quote->setMpShareCartToken($this->randomDataGenerator->getUniqueHash()); + + $this->cartRepository->save($quote); + } + } +} diff --git a/Plugin/Api/CartGet.php b/Plugin/Api/CartGet.php new file mode 100644 index 0000000..c7cbf28 --- /dev/null +++ b/Plugin/Api/CartGet.php @@ -0,0 +1,86 @@ +cartExtensionFactory = $cartExtensionFactory; + $this->quoteFactory = $quoteFactory; + } + + /** + * @param CartRepositoryInterface $cartRepository + * @param CartInterface $cart + * + * @return CartInterface + */ + public function afterGet(CartRepositoryInterface $cartRepository, CartInterface $cart) + { + $cartExtension = $cart->getExtensionAttributes(); + if ($cartExtension && $cartExtension->getMpShareCartToken()) { + return $cart; + } + + try { + $mpShareCartToken = $this->quoteFactory->create()->load($cart->getId())->getMpShareCartToken(); + } catch (Exception $e) { + return $cart; + } + + /** @var OrderExtension $orderExtension */ + $cartExtension = $cartExtension ?: $this->cartExtensionFactory->create(); + + $cartExtension->setMpShareCartToken($mpShareCartToken); + $cart->setExtensionAttributes($cartExtension); + + return $cart; + } +} diff --git a/Plugin/Api/CartManagementGetCustomer.php b/Plugin/Api/CartManagementGetCustomer.php new file mode 100644 index 0000000..8c3283c --- /dev/null +++ b/Plugin/Api/CartManagementGetCustomer.php @@ -0,0 +1,86 @@ +cartExtensionFactory = $cartExtensionFactory; + $this->quoteFactory = $quoteFactory; + } + + /** + * @param CartManagementInterface $cartManagement + * @param CartInterface $cart + * + * @return CartInterface + */ + public function afterGetCartForCustomer(CartManagementInterface $cartManagement, CartInterface $cart) + { + $cartExtension = $cart->getExtensionAttributes(); + if ($cartExtension && $cartExtension->getMpShareCartToken()) { + return $cart; + } + + try { + $mpShareCartToken = $this->quoteFactory->create()->load($cart->getId())->getMpShareCartToken(); + } catch (Exception $e) { + return $cart; + } + + /** @var OrderExtension $orderExtension */ + $cartExtension = $cartExtension ?: $this->cartExtensionFactory->create(); + + $cartExtension->setMpShareCartToken($mpShareCartToken); + $cart->setExtensionAttributes($cartExtension); + + return $cart; + } +} diff --git a/Plugin/Api/GuestCartGet.php b/Plugin/Api/GuestCartGet.php new file mode 100644 index 0000000..87826f7 --- /dev/null +++ b/Plugin/Api/GuestCartGet.php @@ -0,0 +1,86 @@ +cartExtensionFactory = $cartExtensionFactory; + $this->quoteFactory = $quoteFactory; + } + + /** + * @param GuestCartRepositoryInterface $cartRepository + * @param CartInterface $cart + * + * @return CartInterface + */ + public function afterGet(GuestCartRepositoryInterface $cartRepository, CartInterface $cart) + { + $cartExtension = $cart->getExtensionAttributes(); + if ($cartExtension && $cartExtension->getMpShareCartToken()) { + return $cart; + } + + try { + $mpShareCartToken = $this->quoteFactory->create()->load($cart->getId())->getMpShareCartToken(); + } catch (Exception $e) { + return $cart; + } + + /** @var OrderExtension $orderExtension */ + $cartExtension = $cartExtension ?: $this->cartExtensionFactory->create(); + + $cartExtension->setMpShareCartToken($mpShareCartToken); + $cart->setExtensionAttributes($cartExtension); + + return $cart; + } +} diff --git a/Plugin/CustomerData/Cart.php b/Plugin/CustomerData/Cart.php index 3fb5e06..e764ee3 100644 --- a/Plugin/CustomerData/Cart.php +++ b/Plugin/CustomerData/Cart.php @@ -22,8 +22,9 @@ namespace Mageplaza\ShareCart\Plugin\CustomerData; use Magento\Checkout\Model\Session; +use Magento\Framework\Exception\LocalizedException; +use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\UrlInterface; -use Mageplaza\Core\Helper\AbstractData; /** * Class Cart @@ -36,16 +37,6 @@ class Cart */ protected $checkoutSession; - /** - * @var \Magento\Quote\Model\Quote|null - */ - protected $quoteId = null; - - /** - * @var \Mageplaza\Core\Helper\AbstractData - */ - protected $helperData; - /** * Url Builder * @@ -55,34 +46,30 @@ class Cart /** * Cart constructor. - * @param AbstractData $helperData + * * @param Session $checkoutSession * @param UrlInterface $urlBuilder */ public function __construct( - AbstractData $helperData, Session $checkoutSession, UrlInterface $urlBuilder - ) - { + ) { $this->checkoutSession = $checkoutSession; - $this->helperData = $helperData; $this->_urlBuilder = $urlBuilder; } /** - * Add Url data to result - * * @param \Magento\Checkout\CustomerData\Cart $subject * @param $result + * * @return mixed + * @throws LocalizedException + * @throws NoSuchEntityException */ public function afterGetSectionData(\Magento\Checkout\CustomerData\Cart $subject, $result) { - if (null === $this->quoteId) { - $this->quoteId = $this->checkoutSession->getQuoteId(); - } - $result['quote_url'] = $this->_urlBuilder->getUrl('sharecart', ['key' => base64_encode($this->quoteId)]); + $result['quote_url'] = $this->_urlBuilder->getUrl('sharecart', + ['key' => $this->checkoutSession->getQuote()->getMpShareCartToken()]); return $result; } diff --git a/Setup/UpgradeData.php b/Setup/UpgradeData.php new file mode 100644 index 0000000..3e50b4e --- /dev/null +++ b/Setup/UpgradeData.php @@ -0,0 +1,70 @@ +quoteSetupFactory = $quoteSetupFactory; + } + + /** + * {@inheritdoc} + */ + public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) + { + $setup->startSetup(); + + if (version_compare($context->getVersion(), '1.0.1') < 0) { + /** @var QuoteSetup $quoteInstaller */ + $quoteInstaller = $this->quoteSetupFactory->create(['resourceName' => 'quote_setup', 'setup' => $setup]); + + $quoteInstaller->addAttribute('quote', 'mp_share_cart_token', ['type' => 'text']); + + } + + $setup->endSetup(); + } +} diff --git a/etc/di.xml b/etc/di.xml new file mode 100644 index 0000000..72f8252 --- /dev/null +++ b/etc/di.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + diff --git a/etc/events.xml b/etc/events.xml new file mode 100644 index 0000000..4a746c7 --- /dev/null +++ b/etc/events.xml @@ -0,0 +1,27 @@ + + + + + + + diff --git a/etc/extension_attributes.xml b/etc/extension_attributes.xml new file mode 100644 index 0000000..34f633f --- /dev/null +++ b/etc/extension_attributes.xml @@ -0,0 +1,27 @@ + + + + + + + diff --git a/etc/module.xml b/etc/module.xml index 8179299..b87b2cd 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -21,7 +21,7 @@ */ --> - + diff --git a/etc/webapi.xml b/etc/webapi.xml new file mode 100644 index 0000000..0f443bb --- /dev/null +++ b/etc/webapi.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + diff --git a/view/base/templates/template.html b/view/base/templates/template.html index 61d5f87..1acb5d3 100644 --- a/view/base/templates/template.html +++ b/view/base/templates/template.html @@ -31,7 +31,7 @@ {{depend message}}

{{var message}}

{{/depend}} -{{layout handle="sharecart_index_download"}} +{{layout handle="sharecart_index_download" quote=$quote }} \ No newline at end of file diff --git a/view/frontend/layout/checkout_cart_index.xml b/view/frontend/layout/checkout_cart_index.xml index f0005ff..10bfe7f 100644 --- a/view/frontend/layout/checkout_cart_index.xml +++ b/view/frontend/layout/checkout_cart_index.xml @@ -1,29 +1,29 @@ - - - - - - - - - + + + + + + + + + diff --git a/view/frontend/layout/default.xml b/view/frontend/layout/default.xml index f77d96d..680da4b 100644 --- a/view/frontend/layout/default.xml +++ b/view/frontend/layout/default.xml @@ -1,51 +1,51 @@ - - - - - - - - - - - - - - - - - Mageplaza_ShareCart/js/view/minicart - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + Mageplaza_ShareCart/js/view/minicart + + + + + + + + + + + + + + diff --git a/view/frontend/layout/sharecart_index_download.xml b/view/frontend/layout/sharecart_index_download.xml index 5cdacdf..b888ba2 100644 --- a/view/frontend/layout/sharecart_index_download.xml +++ b/view/frontend/layout/sharecart_index_download.xml @@ -1,27 +1,27 @@ - - - - - - - + + + + + + + diff --git a/view/frontend/templates/cart/items.phtml b/view/frontend/templates/cart/items.phtml index f674abf..b98bba7 100644 --- a/view/frontend/templates/cart/items.phtml +++ b/view/frontend/templates/cart/items.phtml @@ -59,4 +59,4 @@ $quote = $block->getData('quote'); getBaseSubtotal($quote) ?> - \ No newline at end of file + diff --git a/view/frontend/web/css/style.css b/view/frontend/web/css/style.css index 46997a8..67fb31d 100644 --- a/view/frontend/web/css/style.css +++ b/view/frontend/web/css/style.css @@ -1,102 +1,102 @@ -/** - * 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 - */ -.actions .primary.sharecart { - margin: 0 10px 15px; -} - -button.action.primary.sharecart { - display: block; - width: 100%; - line-height: 2.2rem; - padding: 14px 17px; - font-size: 1.8rem; - margin: unset; -} - -.mp-sharecart-popup .modal-title { - border: none; - padding-bottom: 25px; -} - -.mp-sharecart-popup .modal-inner-wrap { - width: 600px; -} - -.mp-popup-cart { - display: none; -} - -#mp_show_download_share { - display: inline-block; -} - -.mp-tooltipped { - position: relative; -} - -.mp-tooltipped:before { - position: absolute; - z-index: 1000001; - display: none; - width: 0; - height: 0; - color: rgba(0, 0, 0, .8); - pointer-events: none; - content: ""; - border: 5px solid transparent; - top: auto; - right: 50%; - bottom: -5px; - margin-right: -5px; - border-bottom-color: rgba(0, 0, 0, .8) -} - -.mp-tooltipped:after { - position: absolute; - z-index: 1000000; - display: none; - padding: 5px 8px; - font: normal normal 11px/1.5 Helvetica, arial, nimbussansl, liberationsans, freesans, clean, sans-serif, "Segoe UI Emoji", "Segoe UI Symbol"; - color: #fff; - text-align: center; - text-decoration: none; - text-shadow: none; - text-transform: none; - letter-spacing: normal; - word-wrap: break-word; - white-space: pre; - pointer-events: none; - content: attr(aria-label); - background: rgba(0, 0, 0, .8); - border-radius: 3px; - -webkit-font-smoothing: subpixel-antialiased; - top: 100%; - right: 50%; - margin-top: 5px; - -webkit-transform: translateX(50%); - -ms-transform: translateX(50%); - transform: translateX(50%) -} - -.mp-tooltipped:hover:before, .mp-tooltipped:active:before, .mp-tooltipped:focus:before, -.mp-tooltipped:hover:after, .mp-tooltipped:active:after, .mp-tooltipped:focus:after { - display: inline-block; - text-decoration: none +/** + * 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 + */ +.actions .primary.sharecart { + margin: 0 10px 15px; +} + +button.action.primary.sharecart { + display: block; + width: 100%; + line-height: 2.2rem; + padding: 14px 17px; + font-size: 1.8rem; + margin: unset; +} + +.mp-sharecart-popup .modal-title { + border: none; + padding-bottom: 25px; +} + +.mp-sharecart-popup .modal-inner-wrap { + width: 600px; +} + +.mp-popup-cart { + display: none; +} + +#mp_show_download_share { + display: inline-block; +} + +.mp-tooltipped { + position: relative; +} + +.mp-tooltipped:before { + position: absolute; + z-index: 1000001; + display: none; + width: 0; + height: 0; + color: rgba(0, 0, 0, .8); + pointer-events: none; + content: ""; + border: 5px solid transparent; + top: auto; + right: 50%; + bottom: -5px; + margin-right: -5px; + border-bottom-color: rgba(0, 0, 0, .8) +} + +.mp-tooltipped:after { + position: absolute; + z-index: 1000000; + display: none; + padding: 5px 8px; + font: normal normal 11px/1.5 Helvetica, arial, nimbussansl, liberationsans, freesans, clean, sans-serif, "Segoe UI Emoji", "Segoe UI Symbol"; + color: #fff; + text-align: center; + text-decoration: none; + text-shadow: none; + text-transform: none; + letter-spacing: normal; + word-wrap: break-word; + white-space: pre; + pointer-events: none; + content: attr(aria-label); + background: rgba(0, 0, 0, .8); + border-radius: 3px; + -webkit-font-smoothing: subpixel-antialiased; + top: 100%; + right: 50%; + margin-top: 5px; + -webkit-transform: translateX(50%); + -ms-transform: translateX(50%); + transform: translateX(50%) +} + +.mp-tooltipped:hover:before, .mp-tooltipped:active:before, .mp-tooltipped:focus:before, +.mp-tooltipped:hover:after, .mp-tooltipped:active:after, .mp-tooltipped:focus:after { + display: inline-block; + text-decoration: none } \ No newline at end of file diff --git a/view/frontend/web/js/view/minicart.js b/view/frontend/web/js/view/minicart.js index 836c0a2..d373c63 100644 --- a/view/frontend/web/js/view/minicart.js +++ b/view/frontend/web/js/view/minicart.js @@ -1,71 +1,71 @@ -/** - * 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 - */ -define([ - 'jquery', - 'uiComponent', - 'Magento_Customer/js/customer-data', - 'mage/translate' -], function ($, Component, customerData, $t) { - 'use strict'; - - var isReload = true; - - return Component.extend({ - defaults: { - template: 'Mageplaza_ShareCart/minicart' - }, - - initialize: function () { - this._super(); - - if (isReload) { - customerData.reload(['cart'], false); - isReload = false; - } - this.customer = customerData.get('cart'); - }, - - getQuoteId: function () { - return customerData.get('cart')().quote_url; - }, - - copyQuote: function (object, e) { - var quoteUrl = document.createElement('textarea'); - - quoteUrl.value = customerData.get('cart')().quote_url; - document.body.appendChild(quoteUrl); - quoteUrl.select(); - document.execCommand('copy'); - document.body.removeChild(quoteUrl); - - e.currentTarget.setAttribute('class', 'mp-tooltipped'); - e.currentTarget.setAttribute('aria-label', $t('Copied!')); - }, - - leaveQuote: function (object, e) { - e.currentTarget.removeAttribute('class'); - e.currentTarget.removeAttribute('aria-label'); - }, - - isDisplay: function () { - return customerData.get('cart')().summary_count; - } - }); -}); +/** + * 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 + */ +define([ + 'jquery', + 'uiComponent', + 'Magento_Customer/js/customer-data', + 'mage/translate' +], function ($, Component, customerData, $t) { + 'use strict'; + + var isReload = true; + + return Component.extend({ + defaults: { + template: 'Mageplaza_ShareCart/minicart' + }, + + initialize: function () { + this._super(); + + if (isReload) { + customerData.reload(['cart'], false); + isReload = false; + } + this.customer = customerData.get('cart'); + }, + + getQuoteId: function () { + return customerData.get('cart')().quote_url; + }, + + copyQuote: function (object, e) { + var quoteUrl = document.createElement('textarea'); + + quoteUrl.value = customerData.get('cart')().quote_url; + document.body.appendChild(quoteUrl); + quoteUrl.select(); + document.execCommand('copy'); + document.body.removeChild(quoteUrl); + + e.currentTarget.setAttribute('class', 'mp-tooltipped'); + e.currentTarget.setAttribute('aria-label', $t('Copied!')); + }, + + leaveQuote: function (object, e) { + e.currentTarget.removeAttribute('class'); + e.currentTarget.removeAttribute('aria-label'); + }, + + isDisplay: function () { + return customerData.get('cart')().summary_count; + } + }); +}); diff --git a/view/frontend/web/template/minicart.html b/view/frontend/web/template/minicart.html index 446561b..a96a13a 100644 --- a/view/frontend/web/template/minicart.html +++ b/view/frontend/web/template/minicart.html @@ -1,31 +1,31 @@ - - -
-
- - - -
-
- + + +
+
+ + + +
+
+ From ea7209a58f5cc8761d958b8f4bb944687641d734 Mon Sep 17 00:00:00 2001 From: Brian Date: Mon, 9 Mar 2020 18:19:37 +0700 Subject: [PATCH 9/9] Clean code --- README.md | 2 +- travis.yml | 130 ++++++++++++++++++++++++++--------------------------- 2 files changed, 66 insertions(+), 66 deletions(-) diff --git a/README.md b/README.md index 46edb60..efac040 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ - [Contribute on Github](https://github.com/mageplaza/magento-2-share-cart) - [Get Support](https://github.com/mageplaza/magento-2-share-cart/issues) -## 2. FAQs +## 2. FAQs **Q: I got an error: Mageplaza_Core has been already defined** diff --git a/travis.yml b/travis.yml index ecdf6d5..25fbc1a 100644 --- a/travis.yml +++ b/travis.yml @@ -1,65 +1,65 @@ -language: php -php: - - 7.0 - - 7.1 - - 7.2 -sudo: required -dist: trusty -env: - global: - - COMPOSER_BIN_DIR=~/bin - - INTEGRATION_SETS=3 - - NODE_JS_VERSION=6 - - MAGENTO_HOST_NAME="magento2.travis" - - COMPOSER_MODULE=mageplaza/module-share-cart - matrix: - - MAGENTO_VERSION=2.2.1 TEST_SUITE=integration INTEGRATION_INDEX=1 - - MAGENTO_VERSION=2.2.1 TEST_SUITE=integration INTEGRATION_INDEX=2 - - MAGENTO_VERSION=2.2.1 TEST_SUITE=integration INTEGRATION_INDEX=3 - - MAGENTO_VERSION=2.2.2 TEST_SUITE=static - - MAGENTO_VERSION=2.2.2 TEST_SUITE=js GRUNT_COMMAND=static - - MAGENTO_VERSION=2.2.2 TEST_SUITE=integration INTEGRATION_INDEX=1 - - MAGENTO_VERSION=2.2.2 TEST_SUITE=integration INTEGRATION_INDEX=2 - - MAGENTO_VERSION=2.2.2 TEST_SUITE=integration INTEGRATION_INDEX=3 - -matrix: - exclude: - - php: 7.0 - env: MAGENTO_VERSION=2.2.2 TEST_SUITE=js GRUNT_COMMAND=static - - php: 7.0 - env: MAGENTO_VERSION=2.2.2 TEST_SUITE=static -cache: - apt: true - directories: - - "$HOME/.composer/cache" - - "$HOME/.nvm" -addons: - apt: - packages: - - mysql-server-5.6 - - mysql-client-core-5.6 - - mysql-client-5.6 - - postfix - firefox: '46.0' - hosts: - - magento2.travis -before_install: -- git clone https://github.com/magento/magento2 --branch $MAGENTO_VERSION -- cd magento2 -- bash ./dev/travis/before_install.sh -install: -- composer install --no-interaction --prefer-dist -- composer require $COMPOSER_MODULE -before_script: -#- cp -f ${TRAVIS_BUILD_DIR}/dev/tests/integration/phpunit.xml.dist dev/tests/integration/ -- echo "vendor/$COMPOSER_MODULE" > dev/tests/static/testsuite/Magento/Test/Less/_files/whitelist/common.txt -- echo "vendor/$COMPOSER_MODULE" > dev/tests/static/testsuite/Magento/Test/Php/_files/whitelist/common.txt -- echo "vendor/$COMPOSER_MODULE/**/*.js" > dev/tests/static/testsuite/Magento/Test/Js/_files/whitelist/magento.txt -- bash ./dev/travis/before_script.sh -script: -- test $TEST_SUITE = "static" && TEST_FILTER='--filter "Magento\\Test\\Php\\LiveCodeTest"' || true -- test $TEST_SUITE = "functional" && TEST_FILTER='dev/tests/functional/testsuites/Magento/Mtf/TestSuite/InjectableTests.php' || true -- if [ $TEST_SUITE == "functional" ]; then dev/tests/functional/vendor/phpunit/phpunit/phpunit -c dev/tests/$TEST_SUITE $TEST_FILTER; fi -- if [ $TEST_SUITE != "functional" ] && [ $TEST_SUITE != "js" ]; then phpunit -c dev/tests/$TEST_SUITE $TEST_FILTER; fi -- if [ $TEST_SUITE == "js" ]; then grunt $GRUNT_COMMAND; fi - +language: php +php: + - 7.0 + - 7.1 + - 7.2 +sudo: required +dist: trusty +env: + global: + - COMPOSER_BIN_DIR=~/bin + - INTEGRATION_SETS=3 + - NODE_JS_VERSION=6 + - MAGENTO_HOST_NAME="magento2.travis" + - COMPOSER_MODULE=mageplaza/module-share-cart + matrix: + - MAGENTO_VERSION=2.2.1 TEST_SUITE=integration INTEGRATION_INDEX=1 + - MAGENTO_VERSION=2.2.1 TEST_SUITE=integration INTEGRATION_INDEX=2 + - MAGENTO_VERSION=2.2.1 TEST_SUITE=integration INTEGRATION_INDEX=3 + - MAGENTO_VERSION=2.2.2 TEST_SUITE=static + - MAGENTO_VERSION=2.2.2 TEST_SUITE=js GRUNT_COMMAND=static + - MAGENTO_VERSION=2.2.2 TEST_SUITE=integration INTEGRATION_INDEX=1 + - MAGENTO_VERSION=2.2.2 TEST_SUITE=integration INTEGRATION_INDEX=2 + - MAGENTO_VERSION=2.2.2 TEST_SUITE=integration INTEGRATION_INDEX=3 + +matrix: + exclude: + - php: 7.0 + env: MAGENTO_VERSION=2.2.2 TEST_SUITE=js GRUNT_COMMAND=static + - php: 7.0 + env: MAGENTO_VERSION=2.2.2 TEST_SUITE=static +cache: + apt: true + directories: + - "$HOME/.composer/cache" + - "$HOME/.nvm" +addons: + apt: + packages: + - mysql-server-5.6 + - mysql-client-core-5.6 + - mysql-client-5.6 + - postfix + firefox: '46.0' + hosts: + - magento2.travis +before_install: +- git clone https://github.com/magento/magento2 --branch $MAGENTO_VERSION +- cd magento2 +- bash ./dev/travis/before_install.sh +install: +- composer install --no-interaction --prefer-dist +- composer require $COMPOSER_MODULE +before_script: +#- cp -f ${TRAVIS_BUILD_DIR}/dev/tests/integration/phpunit.xml.dist dev/tests/integration/ +- echo "vendor/$COMPOSER_MODULE" > dev/tests/static/testsuite/Magento/Test/Less/_files/whitelist/common.txt +- echo "vendor/$COMPOSER_MODULE" > dev/tests/static/testsuite/Magento/Test/Php/_files/whitelist/common.txt +- echo "vendor/$COMPOSER_MODULE/**/*.js" > dev/tests/static/testsuite/Magento/Test/Js/_files/whitelist/magento.txt +- bash ./dev/travis/before_script.sh +script: +- test $TEST_SUITE = "static" && TEST_FILTER='--filter "Magento\\Test\\Php\\LiveCodeTest"' || true +- test $TEST_SUITE = "functional" && TEST_FILTER='dev/tests/functional/testsuites/Magento/Mtf/TestSuite/InjectableTests.php' || true +- if [ $TEST_SUITE == "functional" ]; then dev/tests/functional/vendor/phpunit/phpunit/phpunit -c dev/tests/$TEST_SUITE $TEST_FILTER; fi +- if [ $TEST_SUITE != "functional" ] && [ $TEST_SUITE != "js" ]; then phpunit -c dev/tests/$TEST_SUITE $TEST_FILTER; fi +- if [ $TEST_SUITE == "js" ]; then grunt $GRUNT_COMMAND; fi +