Repository: boldcommerce/magento2-ordercomments
Branch: master
Commit: 2f0feb02b422
Files: 68
Total size: 78.1 KB
Directory structure:
gitextract_gvlihgyr/
├── Api/
│ ├── Data/
│ │ └── OrderCommentInterface.php
│ ├── GuestOrderCommentManagementInterface.php
│ └── OrderCommentManagementInterface.php
├── Block/
│ └── Order/
│ └── Comment.php
├── Controller/
│ └── Cart/
│ └── UpdateComment.php
├── LICENSE
├── Model/
│ ├── Config/
│ │ └── Source/
│ │ └── Collapse.php
│ ├── Config.php
│ ├── Data/
│ │ └── OrderComment.php
│ ├── GuestOrderCommentManagement.php
│ ├── OrderCommentConfigProvider.php
│ └── OrderCommentManagement.php
├── Observer/
│ └── AddOrderCommentToOrder.php
├── Plugin/
│ ├── Block/
│ │ └── Adminhtml/
│ │ └── SalesOrderViewInfo.php
│ └── Model/
│ └── Order/
│ └── LoadOrderComment.php
├── README.md
├── Setup/
│ ├── InstallData.php
│ └── Uninstall.php
├── Test/
│ ├── Integration/
│ │ ├── Model/
│ │ │ ├── GuestOrderCommentManagementTest.php
│ │ │ └── OrderCommentManagementTest.php
│ │ └── Observer/
│ │ └── AddOrderCommentToOrderTest.php
│ └── Unit/
│ ├── Model/
│ │ ├── GuestOrderCommentManagementTest.php
│ │ └── OrderCommentManagementTest.php
│ └── Observer/
│ └── AddOrderCommentToOrderTest.php
├── ViewModel/
│ └── Comment.php
├── composer.json
├── etc/
│ ├── adminhtml/
│ │ ├── di.xml
│ │ └── system.xml
│ ├── config.xml
│ ├── di.xml
│ ├── events.xml
│ ├── extension_attributes.xml
│ ├── frontend/
│ │ ├── di.xml
│ │ └── routes.xml
│ ├── module.xml
│ └── webapi.xml
├── i18n/
│ ├── ar_SA.csv
│ ├── bn_BD.csv
│ ├── cs_CZ.csv
│ ├── de_CH.csv
│ ├── de_DE.csv
│ ├── el_GR.csv
│ ├── es_ES.csv
│ ├── fr_FR.csv
│ ├── he_IL.csv
│ ├── hu_HU.csv
│ ├── it_IT.csv
│ ├── ja_JP.csv
│ ├── nl_NL.csv
│ ├── pl_PL.csv
│ ├── sl_SI.csv
│ ├── sv_SE.csv
│ └── th_TH.csv
├── registration.php
└── view/
├── adminhtml/
│ ├── layout/
│ │ └── sales_order_view.xml
│ ├── templates/
│ │ └── order/
│ │ └── view/
│ │ └── comments.phtml
│ └── ui_component/
│ └── sales_order_grid.xml
└── frontend/
├── layout/
│ ├── checkout_cart_index.xml
│ ├── checkout_index_index.xml
│ └── sales_order_view.xml
├── templates/
│ ├── cart/
│ │ └── comment.phtml
│ └── order/
│ └── view/
│ └── comment.phtml
└── web/
├── css/
│ └── source/
│ └── _module.less
├── js/
│ ├── model/
│ │ └── checkout/
│ │ └── order-comment-validator.js
│ └── view/
│ └── checkout/
│ ├── order-comment-block.js
│ └── order-comment-validator.js
└── template/
└── checkout/
├── form-content.html
└── order-comment-block.html
================================================
FILE CONTENTS
================================================
================================================
FILE: Api/Data/OrderCommentInterface.php
================================================
coreRegistry = $registry;
$this->_isScopePrivate = true;
$this->_template = 'order/view/comment.phtml';
parent::__construct($context, $data);
}
public function getOrder() : Order
{
return $this->coreRegistry->registry('current_order');
}
public function getOrderComment(): string
{
return trim((string) $this->getOrder()->getData(OrderComment::COMMENT_FIELD_NAME));
}
public function hasOrderComment() : bool
{
return strlen($this->getOrderComment()) > 0;
}
public function getOrderCommentHtml() : string
{
return nl2br($this->escapeHtml($this->getOrderComment()));
}
}
================================================
FILE: Controller/Cart/UpdateComment.php
================================================
logger = $logger;
$this->orderCommentManagement = $orderCommentManagement;
$this->orderCommentFactory = $orderCommentFactory;
}
/**
* Saves the comment to the quote
*
* @return ResponseInterface|Redirect|ResultInterface
*/
public function execute()
{
try {
$comment = trim($this->getRequest()->getParam('order_comment', ''));
$cartQuote = $this->cart->getQuote();
$commentObj = $this->orderCommentFactory->create();
$commentObj->setComment($comment);
$this->orderCommentManagement->saveOrderComment($cartQuote->getId(), $commentObj);
$this->messageManager->addSuccessMessage(
__(
'Your comment has been saved.'
)
);
} catch (LocalizedException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
} catch (\Exception $e) {
$this->messageManager->addErrorMessage(__('There was an error when updating the quote.'));
$this->logger->critical($e->getMessage(), ['exception' => $e->getTraceAsString()]);
}
return $this->_goBack();
}
}
================================================
FILE: LICENSE
================================================
MIT License
Copyright (c) 2017 Bold Commerce BV
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
================================================
FILE: Model/Config/Source/Collapse.php
================================================
toArray();
$result = [];
foreach ($options as $value => $label) {
$result[] = [
'value' => $value, 'label' => $label
];
}
return $result;
}
/**
* Get options in "key-value" format
*
* @return array
*/
public function toArray()
{
return [
0 => __('Starts with field closed'),
1 => __('Starts with field opened'),
2 => __('Render field without collapse')
];
}
}
================================================
FILE: Model/Config.php
================================================
scopeConfig = $scopeConfig;
}
/**
* @param mixed $website
* @return bool
*/
public function canShowInCheckout($website = null): bool
{
return $this->scopeConfig->isSetFlag(self::XML_PATH_CONFIG_SHOW_IN_CHECKOUT, ScopeInterface::SCOPE_WEBSITE, $website);
}
/**
* @param mixed $website
* @return bool
*/
public function canShowInAccount($website = null): bool
{
return $this->scopeConfig->isSetFlag(self::XML_PATH_CONFIG_SHOW_IN_ACCOUNT, ScopeInterface::SCOPE_WEBSITE, $website);
}
/**
* @param mixed $website
* @return bool
*/
public function canShowInCart($website = null): bool
{
return $this->scopeConfig->isSetFlag(self::XML_PATH_CONFIG_SHOW_IN_CART, ScopeInterface::SCOPE_WEBSITE, $website);
}
/**
* @param mixed $website
* @return mixed
*/
public function getMaximumCharacterLength($website = null)
{
return $this->scopeConfig->getValue(self::XML_PATH_CONFIG_MAX_LENGTH, ScopeInterface::SCOPE_WEBSITE, $website);
}
/**
* @param mixed $website
* @return mixed
*/
public function getInitialCollapseState($website = null)
{
return $this->scopeConfig->getValue(self::XML_PATH_CONFIG_FIELD_COLLAPSE_STATE, ScopeInterface::SCOPE_WEBSITE, $website);
}
}
================================================
FILE: Model/Data/OrderComment.php
================================================
_get(static::COMMENT_FIELD_NAME);
}
/**
* @param string $comment
* @return $this
*/
public function setComment($comment)
{
return $this->setData(static::COMMENT_FIELD_NAME, $comment);
}
}
================================================
FILE: Model/GuestOrderCommentManagement.php
================================================
quoteIdMaskFactory = $quoteIdMaskFactory;
$this->orderCommentManagement = $orderCommentManagement;
}
/**
* {@inheritDoc}
*/
public function saveOrderComment(
$cartId,
\Bold\OrderComment\Api\Data\OrderCommentInterface $orderComment
) {
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
return $this->orderCommentManagement->saveOrderComment($quoteIdMask->getQuoteId(), $orderComment);
}
}
================================================
FILE: Model/OrderCommentConfigProvider.php
================================================
scopeConfig = $scopeConfig;
$this->checkoutSession = $checkoutSession;
}
/**
* Prepare data for use in checkout javascript component
*
* @return array
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getConfig()
{
$comment = '';
if ($this->checkoutSession->getQuoteId()) {
$comment = $this->checkoutSession->getQuote()->getData(OrderComment::COMMENT_FIELD_NAME) ?: '';
}
return [
'show_in_checkout' => $this->scopeConfig->isSetFlag(self::CONFIG_SHOW_IN_CHECKOUT, ScopeInterface::SCOPE_WEBSITE),
'max_length' => (int) $this->scopeConfig->getValue(self::CONFIG_MAX_LENGTH, ScopeInterface::SCOPE_WEBSITE),
'comment_initial_collapse_state' => (int) $this->scopeConfig->getValue(self::CONFIG_FIELD_COLLAPSE_STATE, ScopeInterface::SCOPE_WEBSITE),
'existing_comment' => $comment
];
}
}
================================================
FILE: Model/OrderCommentManagement.php
================================================
quoteRepository = $quoteRepository;
$this->scopeConfig = $scopeConfig;
}
/**
* @param int $cartId
* @param \Bold\OrderComment\Api\Data\OrderCommentInterface $orderComment
* @return null|string
* @throws CouldNotSaveException
* @throws NoSuchEntityException
*/
public function saveOrderComment(
$cartId,
\Bold\OrderComment\Api\Data\OrderCommentInterface $orderComment
) {
$quote = $this->quoteRepository->getActive($cartId);
if (!$quote->getItemsCount()) {
throw new NoSuchEntityException(__('Cart %1 doesn\'t contain products', $cartId));
}
$comment = $orderComment->getComment();
$this->validateComment($comment);
try {
$quote->setData(OrderComment::COMMENT_FIELD_NAME, strip_tags($comment));
$this->quoteRepository->save($quote);
} catch (\Exception $e) {
throw new CouldNotSaveException(__('The order comment could not be saved'));
}
return $comment;
}
/**
* @param string $comment
* @throws ValidatorException
*/
protected function validateComment($comment)
{
$maxLength = $this->scopeConfig->getValue(OrderCommentConfigProvider::CONFIG_MAX_LENGTH);
if ($maxLength && (mb_strlen($comment) > $maxLength)) {
throw new ValidatorException(__('Comment is too long'));
}
}
}
================================================
FILE: Observer/AddOrderCommentToOrder.php
================================================
getEvent()->getOrder();
/** @var $quote \Magento\Quote\Model\Quote $quote */
$quote = $observer->getEvent()->getQuote();
$order->setData(OrderComment::COMMENT_FIELD_NAME, $quote->getData(OrderComment::COMMENT_FIELD_NAME));
}
}
================================================
FILE: Plugin/Block/Adminhtml/SalesOrderViewInfo.php
================================================
getLayout()->getBlock('boldcommerce_order_comments');
if ($commentBlock !== false && $subject->getNameInLayout() == 'order_info') {
$commentBlock->setOrderComment($subject->getOrder()->getData(OrderComment::COMMENT_FIELD_NAME));
$result = $result . $commentBlock->toHtml();
}
return $result;
}
}
================================================
FILE: Plugin/Model/Order/LoadOrderComment.php
================================================
orderFactory = $orderFactory;
$this->orderExtensionFactory = $extensionFactory;
}
public function afterGet(
OrderRepositoryInterface $subject,
OrderInterface $resultOrder
) {
$this->setOrderComment($resultOrder);
return $resultOrder;
}
public function afterGetList(
OrderRepositoryInterface $subject,
\Magento\Sales\Api\Data\OrderSearchResultInterface $orderSearchResult
) {
foreach ($orderSearchResult->getItems() as $order) {
$this->setOrderComment($order);
}
return $orderSearchResult;
}
public function setOrderComment(OrderInterface $order)
{
if ($order instanceof \Magento\Sales\Model\Order) {
$value = $order->getBoldOrderComment();
} else {
$temp = $this->getOrderFactory()->create();
$temp->load($order->getId());
$value = $temp->getBoldOrderComment();
}
$extensionAttributes = $order->getExtensionAttributes();
$orderExtension = $extensionAttributes ? $extensionAttributes : $this->getOrderExtensionFactory()->create();
$orderExtension->setBoldOrderComment($value);
$order->setExtensionAttributes($orderExtension);
}
public function getOrderFactory()
{
return $this->orderFactory;
}
public function getOrderExtensionFactory()
{
return $this->orderExtensionFactory;
}
}
================================================
FILE: README.md
================================================
# Bold Commerce: Magento 2 Order Comments
## Description
This extension allows customers to place a comment during the checkout.
The comment field is displayed in the billing step right above the place order button.
Additionally, there is also the option of showing the comment field on the cart page.
Store owners can then see these comments in the backend on the order grid and on the order view page.
### Checkout view


