diff --git a/CHANGELOG.md b/CHANGELOG.md index 342c14a8..f0298d42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog ## Unreleased +### Add +- Add cart button for product list + ### Change - Support tab navigation for search, suggest and paging components - Upgrade Web Components version to v5.1.5 diff --git a/assets/css/styles.css b/assets/css/styles.css index bc0083de..066bea02 100644 --- a/assets/css/styles.css +++ b/assets/css/styles.css @@ -814,3 +814,7 @@ ff-suggest .ff-popular-searches div { background: #fff; } +.ff-cart-btn { + margin-left: 10px; +} + diff --git a/metadata.php b/metadata.php index bb3f6e89..9d72ea20 100755 --- a/metadata.php +++ b/metadata.php @@ -31,6 +31,7 @@ 'http_article_feed' => Controller\ArticleFeedController::class, 'http_category_feed' => Controller\CategoryFeedController::class, 'http_suggest_category_feed' => Controller\SuggestCategoryFeedController::class, + 'addtocart' => Controller\AddToCartController::class, ], 'extend' => [ ModuleConfiguration::class => \Omikron\FactFinder\Oxid\Controller\Admin\ModuleConfiguration::class, @@ -91,20 +92,27 @@ // 'position' => $settingPosition++, // ], [ - 'group' => 'ffAdvanced', - 'name' => 'ffTrackingAddToCartCount', - 'type' => 'select', - 'value' => 'count_selected_amount', - 'constraints' => 'count_as_one|count_selected_amount', - 'position' => $settingPosition++, + 'group' => 'ffAdvanced', + 'name' => 'ffSidAsUserId', + 'type' => 'bool', + 'value' => false, + 'position' => $settingPosition++, ], [ 'group' => 'ffAdvanced', - 'name' => 'ffSidAsUserId', + 'name' => 'ffCartBtn', 'type' => 'bool', 'value' => false, 'position' => $settingPosition++, ], + [ + 'group' => 'ffAdvanced', + 'name' => 'ffTrackingAddToCartCount', + 'type' => 'select', + 'value' => 'count_selected_amount', + 'constraints' => 'count_as_one|count_selected_amount', + 'position' => $settingPosition++, + ], [ 'group' => 'ffFeatures', 'name' => 'ffUseForCategories', diff --git a/src/Controller/AddToCartController.php b/src/Controller/AddToCartController.php new file mode 100644 index 00000000..fe4db9fa --- /dev/null +++ b/src/Controller/AddToCartController.php @@ -0,0 +1,55 @@ +getRequestParameter('productNumber'); + $amount = 1; + $utilsView = Registry::getUtilsView(); + + try { + if (!$productNumber) { + throw new \Exception("Product with number $productNumber does not exist"); + } + + $product = oxNew(Article::class); + $productId = $this->getProductIdByNumber($productNumber); + + if (!$productId || !$product->load($productId) || !$product->isBuyable()) { + throw new \Exception("Product with number $productNumber does not exist or is not buyable"); + } + + $basket = Registry::getSession()->getBasket(); + $basket->addToBasket($productId, $amount); + $basket->calculateBasket(true); + $utilsView->addErrorToDisplay('Product was added to the cart successfully.'); + $this->redirectToReferer(); + } catch (\Exception $e) { + $utilsView->addErrorToDisplay('Error: ' . $e->getMessage(), false, false, 'error_message'); + $this->redirectToReferer(); + } + } + + private function getProductIdByNumber($productNumber) + { + $database = \OxidEsales\Eshop\Core\DatabaseProvider::getDb(); + $sQuery = 'SELECT oxid FROM oxarticles WHERE oxartnum = ? AND oxactive = 1'; + + return $database->getOne($sQuery, [$productNumber]); + } + + private function redirectToReferer(): void + { + $referer = $_SERVER['HTTP_REFERER'] ?? Registry::getConfig()->getCurrentShopUrl(); + Registry::getUtils()->redirect($referer, false, 302); + } +} diff --git a/views/admin_twig/de/ffwebcomponents_de_lang.php b/views/admin_twig/de/ffwebcomponents_de_lang.php index 2a76ae5b..dcf8de75 100644 --- a/views/admin_twig/de/ffwebcomponents_de_lang.php +++ b/views/admin_twig/de/ffwebcomponents_de_lang.php @@ -17,6 +17,7 @@ 'SHOP_MODULE_ffTrackingAddToCartCount_count_selected_amount' => 'track single click with selected amount', 'SHOP_MODULE_ffUseProxy' => 'Proxy benutzen?', 'SHOP_MODULE_ffSidAsUserId' => 'Die SID als userId senden, wenn der Benutzer nicht angemeldet ist?', + 'SHOP_MODULE_ffCartBtn' => 'Schaltfläche Warenkorb hinzufügen für die Produktliste', 'SHOP_MODULE_GROUP_ffFeatures' => 'Features Settings', 'SHOP_MODULE_ffUseForCategories' => 'Kategorieseiten mit FACT-Finder® rendern?', 'SHOP_MODULE_ffCategoryPathFieldName' => 'Kategoriepfad-Feldname', diff --git a/views/admin_twig/en/ffwebcomponents_en_lang.php b/views/admin_twig/en/ffwebcomponents_en_lang.php index 36a61be2..9125996e 100644 --- a/views/admin_twig/en/ffwebcomponents_en_lang.php +++ b/views/admin_twig/en/ffwebcomponents_en_lang.php @@ -17,6 +17,7 @@ 'SHOP_MODULE_ffTrackingAddToCartCount_count_selected_amount' => 'track single click with selected amount', 'SHOP_MODULE_ffUseProxy' => 'Use Proxy?', 'SHOP_MODULE_ffSidAsUserId' => 'Send the SID as userId when user not logged in?', + 'SHOP_MODULE_ffCartBtn' => 'Add cart button for product list', 'SHOP_MODULE_GROUP_ffFeatures' => 'Features Settings', 'SHOP_MODULE_ffUseForCategories' => 'Use FACT-Finder® for category pages?', 'SHOP_MODULE_ffCategoryPathFieldName' => 'Category Path field name', diff --git a/views/twig/webcomponents/widget/record_list.html.twig b/views/twig/webcomponents/widget/record_list.html.twig index 250b53fe..5721570a 100644 --- a/views/twig/webcomponents/widget/record_list.html.twig +++ b/views/twig/webcomponents/widget/record_list.html.twig @@ -1,9 +1,6 @@