Skip to content

Commit

Permalink
Merge pull request #849 from magento-tango/MAGETWO-64483
Browse files Browse the repository at this point in the history
Bug fixes:
* MAGETWO-56014: [GitHub] Dashboard Most Viewed Products Tab in admin - prices issue #5660
* MAGETWO-62044: Not possible to update or delete products in cart or checkout after deleting address
  • Loading branch information
magicbunneh committed Feb 17, 2017
2 parents f4c95fc + ad23561 commit e82ba2e
Show file tree
Hide file tree
Showing 8 changed files with 559 additions and 181 deletions.
11 changes: 10 additions & 1 deletion app/code/Magento/Backend/Block/Dashboard/Tab/Products/Viewed.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
*/
namespace Magento\Backend\Block\Dashboard\Tab\Products;

use Magento\Catalog\Model\Product;
use Magento\Catalog\Pricing\Price\FinalPrice;

/**
* Adminhtml dashboard most viewed products grid
*
Expand Down Expand Up @@ -66,8 +69,14 @@ protected function _prepareCollection()
);

$this->setCollection($collection);
parent::_prepareCollection();

/** @var Product $product */
foreach ($collection as $product) {
$product->setPrice($product->getPriceInfo()->getPrice(FinalPrice::PRICE_CODE)->getValue());
}

return parent::_prepareCollection();
return $this;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Quote/Api/Data/AddressInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ public function getCustomerAddressId();
/**
* Set customer address id
*
* @param int $customerAddressId
* @param int|null $customerAddressId
* @return $this
*/
public function setCustomerAddressId($customerAddressId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
*/
namespace Magento\Quote\Model\Quote\ShippingAssignment;

use Magento\Framework\Exception\LocalizedException;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Quote\Api\Data\ShippingAssignmentInterface;
use Magento\Framework\Exception\InputException;
use Magento\Quote\Model\ShippingAssignmentFactory;
use Magento\Quote\Model\Quote\Item\CartItemPersister;
use Magento\Customer\Api\AddressRepositoryInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\NoSuchEntityException;

class ShippingAssignmentProcessor
{
Expand All @@ -28,41 +32,56 @@ class ShippingAssignmentProcessor
*/
protected $cartItemPersister;

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

/**
* @param ShippingAssignmentFactory $shippingAssignmentFactory
* @param ShippingProcessor $shippingProcessor
* @param CartItemPersister $cartItemPersister
* @param AddressRepositoryInterface $addressRepository
*/
public function __construct(
ShippingAssignmentFactory $shippingAssignmentFactory,
ShippingProcessor $shippingProcessor,
CartItemPersister $cartItemPersister
CartItemPersister $cartItemPersister,
AddressRepositoryInterface $addressRepository = null
) {
$this->shippingAssignmentFactory = $shippingAssignmentFactory;
$this->shippingProcessor = $shippingProcessor;
$this->cartItemPersister = $cartItemPersister;
$this->addressRepository = $addressRepository
?: ObjectManager::getInstance()->get(AddressRepositoryInterface::class);
}

/**
* Create shipping assignment
*
* @param CartInterface $quote
* @return \Magento\Quote\Api\Data\ShippingAssignmentInterface
*/
public function create(CartInterface $quote)
{
/** @var \Magento\Quote\Model\Quote $quote */
$shippingAddress = $quote->getShippingAddress();

/** @var \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment */
$shippingAssignment = $this->shippingAssignmentFactory->create();
$shippingAssignment->setItems($quote->getItems());
$shippingAssignment->setShipping($this->shippingProcessor->create($shippingAddress));

return $shippingAssignment;
}

/**
* Save shipping assignment
*
* @param ShippingAssignmentInterface $shippingAssignment
* @param CartInterface $quote
* @return void
* @throws InputException
* @throws InputException|LocalizedException
*/
public function save(CartInterface $quote, ShippingAssignmentInterface $shippingAssignment)
{
Expand All @@ -73,6 +92,17 @@ public function save(CartInterface $quote, ShippingAssignmentInterface $shipping
$this->cartItemPersister->save($quote, $item);
}
}

$shippingAddress = $shippingAssignment->getShipping()->getAddress();

if ($shippingAddress->getCustomerAddressId()) {
try {
$this->addressRepository->getById($shippingAddress->getCustomerAddressId());
} catch (NoSuchEntityException $e) {
$shippingAddress->setCustomerAddressId(null);
}
}

$this->shippingProcessor->save($shippingAssignment->getShipping(), $quote);
}
}
37 changes: 32 additions & 5 deletions app/code/Magento/Quote/Model/QuoteRepository/SaveHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
namespace Magento\Quote\Model\QuoteRepository;

