Php Magento 2.3以编程方式将产品添加到报价,价格不显示

Php Magento 2.3以编程方式将产品添加到报价,价格不显示,php,magento,magento2,shopping-cart,magento-2.3,Php,Magento,Magento2,Shopping Cart,Magento 2.3,我目前正试图使用此代码将产品添加到购物车中 $quote = $this->_session->getQuote(); $quote->addProduct($product); $this->_cartRepository->save($quote); 在新的会话中执行此操作时,产品价格和小计显示为0.00,但在摘要中小计和订单总数是正确的。 编辑产品数量后,价格将按其应有的方式运行 我尝试过使用$quote->collectTotals(),但这不

我目前正试图使用此代码将产品添加到购物车中

  $quote = $this->_session->getQuote();
  $quote->addProduct($product);
  $this->_cartRepository->save($quote);
在新的会话中执行此操作时,产品价格和小计显示为0.00,但在摘要中小计和订单总数是正确的。 编辑产品数量后,价格将按其应有的方式运行

我尝试过使用
$quote->collectTotals(),但这不会给出任何可见的更改

如何更新购物车,以便在打开购物车页面时显示产品价格?

请尝试以下代码:

use Magento\Checkout\Model\Cart as Quote;

class Add {   

   protected $quote = null;

   public function __construct( Quote $quote){
        $this->quote = $quote;            
   }

   public function test(\Magento\Catalog\Model\Product $product){
        $options = ['qty'=> 1];          
        $this->quote->addProduct($product, $options);
        //OR  $this->quote->addProductsByIds([$product->getId()]);

        $this->quote->save();
   }
}

请尝试使用以下代码:

use Magento\Checkout\Model\Cart as Quote;

class Add {   

   protected $quote = null;

   public function __construct( Quote $quote){
        $this->quote = $quote;            
   }

   public function test(\Magento\Catalog\Model\Product $product){
        $options = ['qty'=> 1];          
        $this->quote->addProduct($product, $options);
        //OR  $this->quote->addProductsByIds([$product->getId()]);

        $this->quote->save();
   }
}


您需要重新加载数据库中的报价,以使所有collectedTotals标志都为空

/** @var \Magento\Quote\Api\CartRepositoryInterface $quoteRepository */
$quoteRepository->get($quoteId);
$quote->addProduct($product, $request);
$quote->collectTotals()->save();

请参见
Magento\Quote\Model\Quote::collectedTotals
Magento\Quote\Model\Quote\Address\Total\Subtotal::collect

您需要从数据库重新加载报价,以使所有collectedTotals标志都为空

/** @var \Magento\Quote\Api\CartRepositoryInterface $quoteRepository */
$quoteRepository->get($quoteId);
$quote->addProduct($product, $request);
$quote->collectTotals()->save();
请参见
Magento\Quote\Model\Quote::collectTotals
Magento\Quote\Model\Quote\Address\Total\Subtotal::collect