Skip to content

Commit 6990227

Browse files
committed
FFWEB-3433: Add cart button for product list
Add cart button for product list
1 parent 296b410 commit 6990227

7 files changed

Lines changed: 90 additions & 20 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22
## Unreleased
3+
### Add
4+
- Add cart button for product list
5+
36
### Change
47
- Support tab navigation for search, suggest and paging components
58
- Upgrade Web Components version to v5.1.5

assets/css/styles.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,3 +814,7 @@ ff-suggest .ff-popular-searches div {
814814
background: #fff;
815815
}
816816

817+
.ff-cart-btn {
818+
margin-left: 10px;
819+
}
820+

metadata.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
'http_article_feed' => Controller\ArticleFeedController::class,
3232
'http_category_feed' => Controller\CategoryFeedController::class,
3333
'http_suggest_category_feed' => Controller\SuggestCategoryFeedController::class,
34+
'addtocart' => Controller\AddToCartController::class,
3435
],
3536
'extend' => [
3637
ModuleConfiguration::class => \Omikron\FactFinder\Oxid\Controller\Admin\ModuleConfiguration::class,
@@ -91,20 +92,27 @@
9192
// 'position' => $settingPosition++,
9293
// ],
9394
[
94-
'group' => 'ffAdvanced',
95-
'name' => 'ffTrackingAddToCartCount',
96-
'type' => 'select',
97-
'value' => 'count_selected_amount',
98-
'constraints' => 'count_as_one|count_selected_amount',
99-
'position' => $settingPosition++,
95+
'group' => 'ffAdvanced',
96+
'name' => 'ffSidAsUserId',
97+
'type' => 'bool',
98+
'value' => false,
99+
'position' => $settingPosition++,
100100
],
101101
[
102102
'group' => 'ffAdvanced',
103-
'name' => 'ffSidAsUserId',
103+
'name' => 'ffCartBtn',
104104
'type' => 'bool',
105105
'value' => false,
106106
'position' => $settingPosition++,
107107
],
108+
[
109+
'group' => 'ffAdvanced',
110+
'name' => 'ffTrackingAddToCartCount',
111+
'type' => 'select',
112+
'value' => 'count_selected_amount',
113+
'constraints' => 'count_as_one|count_selected_amount',
114+
'position' => $settingPosition++,
115+
],
108116
[
109117
'group' => 'ffFeatures',
110118
'name' => 'ffUseForCategories',
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Omikron\FactFinder\Oxid\Controller;
6+
7+
use OxidEsales\Eshop\Application\Controller\FrontendController;
8+
use OxidEsales\Eshop\Core\Registry;
9+
use OxidEsales\Eshop\Application\Model\Basket;
10+
use OxidEsales\Eshop\Application\Model\Article;
11+
12+
class AddToCartController extends FrontendController
13+
{
14+
public function addToCart(): void
15+
{
16+
$productNumber = Registry::getRequest()->getRequestParameter('productNumber');
17+
$amount = 1;
18+
$utilsView = Registry::getUtilsView();
19+
20+
try {
21+
if (!$productNumber) {
22+
throw new \Exception('Product with number ' . $productNumber . ' does not exist');
23+
}
24+
25+
$product = oxNew(Article::class);
26+
$productId = $this->getProductIdByNumber($productNumber);
27+
28+
if (!$productId || !$product->load($productId) || !$product->isBuyable()) {
29+
throw new \Exception('Product with number ' . $productNumber . ' does not exist or is not buyable');
30+
}
31+
32+
$basket = Registry::getSession()->getBasket();
33+
$basket->addToBasket($productId, $amount);
34+
$basket->calculateBasket(true);
35+
$utilsView->addErrorToDisplay('Product was added to the cart successfully.');
36+
$this->redirectToReferer();
37+
} catch (\Exception $e) {
38+
$utilsView->addErrorToDisplay('Error: ' . $e->getMessage(), false, false, 'error_message');
39+
$this->redirectToReferer();
40+
}
41+
}
42+
43+
private function getProductIdByNumber($productNumber)
44+
{
45+
$db = \OxidEsales\Eshop\Core\DatabaseProvider::getDb();
46+
$sQuery = "SELECT oxid FROM oxarticles WHERE oxartnum = ? AND oxactive = 1";
47+
48+
return $db->getOne($sQuery, [$productNumber]);
49+
}
50+
51+
private function redirectToReferer(): void
52+
{
53+
$referer = $_SERVER['HTTP_REFERER'] ?? Registry::getConfig()->getCurrentShopUrl();
54+
Registry::getUtils()->redirect($referer, false, 302);
55+
}
56+
}

views/admin_twig/de/ffwebcomponents_de_lang.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
'SHOP_MODULE_ffTrackingAddToCartCount_count_selected_amount' => 'track single click with selected amount',
1818
'SHOP_MODULE_ffUseProxy' => 'Proxy benutzen?',
1919
'SHOP_MODULE_ffSidAsUserId' => 'Die SID als userId senden, wenn der Benutzer nicht angemeldet ist?',
20+
'SHOP_MODULE_ffCartBtn' => 'Schaltfläche Warenkorb hinzufügen für die Produktliste',
2021
'SHOP_MODULE_GROUP_ffFeatures' => 'Features Settings',
2122
'SHOP_MODULE_ffUseForCategories' => 'Kategorieseiten mit FACT-Finder® rendern?',
2223
'SHOP_MODULE_ffCategoryPathFieldName' => 'Kategoriepfad-Feldname',

views/admin_twig/en/ffwebcomponents_en_lang.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
'SHOP_MODULE_ffTrackingAddToCartCount_count_selected_amount' => 'track single click with selected amount',
1818
'SHOP_MODULE_ffUseProxy' => 'Use Proxy?',
1919
'SHOP_MODULE_ffSidAsUserId' => 'Send the SID as userId when user not logged in?',
20+
'SHOP_MODULE_ffCartBtn' => 'Add cart button for product list',
2021
'SHOP_MODULE_GROUP_ffFeatures' => 'Features Settings',
2122
'SHOP_MODULE_ffUseForCategories' => 'Use FACT-Finder® for category pages?',
2223
'SHOP_MODULE_ffCategoryPathFieldName' => 'Category Path field name',

views/twig/webcomponents/widget/record_list.html.twig

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
<ff-record-list unresolved class="row gridView list-container" subscribe="{{ subscribe|default("true") }}">
22
<template data-role="record">
33
<ff-record class="productData col-xs-12 col-sm-6 col-md-4 productBox">
4-
<form action="{{ oViewConf.getSelfActionLink()|escape }}" method="post">
5-
<input type="hidden" name="cl" value="{{ oViewConf.getActiveClassName()|escape }}" />
6-
<input type="hidden" name="fnc" value="tobasket" />
74
<div class="card product-card">
85
<div class="product-img-wrapper">
96
<a data-anchor="{{ '{{ masterValues.Deeplink }}' }}" data-redirect="{{ '{{ masterValues.Deeplink }}' }}" data-redirect-target="_self" title="{{ '{{ masterValues.Name }}' }}">
@@ -25,22 +22,22 @@
2522
<div class="listDetails text-center">
2623
<div class="actions text-center">
2724
<div class="btn-group">
28-
29-
{# TODO Implement add to cart btn #}
30-
{# <button type="submit" class="btn btn-default hasTooltip" data-placement="bottom"#}
31-
{# title="" data-container="body"#}
32-
{# data-original-title="{{ translate({ ident: "ADD_TO_CART" }) }}">#}
33-
{# <svg>#}
34-
{# <use xlink:href="#bag"></use>#}
35-
{# </svg>#}
36-
{# </button>#}
3725
<a class="btn btn-primary" data-redirect="{{ '{{ masterValues.Deeplink }}' }}" data-redirect-target="_self" data-anchor="{{ '{{ masterValues.Deeplink }}' }}">{{ translate({ ident: "MORE_INFO" }) }}</a>
26+
27+
{% if oViewConf.getFFBoolConfigParam("ffCartBtn") %}
28+
<form action="{{ oViewConf.getSelfActionLink()|raw }}" method="post" class="add-to-cart-form">
29+
{{ oViewConf.getHiddenSid()|raw }}
30+
<input type="hidden" name="cl" value="addToCart">
31+
<input type="hidden" name="fnc" value="addToCart">
32+
<input type="hidden" name="productNumber" value="{{ '{{ variantValues.0.ProductNumber }}' }}">
33+
<button type="submit" class="btn btn-primary ff-cart-btn"> {{ translate({ ident: "TO_CART" }) }}</button>
34+
</form>
35+
{% endif %}
3836
</div>
3937
</div>
4038
</div>
4139
</div>
4240
</div>
43-
</form>
4441
</ff-record>
4542
</template>
4643
</ff-record-list>

0 commit comments

Comments
 (0)