Woocommerce doesn't redirect to checkout page - php

its me again, i will give you guys a quick rundown of what i'm doing (i'm passing the cart info from one woocommerce store to another woocommerce store) i was finally able to send the product_id and quantity from the store A to the store B and in this one i'm using the woocommerce WC()->cart functions to create a copy of the cart in this last store so the client cant process his purchase, and its working well.
Here is the code.
$incoming_cart=unserialize(base64_decode($_POST["p_cart_t"]));
foreach ( $incoming_cart as $cart_item_key => $cart_item ) {
$product_id=$cart_item['product_id'];
$quantity_pro=$cart_item['quantity'];
print_r($product_id);
print_r($quantity_pro);
WC()->cart->empty_cart();
WC()->cart->add_to_cart($product_id,$quantity_pro);
}
My problem is, when i press checkout the system sends me to the cart page again like just refresh and the cart info gets lost.
I have to say that i'm passing the info of the cart that comes from store A to the cart-empty.php file from the store B cause since im switching between stores there is no cart in the store B and it says that the cart is empty, so what i did was paste the cart.php code into cart-empty.php and its working, the file is catching the info and mirroring the cart but i have this problem with the checkout.
¿do someone may know what this is happening?
¿what can i do to fix this?

I can sense a logic of execution problem here.
One question is where and when are you firing that code you provided. I would suggest, fire it on woocommerce_loaded hook. Then after adding everything to cart, redirect to cart page.
WooCommerce has an option that after adding a product to cart, you will be redirected to cart. I suggest you look for that.
Also, I can't help myself noticing that your code is emptying the cart in every loop. I would transfer WC()->cart->empty_cart(); outside foreach loop. Maybe after checking $incoming_cart is non empty, empty your cart, then run a loop to add all the products from $incoming_cart. Then redirect the page to the cart page. By this time the cart is not empty.
With this, you don't have to edit files from WooCommerce. Forget editing cart-empty.php

Related

Is it possible to have two seperate Carts on WooCommerce?

I'm trying to create a custom WooCommerce Cart. It would have the same capabilities as the real WooCommerce Cart. The only difference would be that in each cart, only specific types of products can go in.
Let's say I have a session variable that tells me which products to show. And based on that session variable, I add products to a specific cart. And you can always change that session variable but keep the products for each cart until the session ends.
After that, I go to the checkout and it will list items from the specific cart based on that session variable once again.
Is something like that possible, specifically having two separate WooCommerce carts, the default one and a new custom one?
I tried this:
if (! isset(WC()->cart_custom) || '' === WC()->cart_custom) {
WC()->cart_custom = new WC_Cart();
}
but every time I add products to the default cart and open the default cart page, the default cart is emptied. I don't know why.
I hope my question makes some sense.

Woocommerce - Cart page - when cart gets renewed with new quantities, I want to be able to pass total

Ok - trying to slowly work my way through this - bear with me! Will try to describe what I'm doing here.
Using Woocommerce with theme called Flatsome - but this problem seems to relate to how Woocommerce updates the cart on the cart page.
So I am trying to bend Woocommerce to suit - so when client gets to Cart page - they do not move onto Checkout - instead I am giving them a Button to Request a Quotation - button when click opens a modal with a GravityForm in it.
I've been able to get the product list, quantities, price and SKU passed fine into the form ok using a 3rd party plugin ....
but boy oh boy - can I easily pass through the most recent TOTAL from woocommerce??
Not as easy as I'd hoped!
I had originally written this function into my functions.php
// Send the page url to parameter called 'gdcarttotal' for use in gravity forms
add_filter("gform_field_value_gdcarttotal", "populate_gd_carttotal");
function populate_gd_carttotal(){
global $woocommerce;
$totalamount = $woocommerce->cart->cart_contents_total;
return $totalamount;
}
And yes that seemed to pass through the Total of the Cart ok.
The problem arises if the quantities are updated whilst on that Cart page.
So if I update the quantity of a product - the totals etc all update fine on the Woocommerce cart page total BUT if I then click the launch Quotation form - the value for the Cart Total remains the value of the original total when user landed on that page. So it takes a full page refresh on the cart page to flush it and update it to the correct value.
Someone mentioned to me, Ajax which is beyond my capabilities.
Really just reaching out - and asking what is best way - or how could I write a function whilst on the cart page that will always give me the most updated TOTAL to pass through into a field on a form.
I've got a couple of screenshots but since my first time on here... I'm not sure if I can post or not. Hopefully this description tells the story.
Basically looking to know - whilst on the Woocommerce Cart page, how to pass the most recent TOTAL calculation to a form or page etc...
Thanks guys!!
Gareth
Your base issue is with the language you're using to solve the problem. You've written PHP, which is server-side (i.e., not real-time).
The WooCommerce cart is AJAX, a hybrid of JavaScript and XML, and it's processed client-side (i.e., in the browser) in real-time. That's how you are able to update the quantity and cart totals without a full page reload, and it's how the website is able to see how many items you have in the cart without reloading the page. Because of this, your solution needs to be client-side using JavaScript, I believe.
I'm not a pro developer, so I don't have the exact code to give you, but hopefully this gets you on the right track. You can also read more about AJAX here: https://www.w3schools.com/whatis/whatis_ajax.asp

