Hello Couchers,
This may seem like an unusual but possible request. I have setup a couch shopping cart. The cart should and can only contain a single product. What I have done so far:
1. User adds items to cart and its added fine the first time.
2. When the user decides to add another product, I want to check if the cart is empty and if it is not, I want to empty the cart before adding the current product to it.
Unfortunately, I am unable to pull this off with pp_empty_cart and pp_refresh_cart. It rather deletes the existing cart and does not add the current product to it.
//MY CURRENT CODE
CURRENT SOLUTION USING GOOGLE GEMINI:
This may seem like an unusual but possible request. I have setup a couch shopping cart. The cart should and can only contain a single product. What I have done so far:
1. User adds items to cart and its added fine the first time.
2. When the user decides to add another product, I want to check if the cart is empty and if it is not, I want to empty the cart before adding the current product to it.
Unfortunately, I am unable to pull this off with pp_empty_cart and pp_refresh_cart. It rather deletes the existing cart and does not add the current product to it.
//MY CURRENT CODE
- Code: Select all
...
async addToCart(form) {
const testCookie = Cookies.get('test');
if(testCookie){
<cms:pp_empty_cart />
<cms:pp_refresh_cart />
Cookies.remove('test')
}
const fd = new FormData(form);
const action = form.action;
try {
const resp = await fetch(action, {
method: "POST",
body: fd,
redirect: 'follow',
headers: {
"X-Requested-By": "alveohive",
}
})
const data = await resp.json()
console.log('RESERAVTION DATA', data);
if(!resp.ok){
throw new Error(data || 'An error occured whiles submitting your space');
}
if(data.status){
const currentUrl = new URL("<cms:show current_space_link />")
Cookies.set('test', "<cms:show current_space_id />")
window.location = "<cms:add_querystring current_space_link 'showcart=1' />";
}
} catch(error){
console.log('RESERVATION ERROR', error.message)
}
},
...
CURRENT SOLUTION USING GOOGLE GEMINI:
- Code: Select all
<?php
// Ensure session is started before accessing $_SESSION.
// CouchCMS usually handles this, but it's a safeguard if this block runs extremely early.
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
// Check if the current request is an 'add item' action.
if (isset($_GET['kcart_action']) && (int)$_GET['kcart_action'] === 1) {
// Check if the session cart already contains any items.
// We directly access $_SESSION['kcart'] as the $CART global object might not be
// fully populated with session data at this very early stage.
if (isset($_SESSION['kcart']) && !empty($_SESSION['kcart']['items'])) {
// Empty the 'items' array within the session cart.
// This effectively removes all existing products from the cart's stored state.
$_SESSION['kcart']['items'] = array();
// It's crucial to also reset other related cart totals in the session
// to ensure a completely clean slate for the new single item.
$_SESSION['kcart']['count_items'] = 0;
$_SESSION['kcart']['count_unique_items'] = 0;
$_SESSION['kcart']['count_shippable_items'] = 0;
$_SESSION['kcart']['sub_total'] = 0;
$_SESSION['kcart']['discount'] = 0;
$_SESSION['kcart']['sub_total_discounted'] = 0;
$_SESSION['kcart']['taxes'] = 0;
$_SESSION['kcart']['shipping'] = 0;
$_SESSION['kcart']['total'] = 0;
$_SESSION['kcart']['custom_vars'] = array();
}
}
?>
<?php require_once( 'xxxxx/cms.php' ); ?>
<cms:no_cache />