Skip to content

Commit

Permalink
Fix add to cart bundle products (#290)
Browse files Browse the repository at this point in the history
  • Loading branch information
pedromcp90 committed Jul 17, 2023
1 parent d022c44 commit 9e222ed
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 7 deletions.
46 changes: 41 additions & 5 deletions Controller/Product/AddToCart.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php

namespace Doofinder\Feed\Controller\Product;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Catalog\Model\Product\Type as ProductType;
use Magento\Quote\Model\Quote\Item as QuoteItem;
use Magento\Framework\DataObject;

/**
* Class AddToCart. This class aims at opening a way for adding to cart from the layer
Expand Down Expand Up @@ -58,18 +59,35 @@ public function __construct(
*/
public function execute()
{
$product = $this->productRepository->getById($this->getRequest()->getParam('id'));
$productId = $this->getRequest()->getParam('id');
$product = $this->productRepository->getById($productId);
$qty = $this->getRequest()->getParam('qty');

$session = $this->checkoutSession->create();
$quote = $session->getQuote();
$params = [
'product' => $productId,
'qty' => $qty
];

$this->logger->info("Request add item to cart");
$this->logger->info("Product: ", ["product" => $product]);

$result = $quote->addProduct($product, $qty);
if ($product->getTypeId() == ProductType::TYPE_BUNDLE) {
$bundleOptions = $this->getBundleOptions($product);
if (!empty($bundleOptions)) {
$params['bundle_option'] = $bundleOptions;
$this->logger->info("Bundle options: ", ["bundle_options" => $bundleOptions]);
}
}

$params = new DataObject($params);
$result = $quote->addProduct($product, $params);

if (is_a($result, QuoteItem::class)) {
//Update totals
$quote->setTriggerRecollect(1);
$quote->setIsActive(true);
$quote->collectTotals();
$this->cartRepository->save($quote);
$session->replaceQuote($quote)->unsLastRealOrderId();
} else {
Expand All @@ -81,4 +99,22 @@ public function execute()
return $resultJson->setData(['product_url' => $product_url]);
}
}

/**
* Get all the selection products used in bundle product
* @param $product
* @return mixed
*/
private function getBundleOptions($product)
{
$bundleOptions = [];
$productType = $product->getTypeInstance();
$optionsIds = $productType->getOptionsIds($product);
$selectionCollection = $productType->getSelectionsCollection($optionsIds, $product);

foreach ($selectionCollection as $selection) {
$bundleOptions[$selection->getOptionId()][] = $selection->getSelectionId();
}
return $bundleOptions;
}
}
Binary file modified Manual.doc
Binary file not shown.
Binary file modified Manual.pdf
Binary file not shown.
4 changes: 4 additions & 0 deletions Model/Config/Backend/DisplayLayerState.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ private function post(array $body): string
private function getAuthToken(): string
{
$apiKey = $this->storeConfig->getApiKey();
if (empty($apiKey)) {
throw new InvalidApiKey("Please enter the API Key");
}

$clusterToken = explode('-', $apiKey);
if (count($clusterToken) != 2) {
throw new InvalidApiKey("Invalid API Key provided");
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "doofinder/doofinder-magento2",
"version": "0.13.3",
"version": "0.13.4",
"description": "Doofinder module for Magento 2",
"type": "magento2-module",
"require": {
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Doofinder_Feed" setup_version="0.13.3">
<module name="Doofinder_Feed" setup_version="0.13.4">
<sequence>
<module name="Magento_Integration" />
</sequence>
Expand Down

0 comments on commit 9e222ed

Please sign in to comment.