将产品添加到购物车后,用户刷新页面,然后如何显示添加到购物车中的数据 [英] 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

查看:90
本文介绍了将产品添加到购物车后,用户刷新页面,然后如何显示添加到购物车中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CodeIgniter购物车。我显示的记录是

I am using the CodeIgniter shopping cart. I am displaying records like

第1行

AFGFD ANAND
trdsd@gmail.com

ActivityName   | duration        | price   | Action
cricket        | Select duration | 100     | Add to cart

第2行

POIUY ANAND
mnbsd@gmail.com

ActivityName   | duration        | price   | Action
cricket        | Select duration | 100     | Add to cart
Badminton      | Select duration | 200     | Add to cart

注意
持续时间是一个选择下拉列表,其中低于

Note duration is a select dropdown which is below

<select name="memberDuration"  class="form-control dropdownDuration"> 
<option selected disabled >Select duration</option>  
<option value="12m">1 Year</option>
<option value="6m">6 months</option>
</select>

下面是正在工作的最终代码。添加到购物车也没有问题。用户最多只能添加一个产品。

Below is the final code which is working for. There is no issue with add to cart as well. The user can't add the same product more than one.

     foreach ($SActivity as $sec_data) {
      $counter = 0; // COUNTER FOR KNOWING POSITION IN ACTIVITIES LOOP              $num_activities = count($sec_data); // TOTAL NUMBER OF ACTIVITIES FOR THIS USER 

foreach ($sec_data as $row) { $counter++; // ADD +1 TO COUNTER FOR EACH
    ACTIVITY // ONLY PRINT OUT THE NAME AND TABLE HEADER IF FIRST ROW if($counter == 1){ ?>

    <h2>
      <?php echo $row->first_name;?>
      <?php echo $row->last_name;?>
    </h2>
    <p>
      <?php echo $row->email;?>
    </p>
    <table>
      <thead>
        <tr>
          <th>ActivityName</th>
          <th>duration</th>
          <th>price</th>
          <th>Action</th>
        </tr>
      </thead>
      <tbody>

        <?php } ?>

        <tr>
          <td>
            <?php echo $row->ActivityName;?>
          </td>
          <td>
            <select name="memberDuration" class="form-control dropdownDuration">
              <option selected disabled>Select duration</option>
              <option value="12m">1 Year</option>
              <option value="6m">6 months</option>
            </select>

          </td>
          <td>
            <div class="calActivitylPrice"></div>
          </td>

          <td>
            <div class="display_table">
              <?php echo $secActivity_Addtocart_active;?>
            </div>
          </td>

        </tr>

        <?php 
                // IF THIS IS THE LAST ACTIVITY OF THIS USER IN THE LOOP: PRINT TABLE END TAG
                if($counter == $num_activities){
                    echo '</tbody></table>';
                }
            } 
        }

添加产品的示例。

POIUY ANAND
mnbsd@gmail.com

ActivityName   | duration | price   | Action
cricket        | 12m      | 100     | remove
Badminton      | 6m       | 200     | remove

到目前为止没有问题。

现在我注意到我在购物车中添加了一个产品,然后刷新页面,然后选择下拉列表,并且价格未显示。

Now what I notice that I added a product in the cart and I refresh the page then my select dropdown and the price is not displaying.

我的意思是我添加了一个期限为 1年的产品,并且根据下拉列表,我使用ajax显示价格。

I mean I added a product with 1 year duration and according to the dropdown, I am displaying the price using ajax.

产品,然后刷新页面,然后我会得到

After adding the product in the cart and refresh the page then I am getting like

POIUY ANAND
mnbsd@gmail.com

ActivityName   | duration        | price   | Action
cricket        | Select duration |         | remove
Badminton      | Select duration |         | remove

所以我试图找到在将产品添加到购物车后用户刷新的解决方案

So I am trying to find the solution that after adding the product in the cart and the user refreshes the page then how can I display the data.

我找到了一些解决方案,它正在工作。但是问题是我的选择下拉列表显示在所有选择选项中,而价格未显示在正确的活动中。

I found some solution and it's working. but the issue is my select dropdown is display in the all the select option and price is not displaying in the in the correct activity.

我正在得到类似的输出。我从第一行选择下拉列表中选择1年,它显示在所有行中

I am getting the output like. I choose 1 year from the first-row select dropdown and it displaying in the all the row

Row 1

AFGFD ANAND
trdsd@gmail.com

ActivityName   | duration  | price   | Action
cricket        | 12m       | 100     | remove

Row 2

POIUY ANAND
mnbsd@gmail.com

ActivityName   | duration  | price   | Action
cricket        | 12m       | 200     | remove
Badminton      | 12m       |         | remove



 <tr>
      <td>
        <?php echo $row->ActivityName;?>
      </td>
      <td>
    <?php 
     $product['options']['duration']=0;
    if (in_array($activityNO, array_column($this->cart->contents(), 'id'))){
            foreach ($this->cart->contents() as $product) { 
               //break;
             } }?>
                  <select name="memberDuration"  class="form-control dropdownDuration"> 
        <option selected disabled >Select duration</option>  
                <option value="12m" <?php if($product['options']['duration'] == "12m"){ echo 'selected="selected"';} ?> >1 Year</option>
                <option value="6m" <?php if($product['options']['duration'] == "6m"){ echo 'selected="selected"';} ?>>6 months</option>
              </select>


      </td>
      <td>
        <div class="calActivitylPrice">

            <?php if (in_array($activityNO, array_column($this->cart->contents(), 'id'))){
            foreach ($this->cart->contents() as $product)  {
              if ($product['id'] == $activityNO) { 
                //print_r($product['price']);
                $priceAct = $product['price']; 
              }
              //
            }
                print_r($priceAct);
          }
      ?>
        </div>
      </td>
      <td>
        <div class="display_table">
          <?php echo $Addtocart_active;?>
        </div>
      </td>
    </tr>

$ this-> cart-> contents()的值

Value of $this->cart->contents()

 Array
    (
        [dc54c1ce61893fa725cf87c9e20b4c78] => Array
            (
                [id] => 1
                [name] => cricket
                [qty] => 1
                [price] => 100
                [options] => Array
                    (
                        [duration => 12m
                    )

                [rowid] => dc54c1ce61893fa725cf87c9e20b4c78
            )

        [e5966e762beda1762e461f27f1dc3ef4] => Array
            (
                [id] => 2
                [name] => Badminton 
                [qty] => 1
                [price] => 200
                [options] => Array
                    (
                        [duration] => 6m
                    )

                [rowid] => e5966e762beda1762e461f27f1dc3ef4
            )

    )


推荐答案

根据更新进行更新

在此区块

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

您在添加的购物车中检查了$ activityNO,这可能是正确的,但是在foreach循环中,您没有从购物车内容中获得正确的$ activityNO值。

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.

顺便说一句,您的解决方案不好,您使用了很多foreach。

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.

注意:上面的代码只是想法,它不能100%起作用。

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. 使用会话存储购物车信息


    • 在用户添加到购物车时将购物车存储到用户会话

    • 如果要阅读,请从Session加载购物车(从要显示的页面)

    • 通过这种方式,购物车数据仅在会话范围内可用


  • 当用户添加到购物车时将购物车存储到数据库中

  • 如果要阅读,请从数据库加载购物车(在您要显示的页面上)

  • 更复杂的解决方案,但它是永久的购物车,可以为一个用户在不同的浏览器之间共享。

这篇关于将产品添加到购物车后,用户刷新页面,然后如何显示添加到购物车中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