2

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;
    }


}
1

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;
    }
}
  • its working @sohel – divya sekar May 24 at 13:35
  • can you give any trick to get a selected simple product id from configurable option in category product listing page – divya sekar May 24 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.