分类
后端

magento2 如何更改购物车价格(和商品价格不一致)

use Magento\Checkout\Model\Session;
use Magento\Framework\App\Action\Context;


 private $session;
    public function __construct(
        Session $session,
        Context $context
    )
    {
        $this->session = $session;
        parent::__construct($context);
    }

    public function execute()
    {
        $quote = $this->session->getQuote();
        $items = $quote->getAllItems();
        if($items){
            foreach ($items as $item){
                //更改购物车商品价格
                $item->setPrice(888);
                $item->setOriginalCustomPrice(888);
                $item->getProduct()->setIsSuperMode(true);
            }
        }
        $quote->collectTotals()->save();
    }