我试图这样更改我的产品的价格:
Controller.php:
[编辑]
   <?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;
        }
    }
}
提前致谢!
                  嗨,你能详细点吗?您使用哪个控制器,您的任务是什么?当客户将产品添加到购物车时,是否要更改价格?
                
                
                  
                    —
                    索尼
                    
                  
                
              
                  您好索尼,不,我想更改价格,如果单击按钮,而不是将产品添加到购物车。现在,我只想将所有产品的价格更改为100美元以进行测试。我会更新我的问题向您展示更多的代码
                
                
                  
                    —
                    费利克斯·圣豪
                    
                  
                
              
                  抱歉,恐怕无法理解您正在尝试什么。当我查看您的代码时,您希望更改购物篮中物品的价格。确实,您是
                
                  
                    —
                    索尼
                    
                  
                
              quote从拨打电话的checkoutSession。我有点困惑。
                
                  是的,我想更改购物篮中物品的价格。我还尝试了其他一些方法来实现此目的,而我尝试的最后一个方法是带有引号的当前代码。
                
                
                  
                    —
                    FelixSchönherr19年
                    
                  
                
              
                  使用您需要使用插件或观察器的控制器,价格更改将不起作用。
                
                
                  
                    —
                    Aasim Goriya 
                    
                  
                
              