Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

32977 Fixed text area for Customizable Options on the Checkout Cart Page #33256

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ $canApplyMsrp = $helper->isShowBeforeOrderConfirm($product) && $helper->isMinima
<dt><?= $block->escapeHtml($_option['label']) ?></dt>
<dd>
<?php if (isset($_formatedOptionValue['full_view'])) :?>
<?= $block->escapeHtml($_formatedOptionValue['full_view']) ?>
<?= $block->escapeHtml($_formatedOptionValue['full_view'], ['span', 'a']) ?>
<?php else :?>
<?= $block->escapeHtml($_formatedOptionValue['value'], ['span', 'a']) ?>
<?php endif; ?>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

use Magento\Catalog\Api\CategoryLinkManagementInterface;
use Magento\Catalog\Api\Data\ProductCustomOptionInterface;
use Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Catalog\Model\Product\Type;
use Magento\Catalog\Model\Product\Visibility;
use Magento\TestFramework\Helper\Bootstrap;

Bootstrap::getInstance()->reinitialize();

/** @var \Magento\TestFramework\ObjectManager $objectManager */
$objectManager = Bootstrap::getObjectManager();

/** @var CategoryLinkManagementInterface $categoryLinkManagement */
$categoryLinkManagement = $objectManager->create(CategoryLinkManagementInterface::class);

/** @var $product Product */
$product = $objectManager->create(Product::class);
$product->isObjectNew(true);
$product->setTypeId(Type::TYPE_SIMPLE)
->setAttributeSetId(4)
->setWebsiteIds([1])
->setName('Simple Product')
->setSku('simple_with_custom_option_text_area')
->setPrice(10)
->setWeight(1)
->setShortDescription("Short description")
->setTaxClassId(0)
->setDescription('Description with <b>html tag</b>')
->setMetaTitle('meta title')
->setMetaKeyword('meta keyword')
->setMetaDescription('meta description')
->setVisibility(Visibility::VISIBILITY_BOTH)
->setStatus(Status::STATUS_ENABLED)
->setStockData(
[
'use_config_manage_stock' => 1,
'qty' => 100,
'is_qty_decimal' => 0,
'is_in_stock' => 1,
]
)->setCanSaveCustomOptions(true)
->setHasOptions(true);

$oldOptions = [
[
'title' => 'area option',
'type' => 'area',
'is_require' => false,
'sort_order' => 1,
'price' => 20.0,
'price_type' => 'percent',
'sku' => 'sku_test',
'max_characters' => 350
]
];

$options = [];

/** @var ProductCustomOptionInterfaceFactory $customOptionFactory */
$customOptionFactory = $objectManager->create(ProductCustomOptionInterfaceFactory::class);

foreach ($oldOptions as $option) {
/** @var ProductCustomOptionInterface $option */
$option = $customOptionFactory->create(['data' => $option]);
$option->setProductSku($product->getSku());

$options[] = $option;
}

$product->setOptions($options);

/** @var ProductRepositoryInterface $productRepositoryFactory */
$productRepositoryFactory = $objectManager->create(ProductRepositoryInterface::class);
$productRepositoryFactory->save($product);
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Checkout\Block\Cart\Item;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\ObjectManagerInterface;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Model\Quote;
use Magento\Quote\Model\Quote\Item;
use PHPUnit\Framework\TestCase;
use Magento\TestFramework\Helper\Bootstrap;

/**
* Class RendererTest
* @package Magento\Checkout\Block\Cart\Item
* @magentoAppArea frontend
*/
class RendererTest extends TestCase
{
/**
* @var ObjectManagerInterface
*/
private $objectManager;

/**
* @var mixed
*/
private $renderer;

/**
* @var mixed
*/
public $productRepository;

/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->renderer = $this->objectManager->get(Renderer::class);
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
}

/**
* Gets quote by reserved order id.
*
* @param string $reservedOrderId
* @return Quote
*/
private function getQuote($reservedOrderId)
{
/** @var SearchCriteriaBuilder $searchCriteriaBuilder */
$searchCriteriaBuilder = $this->objectManager->get(SearchCriteriaBuilder::class);
$searchCriteria = $searchCriteriaBuilder->addFilter('reserved_order_id', $reservedOrderId)
->create();

/** @var CartRepositoryInterface $quoteRepository */
$quoteRepository = $this->objectManager->get(CartRepositoryInterface::class);

/** @var Quote[] $items */
$items = $quoteRepository->getList($searchCriteria)->getItems();

return $items[0];
}

/**
* Gets \Magento\Quote\Model\Quote\Item from \Magento\Quote\Model\Quote by product id
*
* @param Quote $quote
* @param string|int $productId
*
* @return Item|null
*/
private function _getQuoteItemIdByProductId($quote, $productId)
{
/** @var $quoteItems Item[] */
$quoteItems = $quote->getAllItems();
foreach ($quoteItems as $quoteItem) {
if ($productId == $quoteItem->getProductId()) {
return $quoteItem;
}
}
return null;
}

/**
* @magentoDataFixture Magento/Checkout/_files/cart_with_simple_product_and_custom_option_text_area.php
*/
public function testTextAreaCustomOption()
{
$quote = $this->getQuote('test_order_item_with_custom_option_text_area');

/** @var $product Product */
$product = $this->productRepository->get('simple_with_custom_option_text_area');

$quoteItem = $this->_getQuoteItemIdByProductId($quote, $product->getId());
$this->assertNotNull($quoteItem, 'Cannot get quote item for simple product with custom option text area');

$template = 'Magento_Checkout::cart/item/default.phtml';
$this->renderer->setTemplate($template);
$this->renderer->setItem($quoteItem);

$priceBlock = $this->objectManager->create(\Magento\Checkout\Block\Item\Price\Renderer::class);
$this->renderer->getLayout()->setBlock('checkout.item.price.unit', $priceBlock);
$this->renderer->getLayout()->setBlock('checkout.item.price.row', $priceBlock);
$html = $this->renderer->toHtml();

$this->assertMatchesRegularExpression('/Test product simple with
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that heredoc syntax looks more clear

custom option text area
with more 50 characters/', $html);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

use Magento\Catalog\Api\Data\ProductCustomOptionInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product\Option;
use Magento\Checkout\Model\Session;
use Magento\Framework\DataObject;
use Magento\Quote\Model\Quote;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;

Resolver::getInstance()->requireDataFixture('Magento/Catalog/_files/product_simple_with_custom_option_text_area.php');

/** @var ProductRepositoryInterface $productRepository */
$productRepository = Bootstrap::getObjectManager()
->create(ProductRepositoryInterface::class);
$product = $productRepository->get('simple_with_custom_option_text_area');

$options = [];
/** @var $option Option */
foreach ($product->getOptions() as $option) {
switch ($option->getGroupByType()) {
case ProductCustomOptionInterface::OPTION_GROUP_SELECT:
$value = key($option->getValues());
break;
default:
$value = 'Test product simple with
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that heredoc syntax looks more clear

custom option text area
with more 50 characters';
break;
}
$options[$option->getId()] = $value;
}

$requestInfo = new DataObject(['qty' => 1, 'options' => $options]);

/** @var $cart \Magento\Checkout\Model\Cart */
$quote = Bootstrap::getObjectManager()->create(Quote::class);
$quote->setReservedOrderId('test_order_item_with_custom_option_text_area');
$quote->addProduct($product, $requestInfo);
$quote->save();

/** @var $objectManager \Magento\TestFramework\ObjectManager */
$objectManager = Bootstrap::getObjectManager();
$objectManager->removeSharedInstance(Session::class);