Skip to main content
deleted 1 character in body
Source Link
Attila Naghi
  • 4.1k
  • 4
  • 40
  • 76

Eventually, I figured it out. I leaveleft the script, maybe it will help someone. This works for Magento 2.4.1:

public function myMethod()
{
.......
    $product = $this->product->get('BUNDLE_PRODUCT_SKU');
    $params = [
        'product' => $product->getId(),
        'bundle_option' => $this->getBundleOptions($product),
        'qty' => 1
    ];
    $quote->addProduct($product, new DataObject($params));
.....
}
    public function getBundleOptions($product)
    {
        $selectionCollection = $product->getTypeInstance()
            ->getSelectionsCollection(
                $product->getTypeInstance()->getOptionsIds($product),
                $product
            );
        $bundleOptions = [];
        foreach ($selectionCollection as $selection) {
                $bundleOptions[$selection->getOptionId()][] = $selection->getSelectionId();
        }
        return $bundleOptions;
    }

Eventually, I figured it out. I leave the script, maybe it will help someone. This works for Magento 2.4.1:

public function myMethod()
{
.......
    $product = $this->product->get('BUNDLE_PRODUCT_SKU');
    $params = [
        'product' => $product->getId(),
        'bundle_option' => $this->getBundleOptions($product),
        'qty' => 1
    ];
    $quote->addProduct($product, new DataObject($params));
.....
}
    public function getBundleOptions($product)
    {
        $selectionCollection = $product->getTypeInstance()
            ->getSelectionsCollection(
                $product->getTypeInstance()->getOptionsIds($product),
                $product
            );
        $bundleOptions = [];
        foreach ($selectionCollection as $selection) {
                $bundleOptions[$selection->getOptionId()][] = $selection->getSelectionId();
        }
        return $bundleOptions;
    }

Eventually, I figured it out. I left the script, maybe it will help someone. This works for Magento 2.4.1:

public function myMethod()
{
.......
    $product = $this->product->get('BUNDLE_PRODUCT_SKU');
    $params = [
        'product' => $product->getId(),
        'bundle_option' => $this->getBundleOptions($product),
        'qty' => 1
    ];
    $quote->addProduct($product, new DataObject($params));
.....
}
    public function getBundleOptions($product)
    {
        $selectionCollection = $product->getTypeInstance()
            ->getSelectionsCollection(
                $product->getTypeInstance()->getOptionsIds($product),
                $product
            );
        $bundleOptions = [];
        foreach ($selectionCollection as $selection) {
                $bundleOptions[$selection->getOptionId()][] = $selection->getSelectionId();
        }
        return $bundleOptions;
    }
Source Link
Attila Naghi
  • 4.1k
  • 4
  • 40
  • 76

Eventually, I figured it out. I leave the script, maybe it will help someone. This works for Magento 2.4.1:

public function myMethod()
{
.......
    $product = $this->product->get('BUNDLE_PRODUCT_SKU');
    $params = [
        'product' => $product->getId(),
        'bundle_option' => $this->getBundleOptions($product),
        'qty' => 1
    ];
    $quote->addProduct($product, new DataObject($params));
.....
}
    public function getBundleOptions($product)
    {
        $selectionCollection = $product->getTypeInstance()
            ->getSelectionsCollection(
                $product->getTypeInstance()->getOptionsIds($product),
                $product
            );
        $bundleOptions = [];
        foreach ($selectionCollection as $selection) {
                $bundleOptions[$selection->getOptionId()][] = $selection->getSelectionId();
        }
        return $bundleOptions;
    }