Skip to content

Commit

Permalink
Merge pull request #7595 from magento-gl/gl_pr_arrows_april21_2022
Browse files Browse the repository at this point in the history
Arrows Team - Bugfix delivery
  • Loading branch information
sidolov authored May 16, 2022
2 parents 7e794ce + ae60e49 commit f1adf23
Show file tree
Hide file tree
Showing 9 changed files with 420 additions and 109 deletions.
11 changes: 3 additions & 8 deletions app/code/Magento/Catalog/Block/Product/Compare/ListCompare.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ class ListCompare extends \Magento\Catalog\Block\Product\AbstractProduct
protected $_useLinkForAsLowAs = false;

/**
* Customer id
*
* @var null|int
*/
protected $_customerId = null;
Expand All @@ -52,22 +50,16 @@ class ListCompare extends \Magento\Catalog\Block\Product\AbstractProduct
protected $httpContext;

/**
* Customer visitor
*
* @var \Magento\Customer\Model\Visitor
*/
protected $_customerVisitor;

/**
* Catalog product visibility
*
* @var \Magento\Catalog\Model\Product\Visibility
*/
protected $_catalogProductVisibility;

/**
* Item collection factory
*
* @var \Magento\Catalog\Model\ResourceModel\Product\Compare\Item\CollectionFactory
*/
protected $_itemCollectionFactory;
Expand Down Expand Up @@ -205,6 +197,9 @@ public function getProductAttributeValue($product, $attribute)
} else {
$value = $product->getData($attribute->getAttributeCode());
}
if (is_array($value)) {
return __('N/A');
}
return (string)$value == '' ? __('No') : $value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Magento\Catalog\Block\Product\Compare\ListCompare;
use Magento\Catalog\Block\Product\Context;
use Magento\Catalog\Model\Product;
use Magento\Eav\Model\Entity\Attribute\AttributeInterface;
use Magento\Eav\Model\Entity\Attribute\Frontend\AbstractFrontend;
use Magento\Framework\Pricing\Render;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Framework\View\Layout;
Expand Down Expand Up @@ -50,6 +52,48 @@ protected function tearDown(): void
$this->block = null;
}

/**
* @dataProvider attributeDataProvider
* @param array $attributeData
* @param string $expectedResult
*/
public function testProductAttributeValue($attributeData, $expectedResult)
{
$attribute = $this->getMockBuilder(AttributeInterface::class)
->addMethods(['getAttributeCode', 'getSourceModel', 'getFrontendInput', 'getFrontend'])
->getMockForAbstractClass();
$frontEndModel = $this->createPartialMock(AbstractFrontend::class, ['getValue']);
$productMock = $this->createPartialMock(Product::class, ['getId', 'getData', 'hasData']);
$productMock->expects($this->any())
->method('hasData')
->with($attributeData['attribute_code'])
->willReturn(true);
$productMock->expects($this->any())
->method('getData')
->with($attributeData['attribute_code'])
->willReturn($attributeData['attribute_value']);
$attribute->expects($this->any())
->method('getAttributeCode')
->willReturn($attributeData['attribute_code']);
$attribute->expects($this->any())
->method('getSourceModel')
->willReturn($attributeData['source_model']);
$attribute->expects($this->any())
->method('getFrontendInput')
->willReturn($attributeData['frontend_input']);
$frontEndModel->expects($this->any())
->method('getValue')
->with($productMock)
->willReturn($attributeData['attribute_value']);
$attribute->expects($this->any())
->method('getFrontend')
->willReturn($frontEndModel);
$this->assertEquals(
$expectedResult,
$this->block->getProductAttributeValue($productMock, $attribute)
);
}

public function testGetProductPrice()
{
//Data
Expand Down Expand Up @@ -84,4 +128,31 @@ public function testGetProductPrice()

$this->assertEquals($expectedResult, $this->block->getProductPrice($product, '-compare-list-top'));
}

