0

This is my script:

    $quote = $this->quote->create();
    $quote->setStore($store);
    $quote->setCurrency();
    $quote->assignCustomer($customer);

        $product = $this->product->get($order['recipeSku']);
        $params = [
            'product' => $product->getId(),
            'bundle_option' => $this->getBundleOptions($product),
            'qty' => 1
        ];
//        $product->setPrice($product->getPrice());
        $quote->addProduct($product, $params); // the quantity will always be 1(one)

    $shippingAddress = $this->getCustomerAddress($customer->getDefaultShipping());
    $billingAddress = $this->getCustomerAddress($customer->getDefaultBilling());

    //Set Address to quote
    $quote->getBillingAddress()->addData($shippingAddress->getData());
    $quote->getShippingAddress()->addData($billingAddress->getData());

    $defaultCard = $this->getDefaultVault($customer->getId());

    $quote->setPaymentMethod(AdyenCcConfigProvider::CC_VAULT_CODE);
    $quote->setInventoryProcessed(false);
    $quote->save();

    // Set Sales Order Payment
    $quote->getPayment()->importData([
        'method' => AdyenCcConfigProvider::CC_VAULT_CODE,
        'public_hash' => $defaultCard->getData('public_hash'),
        'is_active_payment_token_enabler' => true,
        'purchase' => true
    ]);

    // Collect Totals & Save Quote
    $quote->collectTotals()->save();
    $order = $this->quoteManagement->submit($quote);
    $order->setEmailSent(0);


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

How can i add some bundle options as well ? This question is somehow related to this one: Create order of 'Bundle Product' programmatically with options selection: Magento 2

Right now i m getting this message We found an invalid request for adding product to quote.

Thx in advance for any suggestions :)

1 Answer 1

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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