after adding the product in the cart and the user refreshes the page then how can I display the data which is added in the cart

岁酱吖の 提交于 2019-12-02 09:36:10

Update according to update

In this block

            foreach ($this->cart->contents() as $product) { 

You check $activityNO in the added carts and it is possibly true, but in foreach loop, you didn't get the correct $activityNO value from cart content.

And by the way, your solution is not good, you are using many foreach.

To optimize it, only loop one time and prepare your date and then render.

<?php
$productsInCart = [];
// Loop and prepare data similar to below
foreach ($this->cart->contents() as $cart) {
    $productsInCart[$cart['id']] = $cart;
}
?>

<?php 
foreach ($sec_data as $row) {
    $activityNo = $row->activityNo; // I am not sure about this
?>

<h2>
  <?php echo $row->first_name;?>
  <?php echo $row->last_name;?>
</h2>

<select name="memberDuration" class="form-control dropdownDuration"> 
    <option <?php if (empty($productsInCart[$activityNo])) { echo 'selected="selected"';} ?> disabled >Select duration</option>  
    <option value="12m" <?php if($productsInCart[$activityNo]['options']['duration'] == "12m"){ echo 'selected="selected"';} ?> >1 Year</option>
    <option value="6m" <?php if($productsInCart[$activityNo]['options']['duration'] == "6m"){ echo 'selected="selected"';} ?>>6 months</option>
</select>

<?php
} // END foreach
?>

And apply the same strategy for below price.

Note: Above code is only the idea, it is not working 100%.


To do that, you need to user one of suggestion solutions below, depends on you requirement.

  1. Using Session to store cart info
    • Store cart to user Session when user add to cart
    • Load cart from Session if you want to read it (From the page you want to show)
    • Using this way, cart data is only available on Session scope
  2. Using database to store cart info
    • Store cart to your Database when user add to cart
    • Load cart from Database if you want to read it (From the page you want to show)
    • More complicated solution, but is is perpetual cart and can share among different browser for one user.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!