### Admin panel

## Emails
Add the "order comment" to new order emails by referencing [the code here](https://github.com/boldcommerce/magento2-ordercomments/issues/6#issuecomment-328515806).
## Configuration
There are several [configuration options](https://github.com/boldcommerce/magento2-ordercomments/blob/master/etc/adminhtml/system.xml) for this extension, which can be found at **STORES > Configuration > SALES > Sales > Order Comments**.
## Installation
```
composer require boldcommerce/magento2-ordercomments
php bin/magento module:enable Bold_OrderComment
php bin/magento setup:upgrade
```
## Changelog
1.8.5
=============
* Third party contribution: PHP 8.1 bugfix `Deprecated Functionality: trim(): Passing null to parameter` when viewing
an order in the my orders section that doesn't have an order comment [#72](https://github.com/boldcommerce/magento2-ordercomments/pull/72)
1.8.4
=============
* Third party contribution: PHP 8.1 support [#71](https://github.com/boldcommerce/magento2-ordercomments/pull/71)
* Third party contribution: Spanish translations [#70](https://github.com/boldcommerce/magento2-ordercomments/pull/70)
* Third party contribution: Thai translations [#67](https://github.com/boldcommerce/magento2-ordercomments/pull/67)
1.8.2
=============
* Third party contribution: Bengali translations [#65](https://github.com/boldcommerce/magento2-ordercomments/pull/65)
1.8.1
=============
* fix bug introduced with 1.8.0 in checkout `Cannot read property 'length' of null`
1.8.0
=============
* new feature: ability to show the comment field on the cart page based on a admin configuration setting.
1.7.1
=============
* upgrade tests to phpunit 6
1.7.0
=============
* Added website scope configuration setting to toggle visibility of comment field. [#59](https://github.com/boldcommerce/magento2-ordercomments/pull/59)
1.6.5
=============
* Third party contribution: PHP 7.4 support added in composer [#55](https://github.com/boldcommerce/magento2-ordercomments/pull/55)
* Third party contribution: Japanese translations added [#52](https://github.com/boldcommerce/magento2-ordercomments/pull/52)
* Third party contribution: Fix typo in Italian translation [#51](https://github.com/boldcommerce/magento2-ordercomments/pull/51)
* Third party contribution: Hungarian Translations added [#50](https://github.com/boldcommerce/magento2-ordercomments/pull/50)
* Third party contribution: New sections added in readme [#48](https://github.com/boldcommerce/magento2-ordercomments/pull/48)
1.6.4
=============
* Third party contribution: php 7.3 support in composer [#45](https://github.com/boldcommerce/magento2-ordercomments/pull/45)
* Third party contribution: French translations [#43](https://github.com/boldcommerce/magento2-ordercomments/pull/43)
* Third party contribution: Polish translations [#40](https://github.com/boldcommerce/magento2-ordercomments/pull/40)
* Third party contribution: Czech translations [#39](https://github.com/boldcommerce/magento2-ordercomments/pull/39)
1.6.3
=============
* Third party contribution: move form selector in order-comment-validator.js to a separate method to improve extensibility through mixins [#36](https://github.com/boldcommerce/magento2-ordercomments/pull/36)
1.6.2
=============
* Third party contribution: fix duplicate comment field on admin sales invoice view [#31](https://github.com/boldcommerce/magento2-ordercomments/pull/31)
* Third party contribution: fix typo and added some code improvements to the install script [#30](https://github.com/boldcommerce/magento2-ordercomments/pull/30)
1.6.1
=============
* Third party contribution: Enabled PHP 7.2 support [#29](https://github.com/boldcommerce/magento2-ordercomments/pull/29)
1.6.0
=============
* Third party contribution: Hebrew translations [#28](https://github.com/boldcommerce/magento2-ordercomments/pull/28)
1.5.0
=============
* Third party contribution: Form selector fallback for compatability with external changes that move the comment field [#24](https://github.com/boldcommerce/magento2-ordercomments/pull/24)
1.4.1
=============
* Third party contribution: Fixed it_IT translation csv [#20](https://github.com/boldcommerce/magento2-ordercomments/pull/20)
1.4.0
=============
* Third party contribution: Made the comment available in the order list web api `V1/orders` [#18](https://github.com/boldcommerce/magento2-ordercomments/pull/18)
1.3.0
=============
* UX changes to the max comment length feature [#15](https://github.com/boldcommerce/magento2-ordercomments/issue/15)
* Made the comment available in the order detail web api `V1/orders/{id}` [#15](https://github.com/boldcommerce/magento2-ordercomments/issue/15)
1.2.0
=============
* added setting to change initial collapse state of comment field (closed/opened/no collapse) [#14](https://github.com/boldcommerce/magento2-ordercomments/issue/14)
1.1.4
=============
* updated composer.json to allow PHP 7.1
1.1.3
=============
* Third party contribution: Dutch translations [#10](https://github.com/boldcommerce/magento2-ordercomments/pull/10)
* Third party contribution: Italian translations [#11](https://github.com/boldcommerce/magento2-ordercomments/pull/11)
1.1.2
=============
* Fix for fatal error on admin order view page when used with some other extensions [#9](https://github.com/boldcommerce/magento2-ordercomments/issues/9)
1.1.1
=============
* Third party contribution: Swedish translations and fixes in German translations [#5](https://github.com/boldcommerce/magento2-ordercomments/pull/5)
1.1.0
=============
* Third party contribution: German translations [#2](https://github.com/boldcommerce/magento2-ordercomments/pull/2)
* Third party contribution: Optional configuration for maximum comment length [#3](https://github.com/boldcommerce/magento2-ordercomments/pull/3)
* Third party contribution: Show order comments in customer account [#4](https://github.com/boldcommerce/magento2-ordercomments/pull/4)
1.0.0
=============
initial version
## Technical
To take in account third party payment extensions using custom implementations of Magento_Checkout/js/action/place-order.js to submit the order, this extension sends
the order comment in a separate request during the validation, before the order is placed. It should therefore work out of
the box.
## Uninstall
If you installed this module through composer, then you can run `php bin/magento module:uninstall Bold_OrderComment` to automatically
remove the code and drop the columns added by this extension.
*note:* the uninstall command seems bugged and might get stuck at `Removing code from Magento codebase:` (It worked fine for me on a 2.1.0 install but not on a 2.1.4 install). When this happens you should
exit with `ctrl+c` and run
```
composer update
php bin/magento maintenance:disable
```
See [github issue 3544](https://github.com/magento/magento2/issues/3544)
Alternatively you can manually remove the extension and remove the column `bold_order_comment` from the tables
* quote
* sales_order
* sales_order_grid
## License
MIT
================================================
FILE: Setup/InstallData.php
================================================
salesSetupFactory = $salesSetupFactory;
$this->quoteSetupFactory = $quoteSetupFactory;
}
/**
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
* @return void
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
/** @var \Magento\Quote\Setup\QuoteSetup $quoteInstaller */
$quoteInstaller = $this->quoteSetupFactory->create(['resourceName' => 'quote_setup', 'setup' => $setup]);
/** @var \Magento\Sales\Setup\SalesSetup $salesInstaller */
$salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
$quoteInstaller->addAttribute(
'quote',
OrderComment::COMMENT_FIELD_NAME,
['type' => Table::TYPE_TEXT, 'length' => '64k', 'nullable' => true]
);
$salesInstaller->addAttribute(
'order',
OrderComment::COMMENT_FIELD_NAME,
['type' => Table::TYPE_TEXT, 'length' => '64k', 'nullable' => true, 'grid' => true]
);
$setup->endSetup();
}
}
================================================
FILE: Setup/Uninstall.php
================================================
startSetup();
$setup->getConnection()->dropColumn(
$setup->getTable('quote'),
OrderComment::COMMENT_FIELD_NAME
);
$setup->getConnection()->dropColumn(
$setup->getTable('sales_order'),
OrderComment::COMMENT_FIELD_NAME
);
$setup->getConnection()->dropColumn(
$setup->getTable('sales_order_grid'),
OrderComment::COMMENT_FIELD_NAME
);
$setup->endSetup();
}
}
================================================
FILE: Test/Integration/Model/GuestOrderCommentManagementTest.php
================================================
create('\Magento\Quote\Model\Quote');
$quote->load('test01', 'reserved_order_id');
/** @var \Magento\Quote\Model\QuoteIdMask $quoteMask */
$quoteMask = $objectManager->create('\Magento\Quote\Model\QuoteIdMask');
$quoteMask->load($quote->getId(), 'quote_id');
$model = $objectManager->create('\Bold\OrderComment\Api\GuestOrderCommentManagementInterface');
$data = $objectManager->create('\Bold\OrderComment\Api\Data\OrderCommentInterface');
$data->setComment($comment);
$model->saveOrderComment($quoteMask->getMaskedId(), $data);
$quote->load('test01', 'reserved_order_id');
self::assertEquals($comment, $quote->getData(OrderComment::COMMENT_FIELD_NAME));
}
}
================================================
FILE: Test/Integration/Model/OrderCommentManagementTest.php
================================================
create('\Magento\Quote\Model\Quote');
$quote->load('test01', 'reserved_order_id');
$model = $objectManager->create('\Bold\OrderComment\Api\OrderCommentManagementInterface');
$data = $objectManager->create('\Bold\OrderComment\Api\Data\OrderCommentInterface');
$data->setComment($comment);
$model->saveOrderComment($quote->getId(), $data);
$quote->load('test01', 'reserved_order_id');
self::assertEquals($comment, $quote->getData(OrderComment::COMMENT_FIELD_NAME));
}
}
================================================
FILE: Test/Integration/Observer/AddOrderCommentToOrderTest.php
================================================
create('\Magento\Quote\Model\Quote');
$quote->load('test01', 'reserved_order_id');
$quote->setData(OrderComment::COMMENT_FIELD_NAME, $comment);
$quote->save();
/** @var \Magento\Quote\Api\CartManagementInterface $model */
$model = $objectManager->create('\Magento\Quote\Api\CartManagementInterface');
/** @var \Magento\Sales\Model\Order $order */
$order = $model->submit($quote);
self::assertEquals($comment, $order->getData(OrderComment::COMMENT_FIELD_NAME));
}
}
================================================
FILE: Test/Unit/Model/GuestOrderCommentManagementTest.php
================================================
quoteRepositoryMock = $this->createMock('\Magento\Quote\Api\CartRepositoryInterface');
$this->quoteMock = $this->createMock(
'\Magento\Quote\Model\Quote',
[
'getItemsCount',
'save',
'__wakeup'
],
[],
'',
false
);
$this->orderCommentManagementMock = $this->createMock(
'Bold\OrderComment\Model\OrderCommentManagement',
[],
[],
'',
false
);
$this->maskedCartId = 'f216207248d65c789b17be8545e0aa73';
$this->cartId = 123;
$guestCartTestHelper = new GuestCartTestHelper($this);
list($this->quoteIdMaskFactoryMock, $this->quoteIdMaskMock) = $guestCartTestHelper->mockQuoteIdMask(
$this->maskedCartId,
$this->cartId
);
$this->testObject = $objectManager->getObject(
'Bold\OrderComment\Model\GuestOrderCommentManagement',
[
'orderCommentManagement' => $this->orderCommentManagementMock,
'quoteIdMaskFactory' => $this->quoteIdMaskFactoryMock
]
);
}
public function testSaveComment()
{
$comment = 'test comment';
$orderCommentMock = $this->getMockBuilder('\Bold\OrderComment\Model\Data\OrderComment')
->disableOriginalConstructor()
->getMock();
$this->orderCommentManagementMock->expects($this->once())
->method('saveOrderComment')
->with($this->cartId, $orderCommentMock)
->willReturn($comment);
$result = $this->testObject->saveOrderComment($this->maskedCartId, $orderCommentMock);
$this->assertEquals($comment, $result);
}
}
================================================
FILE: Test/Unit/Model/OrderCommentManagementTest.php
================================================
quoteRepositoryMock = $this->createMock(CartRepositoryInterface::class);
$this->quoteMock = $this->createPartialMock(
Quote::class,
[
'getItemsCount',
'save',
'__wakeup'
]
);
$this->configMock = $this->getMockForAbstractClass(
ScopeConfigInterface::class
);
$this->testObject = new OrderCommentManagement($this->quoteRepositoryMock, $this->configMock);
}
/**
* @expectedException \Magento\Framework\Exception\NoSuchEntityException
* @expectedExceptionMessage Cart 123 doesn't contain products
*/
public function testSaveCommentWithEmptyCart()
{
$this->setupQuoteRepositoryMockQueries(123, 0);
$this->testObject->saveOrderComment(123, $this->mockOrderComment());
}
/**
* @expectedException \Magento\Framework\Exception\CouldNotSaveException
* @expectedExceptionMessage The order comment could not be saved
*/
public function testSaveCommentWhenCouldNotSaveQuote()
{
$cartId = 123;
$cartItemCount = 12;
$this->setupQuoteRepositoryMockQueries($cartId, $cartItemCount);
$exceptionMessage = 'The order comment could not be saved';
$exception = new \Magento\Framework\Exception\CouldNotSaveException(__($exceptionMessage));
$this->quoteRepositoryMock->expects($this->once())
->method('save')
->with($this->quoteMock)
->willThrowException($exception);
$this->testObject->saveOrderComment($cartId, $this->mockOrderComment());
}
/**
* @expectedException \Magento\Framework\Exception\ValidatorException
* @expectedExceptionMessage Comment is too long
*/
public function testSaveCommentThatIsTooLong()
{
$cartId = 123;
$cartItemCount = 12;
$comment = '123456789';
$this->configMock
->method('getValue')
->with(OrderCommentConfigProvider::CONFIG_MAX_LENGTH)
->willReturn(8);
$this->setupQuoteRepositoryMockQueries($cartId, $cartItemCount);
$this->quoteRepositoryMock->expects($this->never())
->method('save');
$this->testObject->saveOrderComment($cartId, $this->mockOrderComment($comment));
}
public function testSaveComment()
{
$cartId = 123;
$comment = 'test comment';
$cartItemCount = 12;
$this->setupQuoteRepositoryMockQueries($cartId, $cartItemCount);
$this->quoteRepositoryMock->expects($this->once())
->method('save')
->with($this->quoteMock)
->will($this->returnSelf());
$this->testObject->saveOrderComment($cartId, $this->mockOrderComment($comment));
$this->assertEquals($comment, $this->quoteMock->getData(OrderComment::COMMENT_FIELD_NAME));
}
public function testSaveCommentWithTags()
{
$cartId = 123;
$cartItemCount = 12;
$comment = 'test comment';
$this->setupQuoteRepositoryMockQueries($cartId, $cartItemCount);
$this->quoteRepositoryMock->expects($this->once())
->method('save')
->with($this->quoteMock)
->will($this->returnSelf());
$this->testObject->saveOrderComment($cartId, $this->mockOrderComment($comment));
$this->assertEquals(strip_tags($comment), $this->quoteMock->getData(OrderComment::COMMENT_FIELD_NAME));
}
private function setupQuoteRepositoryMockQueries(int $cartId, int $cartItemCount)
{
$this->quoteRepositoryMock->expects($this->once())
->method('getActive')->with($cartId)->will($this->returnValue($this->quoteMock));
$this->quoteMock->expects($this->once())->method('getItemsCount')->will($this->returnValue($cartItemCount));
}
/**
* @return \PHPUnit_Framework_MockObject_MockObject|OrderCommentInterface
*/
private function mockOrderComment(string $comment = null): \PHPUnit_Framework_MockObject_MockObject
{
$orderCommentMock = $this->getMockBuilder(OrderComment::class)
->disableOriginalConstructor()
->getMock();
if ($comment !== null) {
$orderCommentMock->expects($this->once())
->method('getComment')
->willReturn($comment);
}
return $orderCommentMock;
}
}
================================================
FILE: Test/Unit/Observer/AddOrderCommentToOrderTest.php
================================================
objectManager = new ObjectManager($this);
$this->observer = new AddOrderCommentToOrder();
}
public function testExecute()
{
$comment = 'test comment';
$observerMock = $this->createMock('Magento\Framework\Event\Observer');
$eventMock = $this->createPartialMock('Magento\Framework\Event', ['getData']);
$quoteMock = $this->createPartialMock('Magento\Quote\Model\Quote', ['getData']);
$orderMock = $this->createPartialMock('Magento\Sales\Model\Order', []);
$map = [
['quote', null, $quoteMock],
['order', null, $orderMock]
];
$observerMock->expects($this->atLeast(2))
->method('getEvent')
->willReturn($eventMock);
$eventMock->expects($this->atLeast(2))
->method('getData')
->will($this->returnValueMap($map));
$quoteMock->expects($this->atLeastOnce())
->method('getData')
->with(OrderComment::COMMENT_FIELD_NAME)
->willReturn($comment);
$this->observer->execute($observerMock);
$this->assertEquals($comment, $orderMock->getData(OrderComment::COMMENT_FIELD_NAME));
}
}
================================================
FILE: ViewModel/Comment.php
================================================
config = $config;
$this->checkoutSession = $checkoutSession;
}
/**
* Get the current comment from quote
*
* @return string|null
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getComment(): ?string
{
if ($this->checkoutSession->getQuoteId()) {
return $this->checkoutSession->getQuote()->getData(OrderComment::COMMENT_FIELD_NAME);
}
return null;
}
/**
* Get Max Length validation classes if character restriction is enabled
*
* @return string
*/
public function getExtraClass(): string
{
$class = '';
if ($maxLength = $this->config->getMaximumCharacterLength()) {
$class .= 'validate-length maximum-length-' . $maxLength;
}
return $class;
}
}
================================================
FILE: composer.json
================================================
{
"name": "boldcommerce/magento2-ordercomments",
"description": "Magento 2 Module to add a comment field above the place order button in the checkout",
"require": {
"php": "^7.0.0|^8.0.0"
},
"type": "magento2-module",
"keywords": ["magento2"],
"license": "MIT",
"authors": [
{
"name": "Bold Commerce BV",
"email": "info@boldcommerce.nl"
}
],
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"Bold\\OrderComment\\": ""
}
}
}
================================================
FILE: etc/adminhtml/di.xml
================================================