From 7d5803a541b9b9528c83c2d40b4490d7f5e2fa7d Mon Sep 17 00:00:00 2001 From: badsuricate Date: Wed, 23 Oct 2013 11:47:42 +0200 Subject: [PATCH 1/8] Add routes for mini-cart and addedToCart --- core/lib/Thelia/Config/Resources/routing/front.xml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/core/lib/Thelia/Config/Resources/routing/front.xml b/core/lib/Thelia/Config/Resources/routing/front.xml index cf8ea636e..bbfb36d35 100755 --- a/core/lib/Thelia/Config/Resources/routing/front.xml +++ b/core/lib/Thelia/Config/Resources/routing/front.xml @@ -9,6 +9,14 @@ index + + Thelia\Controller\Front\DefaultController::noAction + includes/mini-cart + + + Thelia\Controller\Front\DefaultController::noAction + includes/addedToCart + From 5e62da9d29bba6f71ef1cfa0577b6d5836897330 Mon Sep 17 00:00:00 2001 From: badsuricate Date: Wed, 23 Oct 2013 15:20:42 +0200 Subject: [PATCH 2/8] Add new Arguments for CartLoop New args = Limit and position --- core/lib/Thelia/Core/Template/Loop/Cart.php | 32 +++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/core/lib/Thelia/Core/Template/Loop/Cart.php b/core/lib/Thelia/Core/Template/Loop/Cart.php index 5dc40218b..be39371ad 100755 --- a/core/lib/Thelia/Core/Template/Loop/Cart.php +++ b/core/lib/Thelia/Core/Template/Loop/Cart.php @@ -12,8 +12,11 @@ namespace Thelia\Core\Template\Loop; use Thelia\Core\Template\Element\BaseLoop; use Thelia\Core\Template\Element\LoopResult; use Thelia\Core\Template\Element\LoopResultRow; +use Thelia\Core\Template\Loop\Argument\Argument; use Thelia\Core\Template\Loop\Argument\ArgumentCollection; use Thelia\Model\CountryQuery; +use Thelia\Type; +use Thelia\Type\TypeCollection; class Cart extends BaseLoop { @@ -40,7 +43,8 @@ class Cart extends BaseLoop protected function getArgDefinitions() { return new ArgumentCollection( - + Argument::createIntTypeArgument('limit'), + Argument::createAnyTypeArgument('position') ); } @@ -74,6 +78,7 @@ class Cart extends BaseLoop { $cart = $this->getCart($this->request); + $cartItems = $cart->getCartItems(); $result = new LoopResult($cartItems); @@ -81,9 +86,32 @@ class Cart extends BaseLoop return $result; } + $limit = $this->getLimit(); + + $countCartItems = count($cartItems); + + if($limit <= 0 || $limit >= $countCartItems){ + $limit = $countCartItems; + } + + $position = $this->getPosition(); + + if(isset($position)){ + if($position == "first"){ + $limit = 1; + $cartItems = array($cartItems[0]); + }else if($position == "last"){ + $limit = 1; + $cartItems = array(end($cartItems)); + } + + // @TODO : if the position is a number + } + $taxCountry = CountryQuery::create()->findPk(64); // @TODO : make it magic; - foreach ($cartItems as $cartItem) { + for ($i=0; $i<$limit; $i ++) { + $cartItem = $cartItems[$i]; $product = $cartItem->getProduct(); $productSaleElement = $cartItem->getProductSaleElements(); From 77959afefa6eaf2d99ee0050743feaf63e2da0fa Mon Sep 17 00:00:00 2001 From: badsuricate Date: Wed, 23 Oct 2013 15:33:47 +0200 Subject: [PATCH 3/8] Add AJAX on "Add to Cart" buttons --- .../Controller/Front/CartController.php | 7 +++ templates/default/assets/js/script.js | 28 ++++++++++- templates/default/includes/addedToCart.html | 50 +++++++++++++++++++ templates/default/includes/mini-cart.html | 2 +- templates/default/product.html | 4 +- 5 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 templates/default/includes/addedToCart.html diff --git a/core/lib/Thelia/Controller/Front/CartController.php b/core/lib/Thelia/Controller/Front/CartController.php index ae116b40a..afbcfaf7f 100755 --- a/core/lib/Thelia/Controller/Front/CartController.php +++ b/core/lib/Thelia/Controller/Front/CartController.php @@ -61,6 +61,13 @@ class CartController extends BaseFrontController $message = $e->getMessage(); } + // If Ajax Request + if ($this->getRequest()->isXmlHttpRequest()) { + $request = $this->getRequest(); + $request->attributes->set('_view', "includes/mini-cart"); + } + + if ($message) { $cartAdd->setErrorMessage($message); $this->getParserContext()->addForm($cartAdd); diff --git a/templates/default/assets/js/script.js b/templates/default/assets/js/script.js index 7080ffebd..a5d519a8d 100644 --- a/templates/default/assets/js/script.js +++ b/templates/default/assets/js/script.js @@ -204,10 +204,12 @@ var $old_price_container = $(".old-price", $("#product-details")); + var $select_quantity = $(this).find(":selected").attr("data-quantity"); + // Switch Quantity in product page $("select", $(".product-options")).change(function(){ - var $select_quantity = $(this).find(":selected").attr("data-quantity"); + $select_quantity = $(this).find(":selected").attr("data-quantity"); var $old_price = $(this).find(":selected").attr("data-old-price"); var $best_price = $(this).find(":selected").attr("data-price"); @@ -259,7 +261,31 @@ }); } + $(".form-product").submit(function(){ + var url_action = $(this).attr("action"); + var $cartContainer = $(".cart-container"); + $.ajax({type:"POST", data: $(this).serialize(), url:url_action, + success: function(data){ + console.log($(data).html()); + $cartContainer.html($(data).html()); + + $.ajax({url:"ajax/addCartMessage", + success: function(data){ + bootbox.dialog({ + message : data, + buttons : {} + }); + } + }); + }, + error: function(){ + console.log('Error.'); + } + }); + + return false; + }); $('#limit-top').change(function(e){ window.location = $(this).val() diff --git a/templates/default/includes/addedToCart.html b/templates/default/includes/addedToCart.html new file mode 100644 index 000000000..3159f1141 --- /dev/null +++ b/templates/default/includes/addedToCart.html @@ -0,0 +1,50 @@ + {loop type="cart" name="cartloop" position="last"} + + + + + + +
+ {loop name="product_thumbnail" type="image" product=$PRODUCT_ID width="218" height="146" resize_mode="borders" limit="1"} + Product #{$LOOP_COUNT} + {/loop} + +

{$TITLE}

+ {loop type="attribute_combination" name="product_options" product_sale_elements="$PRODUCT_SALE_ELEMENTS_ID"} +

{$ATTRIBUTE_TITLE}

+

{$ATTRIBUTE_AVAILABILITY_TITLE}

+ {/loop} +
+ + {if $IS_PROMO == 1} + {assign "real_price" $PROMO_TAXED_PRICE} +
{currency attr="symbol"} {$PROMO_TAXED_PRICE}
+ {currency attr="symbol"} {$TAXED_PRICE} + {else} + {assign "real_price" $TAXED_PRICE} +
{currency attr="symbol"} {$TAXED_PRICE}
+ {/if} + +
+ {/loop} + + {intl l="View cart"} + {intl l="Continue Shopping"} + + {ifloop rel="product_upsell"} + + {/ifloop} diff --git a/templates/default/includes/mini-cart.html b/templates/default/includes/mini-cart.html index b3b8de597..6c558233b 100644 --- a/templates/default/includes/mini-cart.html +++ b/templates/default/includes/mini-cart.html @@ -1,5 +1,5 @@ {ifloop rel="cartloop"} - - {/for} - - {if $CHILD_COUNT > 0 } -
  • - {$TITLE} ({$CHILD_COUNT}) -
      - {else} -
    • {$TITLE} (0)
    • - {/if} - {assign "previousLevel" $LEVEL} + {* define current ID *} + {loop name="current_cat" type="category" current="yes"} + {assign var="current_category_id" value="{$PARENT}"} {/loop} - {for $i=$previousLevel to 1 step -1} + {* define the function *} + {function menu level=0} + {if $level == 0} +
        + {else} + {if $parent == $current_category_id} +
          + {else} +
            + {/if} + {/if} + + {loop name="cat-parent-$level" type="category" parent=$parent} + + {assign var="product_count" value="0"} + {loop name="product_count" type="product" category="{$ID}"} + {if $LOOP_COUNT == 1} + {assign var="product_count" value="{$LOOP_TOTAL}"} + {/if} + {/loop} + + {if $CHILD_COUNT> 0} +
          • + {menu parent=$ID level=$level+1} +
          • + {else} +
          • {$TITLE} ({$product_count})
          • + {/if} + {/loop} +
          - - {/for} -
        + {/function} + + {menu parent='0'} + From dffb5375f535519000de88dda6a14a0eccef7313 Mon Sep 17 00:00:00 2001 From: badsuricate Date: Wed, 23 Oct 2013 15:45:00 +0200 Subject: [PATCH 7/8] Check if hasbtn is true to display the button "add to cart " --- templates/default/includes/single-product.html | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/templates/default/includes/single-product.html b/templates/default/includes/single-product.html index 76d26de64..bbc9b9d68 100644 --- a/templates/default/includes/single-product.html +++ b/templates/default/includes/single-product.html @@ -56,9 +56,9 @@ {/if} - + {if $hasBtn == true} {form name="thelia.cart.add" } - + {form_hidden_fields form=$form} @@ -115,20 +115,20 @@
        - {if $hasBtn == true} + {if $hasSubmit == true} {else} {intl l="View product"} {/if} - {/if} +
        {/form} - + {/if} \ No newline at end of file From 5515caa0d5f840a798c056c7158fd1b9188a69c8 Mon Sep 17 00:00:00 2001 From: badsuricate Date: Wed, 23 Oct 2013 15:48:06 +0200 Subject: [PATCH 8/8] Internationalizing "No products available in this category" --- templates/default/category.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/default/category.html b/templates/default/category.html index 5ed71ddf4..53a0e74eb 100644 --- a/templates/default/category.html +++ b/templates/default/category.html @@ -33,7 +33,7 @@ {/ifloop} {elseloop rel="product_list"}
        - No products + {intl l="No products available in this category"}
        {/elseloop}