Skip to content

Commit 8e0fb95

Browse files
committed
updated
0 parents  commit 8e0fb95

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1565
-0
lines changed

Api/Data/OrderCommentInterface.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
namespace Ecomteck\OrderComment\Api\Data;
3+
4+
interface OrderCommentInterface
5+
{
6+
/**
7+
* @return string|null
8+
*/
9+
public function getComment();
10+
11+
/**
12+
* @param string $comment
13+
* @return null
14+
*/
15+
public function setComment($comment);
16+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
namespace Ecomteck\OrderComment\Api;
3+
4+
/**
5+
* Interface for saving the checkout comment to the quote for guest orders
6+
*/
7+
interface GuestOrderCommentManagementInterface
8+
{
9+
/**
10+
* @param string $cartId
11+
* @param \Ecomteck\OrderComment\Api\Data\OrderCommentInterface $orderComment
12+
* @return \Magento\Checkout\Api\Data\PaymentDetailsInterface
13+
*/
14+
public function saveOrderComment(
15+
$cartId,
16+
\Ecomteck\OrderComment\Api\Data\OrderCommentInterface $orderComment
17+
);
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
namespace Ecomteck\OrderComment\Api;
3+
4+
/**
5+
* Interface for saving the checkout comment to the quote for orders of logged in users
6+
* @api
7+
*/
8+
interface OrderCommentManagementInterface
9+
{
10+
/**
11+
* @param int $cartId
12+
* @param \Ecomteck\OrderComment\Api\Data\OrderCommentInterface $orderComment
13+
* @return string
14+
*/
15+
public function saveOrderComment(
16+
$cartId,
17+
\Ecomteck\OrderComment\Api\Data\OrderCommentInterface $orderComment
18+
);
19+
}

Block/Order/Comment.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Ecomteck\OrderComment\Block\Order;
4+
5+
use Ecomteck\OrderComment\Model\Data\OrderComment;
6+
use Magento\Framework\Registry;
7+
use Magento\Framework\View\Element\Template\Context as TemplateContext;
8+
use Magento\Sales\Model\Order;
9+
10+
class Comment extends \Magento\Framework\View\Element\Template
11+
{
12+
/**
13+
* Core registry
14+
*
15+
* @var \Magento\Framework\Registry
16+
*/
17+
protected $coreRegistry = null;
18+
19+
public function __construct(
20+
TemplateContext $context,
21+
Registry $registry,
22+
array $data = []
23+
) {
24+
$this->coreRegistry = $registry;
25+
$this->_isScopePrivate = true;
26+
$this->_template = 'order/view/comment.phtml';
27+
parent::__construct($context, $data);
28+
}
29+
30+
public function getOrder()
31+
{
32+
return $this->coreRegistry->registry('current_order');
33+
}
34+
35+
public function getOrderComment()
36+
{
37+
return trim($this->getOrder()->getData(OrderComment::COMMENT_FIELD_NAME));
38+
}
39+
40+
public function hasOrderComment()
41+
{
42+
return strlen($this->getOrderComment()) > 0;
43+
}
44+
45+
public function getOrderCommentHtml()
46+
{
47+
return nl2br($this->escapeHtml($this->getOrderComment()));
48+
}
49+
}

LICENSE

Whitespace-only changes.

Model/Config/Source/Collapse.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Ecomteck\OrderComment\Model\Config\Source;
4+
5+
class Collapse implements \Magento\Framework\Option\ArrayInterface
6+
{
7+
/**
8+
* Options getter
9+
*
10+
* @return array
11+
*/
12+
public function toOptionArray()
13+
{
14+
$options = $this->toArray();
15+
$result = [];
16+
17+
foreach($options as $value => $label){
18+
$result[] = [
19+
'value' => $value, 'label' => $label
20+
];
21+
}
22+
23+
return $result;
24+
}
25+
26+
/**
27+
* Get options in "key-value" format
28+
*
29+
* @return array
30+
*/
31+
public function toArray()
32+
{
33+
return [
34+
0 => __('Starts with field closed'),
35+
1 => __('Starts with field opened'),
36+
2 => __('Render field without collapse')
37+
];
38+
}
39+
}

Model/Data/OrderComment.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
namespace Ecomteck\OrderComment\Model\Data;
3+
4+
use Ecomteck\OrderComment\Api\Data\OrderCommentInterface;
5+
use Magento\Framework\Api\AbstractSimpleObject;
6+
7+
class OrderComment extends AbstractSimpleObject implements OrderCommentInterface
8+
{
9+
const COMMENT_FIELD_NAME = 'ecomteck_order_comment';
10+
11+
/**
12+
* @return string|null
13+
*/
14+
public function getComment()
15+
{
16+
return $this->_get(static::COMMENT_FIELD_NAME);
17+
}
18+
19+
/**
20+
* @param string $comment
21+
* @return $this
22+
*/
23+
public function setComment($comment)
24+
{
25+
return $this->setData(static::COMMENT_FIELD_NAME, $comment);
26+
}
27+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
namespace Ecomteck\OrderComment\Model;
3+
4+
use Magento\Quote\Model\QuoteIdMaskFactory;
5+
6+
class GuestOrderCommentManagement implements \Ecomteck\OrderComment\Api\GuestOrderCommentManagementInterface
7+
{
8+
9+
/**
10+
* @var QuoteIdMaskFactory
11+
*/
12+
protected $quoteIdMaskFactory;
13+
14+
/**
15+
* @var \Ecomteck\OrderComment\Api\OrderCommentManagementInterface
16+
*/
17+
protected $orderCommentManagement;
18+
19+
/**
20+
* GuestOrderCommentManagement constructor.
21+
* @param QuoteIdMaskFactory $quoteIdMaskFactory
22+
* @param \Ecomteck\OrderComment\Api\OrderCommentManagementInterface $orderCommentManagement
23+
*/
24+
public function __construct(
25+
QuoteIdMaskFactory $quoteIdMaskFactory,
26+
\Ecomteck\OrderComment\Api\OrderCommentManagementInterface $orderCommentManagement
27+
) {
28+
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
29+
$this->orderCommentManagement = $orderCommentManagement;
30+
}
31+
32+
/**
33+
* {@inheritDoc}
34+
*/
35+
public function saveOrderComment(
36+
$cartId,
37+
\Ecomteck\OrderComment\Api\Data\OrderCommentInterface $orderComment
38+
) {
39+
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
40+
return $this->orderCommentManagement->saveOrderComment($quoteIdMask->getQuoteId(), $orderComment);
41+
}
42+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Ecomteck\OrderComment\Model;
4+
5+
use Magento\Checkout\Model\ConfigProviderInterface;
6+
use Magento\Framework\App\Config\ScopeConfigInterface;
7+
8+
class OrderCommentConfigProvider implements ConfigProviderInterface
9+
{
10+
const CONFIG_MAX_LENGTH = 'order_comment/general/max_length';
11+
12+
const CONFIG_FIELD_COLLAPSE_STATE = 'order_comment/general/collapse_state';
13+
14+
/**
15+
* @var ScopeConfigInterface
16+
*/
17+
private $scopeConfig;
18+
19+
public function __construct(ScopeConfigInterface $scopeConfig)
20+
{
21+
$this->scopeConfig = $scopeConfig;
22+
}
23+
24+
public function getConfig()
25+
{
26+
return [
27+
'max_length' => (int) $this->scopeConfig->getValue(self::CONFIG_MAX_LENGTH),
28+
'comment_initial_collapse_state' => (int) $this->scopeConfig->getValue(self::CONFIG_FIELD_COLLAPSE_STATE)
29+
];
30+
}
31+
32+
}

Model/OrderCommentManagement.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
namespace Ecomteck\OrderComment\Model;
3+
4+
use Ecomteck\OrderComment\Model\Data\OrderComment;
5+
use Magento\Framework\App\Config\ScopeConfigInterface;
6+
use Magento\Framework\Exception\NoSuchEntityException;
7+
use Magento\Framework\Exception\CouldNotSaveException;
8+
use Magento\Framework\Exception\ValidatorException;
9+
10+
class OrderCommentManagement implements \Ecomteck\OrderComment\Api\OrderCommentManagementInterface
11+
{
12+
/**
13+
* Quote repository.
14+
*
15+
* @var \Magento\Quote\Api\CartRepositoryInterface
16+
*/
17+
protected $quoteRepository;
18+
19+
/**
20+
* @var ScopeConfigInterface
21+
*/
22+
protected $scopeConfig;
23+
24+
/**
25+
*
26+
* @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository Quote repository.
27+
*/
28+
public function __construct(
29+
\Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
30+
ScopeConfigInterface $scopeConfig
31+
) {
32+
$this->quoteRepository = $quoteRepository;
33+
$this->scopeConfig = $scopeConfig;
34+
}
35+
36+
/**
37+
* @param int $cartId
38+
* @param \Ecomteck\OrderComment\Api\Data\OrderCommentInterface $orderComment
39+
* @return null|string
40+
* @throws CouldNotSaveException
41+
* @throws NoSuchEntityException
42+
*/
43+
public function saveOrderComment(
44+
$cartId,
45+
\Ecomteck\OrderComment\Api\Data\OrderCommentInterface $orderComment
46+
) {
47+
$quote = $this->quoteRepository->getActive($cartId);
48+
if (!$quote->getItemsCount()) {
49+
throw new NoSuchEntityException(__('Cart %1 doesn\'t contain products', $cartId));
50+
}
51+
$comment = $orderComment->getComment();
52+
53+
$this->validateComment($comment);
54+
55+
try {
56+
$quote->setData(OrderComment::COMMENT_FIELD_NAME, strip_tags($comment));
57+
$this->quoteRepository->save($quote);
58+
} catch (\Exception $e) {
59+
throw new CouldNotSaveException(__('The order comment could not be saved'));
60+
}
61+
62+
return $comment;
63+
}
64+
65+
/**
66+
* @param string $comment
67+
* @throws ValidatorException
68+
*/
69+
protected function validateComment($comment)
70+
{
71+
$maxLength = $this->scopeConfig->getValue(OrderCommentConfigProvider::CONFIG_MAX_LENGTH);
72+
if ($maxLength && (mb_strlen($comment) > $maxLength)) {
73+
throw new ValidatorException(__('Comment is too long'));
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)