use Magento\Quote\Api\Data\CartInterface;
use Magento\Customer\Api\AddressRepositoryInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\InputException;

class SaveHandler
Expand All @@ -30,38 +33,48 @@ class SaveHandler
*/
private $shippingAssignmentPersister;

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

/**
* @param \Magento\Quote\Model\ResourceModel\Quote $quoteResource
* @param \Magento\Quote\Model\Quote\Item\CartItemPersister $cartItemPersister
* @param \Magento\Quote\Model\Quote\Address\BillingAddressPersister $billingAddressPersister
* @param \Magento\Quote\Model\Quote\ShippingAssignment\ShippingAssignmentPersister $shippingAssignmentPersister
* @param AddressRepositoryInterface $addressRepository
*/
public function __construct(
\Magento\Quote\Model\ResourceModel\Quote $quoteResource,
\Magento\Quote\Model\Quote\Item\CartItemPersister $cartItemPersister,
\Magento\Quote\Model\Quote\Address\BillingAddressPersister $billingAddressPersister,
\Magento\Quote\Model\Quote\ShippingAssignment\ShippingAssignmentPersister $shippingAssignmentPersister
\Magento\Quote\Model\Quote\ShippingAssignment\ShippingAssignmentPersister $shippingAssignmentPersister,
AddressRepositoryInterface $addressRepository = null
) {
$this->quoteResourceModel = $quoteResource;
$this->cartItemPersister = $cartItemPersister;
$this->billingAddressPersister = $billingAddressPersister;
$this->shippingAssignmentPersister = $shippingAssignmentPersister;
$this->addressRepository = $addressRepository
?: ObjectManager::getInstance()->get(AddressRepositoryInterface::class);
}

/**
* Process and save quote data
*
* @param CartInterface $quote
* @return CartInterface
*
* @throws InputException
* @throws \Magento\Framework\Exception\CouldNotSaveException
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function save(CartInterface $quote)
{
/** @var \Magento\Quote\Model\Quote $quote */
// Quote Item processing
$items = $quote->getItems();

if ($items) {
foreach ($items as $item) {
/** @var \Magento\Quote\Model\Quote\Item $item */
Expand All @@ -73,17 +86,28 @@ public function save(CartInterface $quote)

// Billing Address processing
$billingAddress = $quote->getBillingAddress();

if ($billingAddress) {
if ($billingAddress->getCustomerAddressId()) {
try {
$this->addressRepository->getById($billingAddress->getCustomerAddressId());
} catch (NoSuchEntityException $e) {
$billingAddress->setCustomerAddressId(null);
}
}

$this->billingAddressPersister->save($quote, $billingAddress);
}

$this->processShippingAssignment($quote);

$this->quoteResourceModel->save($quote->collectTotals());

return $quote;
}

/**
* Process shipping assignment
*
* @param \Magento\Quote\Model\Quote $quote
* @return void
* @throws InputException
Expand All @@ -92,11 +116,14 @@ private function processShippingAssignment($quote)
{
// Shipping Assignments processing
$extensionAttributes = $quote->getExtensionAttributes();

if (!$quote->isVirtual() && $extensionAttributes && $extensionAttributes->getShippingAssignments()) {
$shippingAssignments = $extensionAttributes->getShippingAssignments();

if (count($shippingAssignments) > 1) {
throw new InputException(__("Only 1 shipping assignment can be set"));
throw new InputException(__('Only 1 shipping assignment can be set'));
}

$this->shippingAssignmentPersister->save($quote, $shippingAssignments[0]);
}
}
Expand Down
Loading

0 comments on commit e82ba2e

Please sign in to comment.