Out of the box Magento supports Bundle products, You can customize the item from the given option selection and add items to a single click of add to cart.
catalog_product_bundle_selection table used to store the bundle item’s relation with the child item id, option id, qty, position of the item and default item selection from the given option.
You can retrieve the selection id by the Bundle Item Id and Child Product id using database query in the function,
<?php namespace Jesadiya\SelectionId\Model; use Magento\Framework\App\ResourceConnection; class BundleSelectionId { private const BUNDLE_SELECTION_TABLE = "catalog_product_bundle_selection"; /** * @var ResourceConnection */ private $resourceConnection; public function __construct( ResourceConnection $resourceConnection ) { $this->resourceConnection = $resourceConnection; } /** * Get Selection Id by parent item and child item id for the bundle product * * @param int $bundleItemId * @param int $childItemId * @return array|bool */ public function getSelectionId(int $bundleItemId, int $childItemId) { $tableName = $this->resourceConnection->getTableName(self::BUNDLE_SELECTION_TABLE);; $connection = $this->resourceConnection->getConnection(); $select = $connection->select() ->from( ['e' => $tableName], ['selection_id','option_id','product_id'] ) ->where( "e.product_id = :product_id" )->where( "e.parent_product_id = :parent_product_id" ); $bind = ['product_id'=> $childItemId, 'parent_product_id'=> $bundleItemId]; return $connection->fetchRow($select, $bind); } }
You can get the selection_id to passing the required parameter in the function,
$bundleItemId = 52; $childItemId = 29; echo $selectionId = $this->getSelectionId($bundleItemId, $childItemId);
Output:
2