Friends, I appreciate if anyone can analyze.
I'm having trouble displaying the total price in the shopping cart.
my index.ctp looks like this:
<?php foreach((array) $this->request->getSession()->read('cart') as $index=>$cart): ?>
<?= $cart->has('product') ? $this->Html->link($cart->product->name, ['controller' => 'Products', 'action' => '/', $cart->product->id]) : '' ?>
<?php echo number_format($cart->product->price, 2, ',', '') ?>
Quantity
<?php echo $this->Form->create('Orders',array('id'=>'add-form','url'=>array('controller'=>'Orders','action'=>'update', $index)));?>
<?php echo $this->Form->control('quantity', array('type'=>'number', 'label'=>false,'min'=> 1,'size' => 2, 'maxlenght' => 2,'class'=>'form-control form-control-sm mt-1','required' => 'true', 'value'=>$cart->quantity));?>
Subtotal
<?php $sub = ($cart->product->price * $cart->quantity);
echo number_format($sub, 2, ',', '');
?>
<?php endforeach; ?>
</div>
Total: <?php
$sum = 0;
foreach(array($sub) as $total){ $sum+= $total; }
echo $sum;
?>
I don't know if the best way to do this is in index.ctp, but here it displays the subtotal of the last element. It does not add up the subtotal value of all products.
When I do:
<?php
$sum = 0;
foreach(array($sub) as $total)
{
debug($sub);
$sum+= $total;
}
echo $sum;
?>
Return:
(line 127) (float) 35
This is the subtotal value of the last element in the list. I need to get all the elements of the subtotal to be able to add. I appreciate any comments!