I have a custom template file, rendering of some products and their Add to Cart buttons, which I try to implement manually. When you click the Add to Cart button, the page reloads, and the $ _POST variable, containing some data, adds new products. 'cart_contents_count' also reflects the added item. However, when I go to the cart page, it is empty. See the following code.
global $woocommerce;
if ( isset( $_POST['AddedToCart'] ) ) {
$woocommerce->cart->add_to_cart($_POST['event'], $_POST['qty']);
}
$cart_total = $woocommerce->cart->cart_contents_count;
When I, however, go to the regular default store page (/ shop /) and add the product from there, my shopping cart page indicates that this product has been added. When I NOW go to my own page and add products from this "Add to Cart" button, it works great.
It seems to me that before running the above code, I should check if the Cart session is initialized, and if not, initialize it. Can someone please confirm that I understand correctly and show how to initialize the basket?
source
share