Imagine invoice. It has products. Each product should have computed property 'total' (quantity * price). Total of invoice is also computed property (sum of all products totals). How could I solve this? I tried to create 'products' array attribute in store state, and push there separated product stores. But in strict mode my browser crashed, so probably this isn't a right way :)
Related
I am setting up a site (WP) which will essentially sell one type of product and integrating a mobile app (ionic).
Multiple vendors can sell (WC Vendors) the variable product on the site at different quantities (100 litres, 200 litres etc).
I need to be able to make a GET request to return each vendors variable product for that quantity.
e.g. Customer selects they want 300 litres in the mobile app, I then use the API to return all variable products for that quantity (300 litres). So in the end, I have a response which will contain something like below;
Vendor: Vendor 1, Product: Product Name, Quantity: 300 litres, Price: £200
Is this possible as the WC API stands now?
For anyone else struggling on this, I finally got it to work by using the tags as suggested above and to query by tags using this;
https://example.com/wp-json/wc/v3/products?tag=20?consumer_key=ck_xxxxxxxxxxxxxxxxx&consumer_secret=cs_2xxxxxxxxxxxxx
I think you need to rephrase this a little bit, But From what I can get. You have a Vendor and a Product that has a Quantity of 300 L which in the first place I'd recommend you Should avoid, Rather just use Liter , Now what I'd Recommend is Tag's.
You'd have your Vendors and products under Categories.
And you can Set Tags on Products that you'd like to have what you require.
Then When someone Selects 300L , 200L It will pull every Product with the Tag 200L And you'll be able to display them.
Official Document
/wp-json/wc/v3/products/tags/<id>
They have:
id integer Unique identifier for the resource. name string Tag name.
slug string An alphanumeric identifier for the resource unique to its type.
description string HTML description of the resource.
count integer Number of published products for the resource.
This is the sale price that is displayed (excluding the VAT) in addition to the quantity and the total price and the VAT, for each item in the order items table.
I would like to display the regular price and the discount of the product item.
I checked in the WC_Order_Item_Product, but I could not find the information and I do not want to retrieve it from the related product attributes because that information can change.
I want the information that was active when the order was created.
I searched on this site and and googled a lot but could find any information. Is it even possible ?
I need to limit user's orders in WooCommerce in a way that each user would be able to order some specific products for a limited amount per day. this way the limitation is "per user"/"per product"/"per day" !
for example each user could buy only 10 items of a product each day, and the next day he would be able to buy 10 more.
I have checked almost all plugins related to limiting orders but none of them had all these conditions. I also read some sample codes here like this one. but since I'm new to php and wordpress coding I couldn't figure out how to change it to my preference.
I appreciate any help
I would start with adding a function to the woocommerce_add_to_cart hook.This is a basically a hook that get called when a new product is added to the cart. Then I will work with the users meta where I will check if the user has bought this product already when this has happened. If there is no such data I will then add it to the users meta.
Mind that you have two scenarios here - first, the product is just added to the cart and not purchased, the second one is when the product is purchased. This is important because you will have also the case when the user deletes product from the cart and you will need also a hook there so you can update the user meta. Here you can use woocommerce_remove_cart_item and woocommerce_after_cart_item_quantity_update (check for references here )
And finally you will need to hook on order completion -> check those hooks.
But still, one of the most important parts is how you are going to build the array for the user meta. I would suggest creating an associative array with key value -> the product id. As value of the array you can have another array with date of purchase and products quantity. Something like this:
$users_products = [
product_id1 => [
'date' => timestamp,
'quantity' => 3
],
product_id2 => [
'date' => timestamp,
'quantity' => 3
]
];
Where product_id1 and product_id2 are actual products ids. This way it will be easier for you to loop through the user meta and check if the current product is in the list. Also to avoid making this user meta data too big check if the date of each item is not outdated and if so - delete(unset) this element from the array and update it the user meta with the refreshed data.
There is much more that you will need to code but I think this will be a good start for you.
I have a digital product which is described by a quantity and a price, but which also needs 3 more numbers to completely specify it (Unix dates, etc). Problem: how do I get these numbers into the cart?
As far as I can see, there are 2 possible ways to handle this:
A product variation
A product custom field
It looks like variations can only handle discrete values with a limited range (ie. red/yellow/green, S/M/L, etc), and can't handle general integers, like dates. That leaves custom fields. I think I'm right in saying that custom fields are ordinary meta data on the product post page, so I can handle them with get_post_meta and update_post_meta.
So, if I go for custom fields, then I would update the product page field during ordering, and then I would read back the field during checkout, when the WC_Order is created, and add the field to the new order. However, this won't work. I can't change metadata on the product page, because the product is global to all customers, and this operation would interfere with other customers. In other words, you can't store order-specific information in a product, so neither of these options would work.
So, how do I store temporary product metadata and pass it between the ordering and checkout phases (ie. between WC_Cart and WC_Order)?
One option would be to store it as user metadata (or as session data?), but there's got to be a better way - any ideas?
It turns out to be easy to do this with session data. When you're adding an item to the cart (see the source for add_to_cart_action) you create a session variable, containing all your additional meta data:
WC()->session->set(
'my_session_var_name',
array(
'members' => $members,
'start' => $start,
'expiry' => $expiry,
'etc' => $etc));
When the user checks out, the cart data disappears, and a new order is created. You can hook into woocommerce_add_order_item_meta to add the session meta data to the order meta data:
add_action(
'woocommerce_add_order_item_meta', 'hook_new_order_item_meta', 10, 3);
function hook_new_order_item_meta($item_id, $values, $cart_item_key) {
$session_var = 'my_session_var_name';
$session_data = WC()->session->get($session_var);
if(!empty($session_data))
wc_add_order_item_meta($item_id, $session_var, $session_data);
else
error_log("no session data", 0);
}
That's it. You do have to figure out how to get the order metadata out and do something useful with it, though. You may also want to clear the session data, from hooks into woocommerce_before_cart_item_quantity_zero, and woocommerce_cart_emptied. There's gist here which has some example code for this.
I've generated a simple access report that is used for purchasing.
Basically, there are two tables, one for purchase orders, and one for the items on the purchase orders.
For the items on an order, I store the item details, quantity ordered, quantity delivered, and price per unit. (plus a few other fields which aren't relevant in the context of this question).
I'm generating a report to list all outstanding items still on order, and the report has a calculated field showing the outstanding quantity * cost per item. This works perfectly fine.
What I'm struggling with, is showing a sum of this calculated field (i.e. a total cost of all outstanding items), but when I try to add a total to the column, it only gives me the option of adding an item count for the column. The column is a 'Currency' field.
What might I be doing wrong, or am I expecting too much from access?
Resolved. I created the only option that the GUI would allow (item count), then modified the query from:
=Count(*)
to
=Sum([Quantity]*[Cost])
Works perfectly.