Skip to content

Commit

Permalink
Add ability to remove products from basket
Browse files Browse the repository at this point in the history
Adds a new method that removes a given row from the cart to begin implementing
task #4. Also updates the default basket component to include a remove button
which will remove that row and update basket total and item count with the aid
of new ocshop-x helper classes, where 'x' is 'total', 'count' or 'row-{ROWID}'.

Finally rejigs the prepareVars method to clean things up, since much of
it is used in the onFoo listeners.
  • Loading branch information
dshoreman committed Dec 4, 2014
1 parent ba8f743 commit 2888027
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 17 deletions.
53 changes: 39 additions & 14 deletions components/Basket.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,30 +92,45 @@ public function onRender()
public function prepareVars($on = 'run')
{
if ($on == 'run') {
$this->paymentPage = $this->page['paymentPage'] = $this->property('paymentPage');
$this->productPage = $this->page['productPage'] = $this->property('productPage');
$this->registerBasketInfo();
$this->registerPages();

$this->basketComponent = $this->page['basketComponent'] = $this->property('basketComponent');;
$this->basketPartial = $this->page['basketPartial'] = $this->property('basketPartial');;
$this->basketItems = $this->page['basketItems'] = Cart::content();
$this->basketCount = $this->page['basketCount'] = Cart::count();
$this->basketTotal = $this->page['basketTotal'] = Cart::total();
}

if ($on == 'render') {
$this->tableClass = $this->page['tableClass'] = $this->propertyOrParam('tableClass');
$this->nameColClass = $this->page['nameColClass'] = $this->property('nameColClass');
$this->qtyColClass = $this->page['qtyColClass'] = $this->property('qtyColClass');
$this->priceColClass = $this->page['priceColClass'] = $this->property('priceColClass');
$this->subtotalColClass = $this->page['subtotalColClass'] = $this->property('subtotalColClass');
$this->totalLabelClass = $this->page['totalLabelClass'] = $this->property('totalLabelClass');
$this->registerClasses();
}

if (Session::has('orderId')) {
$this->orderId = $this->page['orderId'] = Session::get('orderId');
}
}

public function registerBasketInfo()
{
$this->basketCount = $this->page['basketCount'] = Cart::count();
$this->basketItems = $this->page['basketItems'] = Cart::content();
$this->basketTotal = $this->page['basketTotal'] = Cart::total();
}

public function registerPages()
{
$this->paymentPage = $this->page['paymentPage'] = $this->property('paymentPage');
$this->productPage = $this->page['productPage'] = $this->property('productPage');
}

public function registerClasses()
{
$this->tableClass = $this->page['tableClass'] = $this->propertyOrParam('tableClass');
$this->nameColClass = $this->page['nameColClass'] = $this->property('nameColClass');
$this->qtyColClass = $this->page['qtyColClass'] = $this->property('qtyColClass');
$this->priceColClass = $this->page['priceColClass'] = $this->property('priceColClass');
$this->subtotalColClass = $this->page['subtotalColClass'] = $this->property('subtotalColClass');
$this->totalLabelClass = $this->page['totalLabelClass'] = $this->property('totalLabelClass');
}

public function onAddProduct()
{
$id = post('id');
Expand All @@ -125,9 +140,19 @@ public function onAddProduct()

Cart::add($id, $product->title, $quantity, $product->price);

$this->page['basketCount'] = Cart::count();
$this->page['basketItems'] = Cart::content();
$this->page['basketTotal'] = Cart::total();
$this->registerBasketInfo();
}

public function onRemoveProduct()
{
Cart::remove(post('row_id'));

$this->registerBasketInfo();

return [
'total' => $this->basketTotal,
'count' => $this->basketCount,
];
}

public function onCheckout()
Expand Down
15 changes: 12 additions & 3 deletions components/basket/default.htm
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<table class="{{ tableClass }}">
<thead>
<tr>
<th class="col-xs-1">&nbsp;</th>
<th class="{{ nameColClass }}">Product</th>
<th class="{{ qtyColClass }}">Qty.</th>
<th class="{{ priceColClass }}">Price</th>
Expand All @@ -9,18 +10,26 @@
</thead>
<tbody>
{% for item in basketItems %}
<tr>
<tr class="ocshop-row-{{ item.rowid }}">
<td>
<button type="submit" class="btn btn-xs btn-default"
data-request="{{ __SELF__ }}::onRemoveProduct"
data-request-data="row_id: '{{ item.rowid }}'"
data-request-success="$('.ocshop-row-{{ item.rowid }}').remove(); $('.ocshop-total').text(data.total); $('.ocshop-count').text(data.count);">
<span class="glyphicon glyphicon-remove"></span>
</button>
</td>
<td>{{ item.name }}</td>
<td>{{ item.qty }}</td>
<td>{{ item.price }}</td>
<td>{{ item.subtotal }}</td>
</tr>
{% endfor %}
<tr>
<td colspan="3">
<td colspan="4">
<strong class="{{ totalLabelClass }}">Total:</strong>
</td>
<td>&pound;{{ basketTotal }}</td>
<td>&pound;<span class="ocshop-total">{{ basketTotal }}</span></td>
</tr>
</tbody>
</table>

0 comments on commit 2888027

Please sign in to comment.