Woocommerce get product subtotal in cart page - wordpress

I am trying to implement ajax functionality to my cart page for updating quantity. So far so good. But I am stuck at one point - updating the product subtotal. I was trying to use this : get_product_subtotal($_product, $quantity), but could not succeed. Because the 1st argument ($_product) is an object. I don't know how to get that. I can get product id and quantity which I passed with ajax call. Now I need the project object with the id. Can anyone help with solution or suggestion?

You can create product object with the following code
$product = new WC_Product( $product_id );

Related

Modern Events Calendar filtering with variable on frontend

I am using both WooCommerce (WC) and Modern Events Calendar (MEC). I am trying to display a list view of the MEC events on a corresponding WC product page. As there is no connecting between these two instances it will need some creativity to get to work.
The WC product pages won't be changing a lot in the future so I figured I'd use the product ID. Then every event I make in MEC, I tag with the WC product ID. Then on the frontend I have the MEC shortcode (the list view widget) filter it's output on the WC product ID. Sounds simple enough but I have not been able to figure out how I can get it to work.
In my quest to archive this I have created a shortcode in MEC and changed the filter setting to filter by tag with the following variable [prod_id] after I created this shortcode in the functions file:
function product_id_shortcode() {
global $product;
$id = $product->get_id();
return $id;
}
add_shortcode( 'prod_id', 'product_id_shortcode' );
Unfortunately this did not give the expected result, so I assume filtering by [prod_id] is not being accepted. Any help would be greatly appreciated!

Differences between WooCommerce price filters

First of all, thank you for your support all those years.
I am building a role-based pricing plugin for a client and I am having an existential crisis over filters WooCommerce uses for prices. I made this post in hope someone can explain this so someone in the future can find this post.
I managed to understand how filters for simple products work but I don't have an idea how equivalent filters for variable products work
Simple products
As from what I understood, you can override price, regular price, and sale price for simple products returning some value inside filters woocommerce_product_get_price, woocommerce_product_get_regular_price, and woocommerce_product_get_sale_price. Those filters will save provided data into $product object and you can easily access them later in woocommerce_get_price_html filter by using $product->get_regular_price
Variable products
Looks like variable products work differently. For example, they dont save overriden price to $product or $variation object and you cannot fetch overriden prices by calling $variation->get_regular_price.
As I understood, following filters change price of the product on add-to-cart variation select, and in the cart and do not save them in $variation object.
woocommerce_product_variation_get_price
woocommerce_product_variation_get_regular_price
woocommerce_product_variation_get_sale_price
I didnt manage to fully understand what following filters do. But when I turn them on loop price changes and product price changes but prices are removed from add-to-cart select and cart prices are not changed.
woocommerce_variation_prices_price
woocommerce_variation_prices_regular_price
woocommerce_variation_prices_sale_price

woocommerce add_to_cart function not add two different products in cart

i am creating a custom button to add products to cart using add_to_cart($product_id, $quantity) function. It's working well for the single property but when I change product_id with another product id then it shows error.
$cart = $woocommerce->cart->add_to_cart($product_id, $quantity);
when I try same with different product_ids it stops working.
any solution?
Okay, thanks for the help. Finally, the problem is solved. Actually, 93 product_id product is out of stock that's why it returns false. add_to_cart() function does not work with a product out of stock.

WooCommerce custom quantity available in URL

Hi I have a web page with Wordpress and WooCommerce, but the stock (quantity available of each product) I get it in a URL like this (http://www.mystock.com/page.php&id_item=623533&coloritem=68)
I have to put in the link, the SKU (e.g. 623533-68) and I get in return a CVS whit some data
I need to update (or at least show in memory/in front-end) the real price_a and quantity available. How can I do that?
EDIT 1:
For now I donĀ“t need the code for get the CSV info, for information is:
$url = "http://www.mystock.com/page.php&id_item=623533&coloritem=68";
$csv = file_get_contents($url);
$data = str_getcsv($csv);
$data
is an array of string, and I can get the info that I need.
But I still don't know what php file of wordpress to change, for show the actual_quantity and the if the customer add in a cart the product, know if quantity is greater than zero.
EDIT 2:
In this page show the oficial way that update the quantity, whit the function
$product->set_stock_quantity(SomeFloat); but doesn't works. $product is a new WC_Product object. And desn't show any error, just, nothing happen.
Thanks

Woocommerce: how do I add metadata to a cart item?

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.

Resources