Is it possible to have two seperate Carts on WooCommerce? - php

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.

Related

WooCommerce Meta-key for products in cart

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!

Woocommerce -- Adding Products with Javascript/PHP

I have a custom form that users make various selections before being presented with products they can buy. I need to add some logic that if an input has been selected when the user adds a product to the cart, it will also add an additional product to the cart.
The input is 3 radio check boxes. Somehow I need to hook into the add to cart click and check the page to see if one of the radios has been selected and then add the appropriate product but I'm not sure where to start.
I know I'll need to have an action that runs something like:
WC()->cart->add_to_cart( 'product_id', 'quantity');
But I'm not sure how to hook into the initial add to cart click and how to check for the radios on the page.
Thank you for your help.
Not a lot of information to go off of here, but here is a few useful WooCommerce hooks to give you a starting point/ order of operations. When overriding any native action in WordPress or its plugins. You should use the hooks if available.
Add the radio inputs to the single product before the add to cart is added (hook):
add_action("woocommerce_before_add_to_cart_button", "your_input_render_function_here");
Add the new input data from the radio buttons to the cart item (filter):
add_filter("woocommerce_add_cart_item_data", "your_filter_function_to_capture_data_here");
Capture the data and render in cart (filter):
add_filter("woocommerce_get_item_data", "your_cart_render_function");
Finally, save the newly aquired data to the order (hook):
add_action("woocommerce_checkout_create_order_line_item", "your_function_to_add_to_order")
This is one of many directions you can take, depending on your requirements. I recommend giving it a shot, and if you still have issues return with an example of your code.

How to stop Woocommerce Add to Cart redirecting to Product Pages

Because of a way a customer wanted their products displayed I've used woocommerce shortcode to display products. When Add to Cart is clicked, I didn't want the customer to leave the page but stay on that page until they'd finished ordering.
I have used this code in my Child Theme functions.php file and it successfully stopped the redirection and kept the customer on the page they were on.
//Prevent shortcode [product_page] redirecting to the actual product's page after add to cart
add_filter( 'woocommerce_add_to_cart_redirect', 'wp_get_referer' );
BUT after I added a plugin that gave minimum or maximum quantities and the quantity is below the minimum the Add to Cart redirects to the product page again whenever a product is below the minimum.
I'm not great on php and am not sure what I need to put in the functions.php file to stop this happening.
Any suggestions much appreciated.
Maybe other plugins are adding filter to the cart redirect as well. You could try to set your own filter's priority to a big number so that it's executed after the other filters.
add_filter('','',50); //or even a bigger number

Woocommerce doesn't redirect to checkout page

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

PHP:update a stock with values in the cart

I am currently working on an eCommerce website, but i need some advice regarding the stock management.
I want the shopping cart to work as follow:
CostumerA add a productO to cart with a quantity of '1'.
CostumerB tries to add the same product to their cart but with no success.
CostumerA logs out or their session expires.
CostumerB tries to add productO again and is allowed to do so since the product hasn't been added to CustomerA's cart ...
i came up with 2 solutions but neither is reliable from my point of view:
add a field 'champ' to Table product that initially have value of product.quantity but change with cart movements.
add table Cart and update it with cart movements.
as u can see both solution require lot of work.Is there a better way to implement my shopping cart.
I think a better way might be to only update if the user checks out and check stock on the product listing pages.
The downside being the following scenario:
user A adds an item to their cart
user B adds the same item to their cart
user A checks out and the item is now out of stock
user B checks out, but item is out of stock
You'll need to check stock at every point in this process and inform your users what's going on.
Tying up inventory by having a user placing it in their cart is a bad idea. If user A adds all of the available stock of one item to their cart, no one else can buy it until they either log out, or you log them out with some sort of timeout.

Categories