How to get a simple product id from configurable in checkout order summary in magento 2

magento.stackexchange https://magento.stackexchange.com/questions/275932

  •  02-03-2021
  •  | 
  •  

Question

How to get a simple product id from configurable in checkout order summary in magento 2

image is https://prnt.sc/nsnh7y

code:

    <?php
namespace Cm\Preorder\Plugin\Checkout\Model;

use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Catalog\Model\ProductRepository as ProductRepository;
use Magento\CatalogInventory\Model\Stock\StockItemRepository;

class DefaultConfigProvider extends \Magento\Framework\Model\AbstractModel
{
    protected $checkoutSession;

    protected $stockItem;

    protected $_productRepository;

    private $_objectManager;



    public function __construct(
        CheckoutSession $checkoutSession,
        ProductRepository $productRepository,
        \Magento\CatalogInventory\Model\Stock\StockItemRepository $stockItem,
          \Magento\Framework\ObjectManagerInterface $objectmanager
    ) {
        $this->checkoutSession = $checkoutSession;
        $this->_productRepository = $productRepository;
        $this->stockItem = $stockItem;
        $this->_objectManager = $objectmanager;

    }

    public function afterGetConfig(
        \Magento\Checkout\Model\DefaultConfigProvider $subject, 
        array $result
    ) {
        $items = $result['totalsData']['items'];
        foreach ($items as $index => $item) {
            $quoteItem = $this->checkoutSession->getQuote()->getItemById($item['item_id']);
            $product = $this->_productRepository->getById($quoteItem->getProduct()->getId());

            $productId = $product->getId();
            $stock = $this->stockItem->get($productId);
        /*    $preorder_note = $stock->getData('preorder_note'); */
            $result['quoteItemData'][$index]['preorder_note'] =$productId;
        }
        return $result;
    }


}
Was it helpful?

Solution

Try following way:

<?php
namespace SR\MagentoCommunity\Plugin\Checkout\Model;

use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Catalog\Model\ProductRepository as ProductRepository;
use Magento\CatalogInventory\Model\Stock\StockItemRepository;

class DefaultConfigProvider
{
    /**
     * @var CheckoutSession
     */
    protected $checkoutSession;

    /**
     * @var StockItemRepository
     */
    protected $stockItem;

    /**
     * @var ProductRepository
     */
    protected $productRepository;

    /**
     * DefaultConfigProvider constructor.
     * @param CheckoutSession $checkoutSession
     * @param ProductRepository $productRepository
     * @param StockItemRepository $stockItem
     */
    public function __construct(
        CheckoutSession $checkoutSession,
        ProductRepository $productRepository,
        StockItemRepository $stockItem
    ) {
        $this->checkoutSession = $checkoutSession;
        $this->productRepository = $productRepository;
        $this->stockItem = $stockItem;
    }

    public function afterGetConfig(
        \Magento\Checkout\Model\DefaultConfigProvider $subject,
        array $result
    ) {

        /** @var \Magento\Quote\Model\Quote $quote */
        $quote = $this->checkoutSession->getQuote();
        $quoteItemsArray = [];
        /** @var \Magento\Quote\Model\Quote\Item $item */
        foreach ($quote->getAllItems() as $item) {
            if ($item->getParentItemId()) {
                $quoteItemsArray[$item->getParentItemId()] = $item->getProductId();
            }
        }

        $items = $result['totalsData']['items'];
        foreach ($items as $index => $item) {
            if (isset($quoteItemsArray[$item['item_id']])) {
                $item['child_product_id'] = $quoteItemsArray[$item['item_id']];
                $result['totalsData']['items'][$index] = $item;
            }
        }

        return $result;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top