Ich habe versucht, den Preis meiner Produkte folgendermaßen zu ändern:
Controller.php:
[BEARBEITEN]
<?php
namespace MassiveArt\ShoppingCart\Controller\Index;
use Magento\Catalog\Model\ProductFactory;
use Magento\Checkout\Model\Cart;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Data\Form\FormKey;
class Index extends Action
{
/**
* @var FormKey
*/
protected $formKey;
/**
* @var Session
*/
protected $checkoutSession;
/**
* @var Cart
*/
protected $cart;
/**
* @var ProductFactory
*/
protected $productFactory;
/**
* Constructor.
*
* @param Context $context
* @param \Magento\Checkout\Model\Session $checkoutSession
* @param \Magento\Customer\Model\Session $customerSession
* @param JsonFactory $resultJsonFactory
* @param FormKey $formKey
* @param Cart $cart
* @param ProductFactory $productFactory
*/
public function __construct(
Context $context,
\Magento\Checkout\Model\Session $checkoutSession,
\Magento\Customer\Model\Session $customerSession,
JsonFactory $resultJsonFactory,
FormKey $formKey,
Cart $cart,
ProductFactory $productFactory
) {
$this->checkoutSession = $checkoutSession;
$this->customerSession = $customerSession;
$this->formKey = $formKey;
$this->resultJsonFactory = $resultJsonFactory;
$this->cart = $cart;
$this->productFactory = $productFactory;
parent::__construct($context);
}
public function execute()
{
try {
// Set result data and pass back
$result = $this->resultJsonFactory->create();
$allItems = $this->checkoutSession->getQuote()->getAllVisibleItems();
foreach ($allItems as $item) {
$item = ( $item->getParentItem() ? $item->getParentItem() : $item );
$price = 100; //set your price here
$item->setCustomPrice($price);
$item->setOriginalCustomPrice($price);
$item->setSubtotal($price);
$item->getProduct()->setIsSuperMode(true);
}
$this->checkoutSession->setTotalsCollectedFlag(false);
$this->checkoutSession->getQuote()->save();
$this->checkoutSession->getQuote()->setTotalsCollectedFlag(false);
$this->setTotalsCollectedFlag(false);
$result->setData(['message' => __("Products added succesfully")]);
return $result;
} catch (\Exception $e) {
$result->setData(['error' => __($e->getMessage())]);
return $result;
}
}
}
(BEARBEITEN) Mit dem neuen Code ändert sich der Preis, die Zwischensumme jedoch nicht! Wie Sie hier sehen können:
Danke im Voraus!
quote
von der checkoutSession
. Ich bin etwas verwirrt.