Woocommerce cart is gone when going to another page

I have been using WC()->cart->add_to_cart($produkt->id); to add product to woocommerce programatically in php. And it seems to work, as when I dump the cart afterwards, the products are there. The problem is that when I refresh the page or go to another page, the cart is simply empty again! Is it a way to make my added products "stick"? I am a newcomer to using Woocommerce like this, so know I may not be doing it right? Guess there should be a method for saving the cart, but cannot find it.

Confirmation message before removing a product from cart

I just notice that in WooCommerce if customer removes a product from cart, there is no warning message or confirmation before deleting it.
Is there any ways to make a confirmation before removing a product from cart?
Thanks.
If you look at WC_Cart remove_cart_item() method source code here you will see on line 1015 that you can only use woocommerce_remove_cart_item action hook that is triggered before an item is removed from cart.
As removing an item is ajax driven in cart page, to do what you are expecting is certainly possible, but something really complicated.
Remember that on cart page, you already got a notice when you remove a cart items, that allow you to restore it… So adding a confirmation notice, is not really viable and even disturbing for customer experience.

Ability to pass variables through add to cart url and query them at time of checkout?

I've been having difficulty trying to accomplish the passing of variables that a user inputs from within the product page into the cart so that when the user checks out I can utilize that data.
I'm confused on whether I should be using custom fields / variables / or some other means to make this work. I'll try to describe the exact workflow before so my use case is clear.
1) User lands on a specific simple product page
2) Before the user can click the add to cart button, they must validate their account credentials from my auth server that is decoupled from wordpress.
3) Once the details are validated the user can click add to cart and the product in the cart would have the user details attached to it in some form for querying at checkout.
4) * At this point user interaction is done
5) * At time of checkout I've hooked add_action( 'woocommerce_order_status_completed', 'order_system' ); to inspect the cart and the items within the cart
6) * *I need to retrieve the variables that were passed along with the item **
7) ** My checkout hook then POSTs the referenced variables to an API **
Any help is greatly appreciated, I've struggled with this for some time now.
This guide I wrote is probably perfect for what you need. This plugin is different, but it should have all of the hooks and info you need to modify it for your own needs.
I show all of the steps on adding a custom form and adding the values the user enters to the cart meta so you can query it out during the checkout process.
function is_xaik_wc_in_cart() {
global $woocommerce;
if (sizeof($woocommerce->cart->get_cart())>0) :
foreach ($woocommerce->cart->get_cart() as $item_id => $values) :
if (isset($values['_xatik_wc_data'])) :
return TRUE;
endif;
endforeach;
endif;
return FALSE;
}
Once you know it's in the cart, you can just query it doing another foreach loop on the cart. Or just modify the function to do it all at once.
All of these variables should stick with the cart meta information.
Here is a gist example that should work.
On a shopping cart your main objective is to make the shopping as easy as possible, so you should NOT use java script variables or custom hidden fields (i am assuming you were talking about these), if you do so those variables will be lost if the user exit of your website or he just hit back after adding something to the car.
Try using cookies or sessions.
You can find information about cookies Here

Categories