/**
* @return array
*/
public function attributeDataProvider(): array
{
return [
[
'attributeData' => [
'attribute_code' => 'tier_price',
'source_model' => null,
'frontend_input' => 'text',
'attribute_value' => []
],
'expectedResult' => __('N/A')
],
[
'attributeData' => [
'attribute_code' => 'special_price',
'source_model' => null,
'frontend_input' => 'decimal',
'attribute_value' => 50.00
],
'expectedResult' => '50.00'
]
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,55 +8,27 @@
namespace Magento\Multishipping\Model\Cart\Controller;

use Magento\Checkout\Controller\Cart;
use Magento\Checkout\Model\Session;
use Magento\Customer\Api\AddressRepositoryInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Multishipping\Model\DisableMultishipping;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Model\Quote;
use Magento\Multishipping\Model\Cart\MultishippingClearItemAddress;

/**
* Cleans shipping addresses and item assignments after MultiShipping flow
*/
class CartPlugin
{
/**
* @var CartRepositoryInterface
* @var MultishippingClearItemAddress
*/
private $cartRepository;
private $multishippingClearItemAddress;

/**
* @var Session
*/
private $checkoutSession;

/**
* @var AddressRepositoryInterface
*/
private $addressRepository;

/**
* @var DisableMultishipping
*/
private $disableMultishipping;

/**
* @param CartRepositoryInterface $cartRepository
* @param Session $checkoutSession
* @param AddressRepositoryInterface $addressRepository
* @param DisableMultishipping $disableMultishipping
* @param MultishippingClearItemAddress $multishippingClearItemAddress
*/
public function __construct(
CartRepositoryInterface $cartRepository,
Session $checkoutSession,
AddressRepositoryInterface $addressRepository,
DisableMultishipping $disableMultishipping
MultishippingClearItemAddress $multishippingClearItemAddress
) {
$this->cartRepository = $cartRepository;
$this->checkoutSession = $checkoutSession;
$this->addressRepository = $addressRepository;
$this->disableMultishipping = $disableMultishipping;
$this->multishippingClearItemAddress = $multishippingClearItemAddress;
}

/**
Expand All @@ -70,62 +42,6 @@ public function __construct(
*/
public function beforeDispatch(Cart $subject, RequestInterface $request)
{
/** @var Quote $quote */
$quote = $this->checkoutSession->getQuote();
$isMultipleShippingAddressesPresent = $quote->isMultipleShippingAddresses();
if ($isMultipleShippingAddressesPresent || $this->isDisableMultishippingRequired($request, $quote)) {
$this->disableMultishipping->execute($quote);
foreach ($quote->getAllShippingAddresses() as $address) {
$quote->removeAddress($address->getId());
}

$shippingAddress = $quote->getShippingAddress();
$defaultShipping = $quote->getCustomer()->getDefaultShipping();
if ($defaultShipping) {
$defaultCustomerAddress = $this->addressRepository->getById($defaultShipping);
$shippingAddress->importCustomerAddressData($defaultCustomerAddress);
}
if ($isMultipleShippingAddressesPresent) {
$this->checkoutSession->setMultiShippingAddressesFlag(true);
}
$this->cartRepository->save($quote);
} elseif ($this->disableMultishipping->execute($quote) && $this->isVirtualItemInQuote($quote)) {
$quote->setTotalsCollectedFlag(false);
$this->cartRepository->save($quote);
}
}

/**
* Checks whether quote has virtual items
*
* @param Quote $quote
* @return bool
*/
private function isVirtualItemInQuote(Quote $quote): bool
{
$items = $quote->getItems();
if (!empty($items)) {
foreach ($items as $item) {
if ($item->getIsVirtual()) {
return true;
}
}
}

return false;
}

/**
* Check if we have to disable multishipping mode depends on the request action name
*
* We should not disable multishipping mode if we are adding a new product item to the existing quote
*
* @param RequestInterface $request
* @param Quote $quote
* @return bool
*/
private function isDisableMultishippingRequired(RequestInterface $request, Quote $quote): bool
{
return $request->getActionName() !== "add" && $quote->getIsMultiShipping();
$this->multishippingClearItemAddress->clearAddressItem($subject, $request);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Multishipping\Model\Cart\Controller;

use Magento\Checkout\Controller\Sidebar\UpdateItemQty;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Multishipping\Model\Cart\MultishippingClearItemAddress;

/**
* Cleans shipping addresses and item assignments after MultiShipping flow
*/
class MiniCartPlugin
{
/**
* @var MultishippingClearItemAddress
*/
private $multishippingClearItemAddress;

/**
* @param MultishippingClearItemAddress $multishippingClearItemAddress
*/
public function __construct(
MultishippingClearItemAddress $multishippingClearItemAddress
) {
$this->multishippingClearItemAddress = $multishippingClearItemAddress;
}

/**
* Cleans shipping addresses and item assignments after MultiShipping flow
*
* @param UpdateItemQty $subject
* @param RequestInterface $request
* @return void
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @throws LocalizedException
*/
public function beforeDispatch(UpdateItemQty $subject, RequestInterface $request)
{
$this->multishippingClearItemAddress->clearAddressItem($subject, $request);
}
}
Loading

0 comments on commit f1adf23

Please sign in to comment.