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.
Related
First post here so I'll try to make it as good as possible.
For a WooCommerce / WordPress project, I'm trying to achieve the following. When the cart is empty, there should be a button displayed with the text "Return to store".
Now the button isn't the problem, I've added this with Elementor, now usually I use the JetEngine Dynamic Visibility option to show/hide things.
I've also done this with stock status. There I used the _stock option to only display when there are 5 or more in stock left.
So I'm trying something similar with the cart, whenever there are 1 or more items in the cart, this button needs to be hidden because whenever someone has 1 item in their cart we want them to proceed to the checkout and not return to the shop, because the average product bought per order is 1 for this particular shop.
But the one thing I can figure out is, what key do I need to use to check items in the cart?
I've been trying several options I found online but none seem to work.
Does anyone know how to do this?
Thanks!
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
I need to remove td.actions from a WooCommerce cart page via PHP. The table contains a Coupon field, and a Update Basket button. I searched around, but only found ways to hide using CSS. Any help is more than appreciated.
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
I know that setCouponCode is catch inside __call in Varien_Object but I can't figure out where it is defined.
I need it because I want to show the coupon code even if the discount equal zero.
I believed it's done inside this function.
So if anybody know where the function is defined or where I can modify the code to get the coupon code displayed all the time, please let me know.
Most text editors have a 'search in files/folders' option. Open the source folder and search for function __setCouponCode.
Ok I figured it out.
the SetCouponCode is setting the value for coupon_code in the magic function.
Related to this, did anybody notice that the quote is not being deleted properly? I say it is related to this because the coupon code variable keeps the value [if any] after you delete a product from cart.
Try:
1. Set some Shopping Price Cart Rule to a product and make it display a banner on the header section for example.
2. Add product to checkout/cart in order to trigger the rule and show the banner on the Cart.
3. Delete the product from cart and you will see the banner still showing on the header section.
Note. If you have another product in cart this will not work because when you delete the one that triggered the rule&banner the Coupon Code will get replaced with the one that belongs to this other product in cart.
SO this bug only works if only the product that triggers the rule&banner is in cart.
If anybody has a fix on this or can replicate these conditions: I'd love to have a conversation about magento's deficiency to make a proper product delete from cart - which implies a proper quote refresh.
Reference:
- app/code/core/Mage/Sales/Model/Quote.php -> public function removeItem($itemId)
And yes, magento core issue.