【发布时间】:2020-09-10 06:10:00
【问题描述】:
我正在使用会话创建购物车,但是当相同的商品添加到购物车会话时,我不知道如何更新产品数量?
- 我在其中创建会话的 WhatsApp_add_cart 函数..
- 从隐藏字段中获取数据...
- WhatsApp_Cart_page 我在其中显示用于每个循环的所有会话值。
public function WhatsApp_Cart_button() {
$whatsapp_names = '';
$whatsapp_price = '';
$whatsapp_quantity = '';
$total_price = '';
$Whatsapp_id='';
if ( isset( $_POST['WhatsApp_Cart_btn'] ) ) {
if ( isset( $_POST['whatsapp_cart_nonce_field'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['whatsapp_cart_nonce_field'] ) ), 'whatsapp_cart_field' ) ) {
echo '';
}
if ( isset( $_POST['product_name'] ) ) {
$whatsapp_names = sanitize_text_field( wp_unslash( $_POST ['product_name'] ) );
}
if ( isset( $_POST ['product_price'] ) ) {
$whatsapp_price = sanitize_text_field( wp_unslash( $_POST ['product_price'] ) );
}
if ( isset( $_POST ['quantity'] ) ) {
$whatsapp_quantity = sanitize_text_field( wp_unslash( $_POST ['quantity'] ) );
}
if ( isset( $_POST ['product_id'] ) ) {
$Whatsapp_id = sanitize_text_field( wp_unslash( $_POST ['product_id'] ) );
}
$product_info = array( $Whatsapp_id,$whatsapp_names, $whatsapp_price, $whatsapp_quantity);
$_SESSION['WhatsApp_cart'][] = $product_info;
}
}
public function Whatsapp_cart_function() {
global $product;
if( $product->is_type( 'simple' ) ){
$product_id = $product->get_id();
$product_name = $product->get_title();
// $p_Desp =$product->get_description();.
$final_price = $product->get_price();
$button_cart = get_option( sanitize_text_field( 'WhattsApp_BtnTxt_cart' ) );
?>
<form method="post" action="" enctype="multipart/form-data">
<input type="hidden" name="product_id" value="<?php echo esc_html( $product_id ); ?>">
<input type="hidden" name="product_name" value="<?php echo esc_html( $product_name ); ?>">
<input type="hidden" name="product_price" value="<?php echo esc_html( $final_price ); ?>">
<?PHP
if ( ! empty( $_SESSION['WhatsApp_cart'] ) ) {
foreach ( $_SESSION['WhatsApp_cart'] as $key => $values ) {
p_id=$values[0];
if ( isset( $_GET['product_id'] ) ) {
if($p_id==$_GET['product_id']){
$_SESSION["WhatsApp_cart"][$key][$values[3]] + $_POST["quantity"];
?>
<input type="hidden" name="quantity" value="1" min="1" max="10" step="1" />
<?PHP
}
?>
<?PHP
}
?>
<?php
}
?>
<input type="submit" name="WhatsApp_Cart_btn" style="background:#25d366; color: white;" value="
<?
PHP
echo esc_html( $button_cart ); ?>" id="demo" onclick="Add_items();" />
<?php wp_nonce_field( 'whatsapp_cart_field', 'whatsapp_cart_nonce_field' ); ?>
</form>
public function WhatsApp_cart_page() {
if ( isset( $_GET['index_to_remove'] ) ) {
unset( $_SESSION['WhatsApp_cart'][$_GET['index_to_remove']]);
wc_add_notice('Item removed from cart successfully' , 'success');
}
?>
<table class="" cellspacing="0">
<thead>
<tr>
<th class="product_id"><?php esc_html_e( 'Remove product', 'Woo-WhatsApp' ); ?></th>
<th class="product-name"><?php esc_html_e( 'Product', 'Woo-WhatsApp' ); ?></th>
<th class="product-price"><?php esc_html_e( 'Price', 'Woo-WhatsApp' ); ?></th>
<th class="product-quantity"><?php esc_html_e( 'Quantity', 'Woo-WhatsApp' ); ?></th>
<th class="Total-price"><?php esc_html_e( 'Total', 'Woo-WhatsApp' ); ?></th>
</tr>
</thead>
<?php
global $wp;
if ( ! empty( $_SESSION['WhatsApp_cart'] ) ) {
foreach ( $_SESSION['WhatsApp_cart'] as $key => $values ) {
print_r( $_SESSION['WhatsApp_cart']);
$current_url = home_url( $wp->request );
$remove_url = $current_url . '?index_to_remove=' . $key;
?>
<tr>
<th ><form action="" method="post"><a href="<?php echo esc_url($remove_url); ?> " name="index_to_remove" >X</a></form>
</th>
<?php echo '<th>' . esc_html( $values[1] ) . '</th>'; ?>
<?php echo '<th>' . esc_html( $values[2] ) . '</th>'; ?>
<th><input type="number" name="quantity" value="<?php echo $values[3]?>" min="0" max="10" step="1"> </th>
</tr>
<?php }
?>
</table>
<?php } ?>
【问题讨论】:
-
您可以使用 product_id 作为会话数组中的键,这样您就可以轻松地在购物车中找到产品。使用 key_exist 您可以检查它是否已经在您的购物车中。
-
但是您可以更好地将购物车存储在数据库中,并在 cookie 中为用户提供一个唯一且随机生成的购物车 ID。这样,您可以更好地了解人们添加到购物车的情况,并且只要存在 cookie,用户购物车就会被保存
-
感谢您的建议。但我解决了它与产品 ID 匹配。查看答案
-
我希望您在有人结账时使用数据库中的数据重新计算总价。您将价格保存在发布的会话中,以便访问者可以轻松操纵该值。
标签: php session cart shopping-cart