简体   繁体   English

将产品添加到购物车后,用户刷新页面,然后如何显示添加到购物车中的数据

[英]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

I am using the CodeIgniter shopping cart. 我正在使用CodeIgniter购物车。 I am displaying records like 我正在显示类似的记录

Row 1 第1行

AFGFD ANAND
trdsd@gmail.com

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

Row 2 第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>';
                }
            } 
        }

example of the added product. 添加产品的示例。

POIUY ANAND
mnbsd@gmail.com

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

There is no issue till now. 到现在为止没有问题。

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. 现在,我注意到我在购物车中添加了产品,刷新页面,然后选择下拉列表,并且价格未显示。

I mean I added a product with 1 year duration and according to the dropdown, I am displaying the price using ajax. 我的意思是我添加了一个期限为1 year的产品,并且根据下拉列表,我使用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. 但问题是我的选择下拉列表显示在所有选择选项中,而价格未显示在正确的活动中。

I am getting the output like. 我得到的输出像。 I choose 1 year from the first-row select dropdown and it displaying in the all the row 我从第一行选择下拉列表中选择1年,它显示在所有行中

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>

Value of $this->cart->contents() $ 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
            )

    )

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. 您在添加的购物车中检查了$ activityNO,这可能是正确的,但是在foreach循环中,您没有从购物车内容中获得正确的$ activityNO值。

And by the way, your solution is not good, you are using many foreach. 而且,您的解决方案不好,您正在使用许多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%. 注意:上面的代码仅是个主意,并非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. 更复杂的解决方案是永久性的购物车,可以为一个用户在不同的浏览器之间共享。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 添加到购物车并侦听“ checkout_cart_product_add_after”事件时,如何获取产品数据 - how do I get product data when adding to cart and listening for the “checkout_cart_product_add_after” event 刷新页面后产品自动加入购物车 - After refreshing the page product adding automatically in cart 如何将购物车商品产品重定向到Woocommerce中的特定页面? - How can i redirect cart item product to specific page in Woocommerce? 在购物车页面中显示产品信息 - display product information in the cart page Woocommerce 添加到购物车钩子(产品成功添加到购物车后) - Woocommerce added to cart hook (after the product was successfully added to cart) 如果我单击已在购物车中的产品的“添加到购物车”按钮,如何重定向到购物车页面 - How do I redirect to the cart page if I click on the 'Add to cart' button for a product that's already in cart 如何在php的单独选项卡中显示购物车页面? - How can i display cart page in separate tab in php? 如何在购物车页面上显示所有 woocommerce 产品? - How can I display all woocommerce products on the cart page? 刷新 WooCommerce 产品页面添加到购物车 - Refresh WooCommerce product page on added to cart 如何在空购物车页面中保存访客在woocommerce中添加到购物车的第一个商品的产品ID? - How save product id of 1st item added to cart in woocommerce for a visitor in empty cart page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM