my woocommerce mini cart shows the product price including tax by default. (the prices are shown excluding the tax all over the website) I'd like to show the prices excluding tax on mini cart, but keep showing them including tax in the main cart page. I believe it can be achieved with a hook but I wasn't able to do it. Any help would be very much appreciated.
Woocommerce provides two function wc_get_price_excluding_tax and wc_get_price_including_tax that you can use to display the product with or without tax.
What you might need to do is update your cart template file to use wc_get_price_excluding_tax to display the item price.
You can look at how these functions are implemented here: https://docs.woocommerce.com/wc-apidocs/function-wc_get_price_including_tax.html.
One thing to note that these function take a WC_Product object product as their first parameter, so you might need to get the product from the cart item first. You can do it by using wc_get_product. An example:
foreach( WC()->cart->get_cart() as $cart_item ){
$product_id = $cart_item['product_id'];
$product = wc_get_product($product_id);
// Display the price here
echo wc_get_price_excluding_tax($product);
}
Related
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
Hi community I have a pickle of a problem involving TAX, and I really need your help with it. I need a solution whereby if a certain product is added to the cart then the Tax rate for all products that are in the cart drop to the lower tax rate. Here is how this problem arises, one of the products is a service, this product can only be chosen if other products are also in the cart. If the customer chooses this product then the tax rate for all products drops to a lower tax rate, (as the products become part of a service which is taxed here at a lower rate) if the customer doesn't choose this product then the rate remains at the higher rate. - The logic is .. If cart contains product A then tax rate of all products changes to Lower Tax Rate.
I found this code which has a similar logic to it, but not quite there, this changes the tax class based on the subtotal being less than 110. I need to change the tax class based on wether a specific product is in the cart.. I have tried changing "subtotal" to "product" changing the "<= 110" to "= 'fitting' (fitting is the product title) but it didn't work. Any code gurus got any input on this one?
add_filter( 'woocommerce_product_tax_class', 'big_apple_get_tax_class', 1, 2 );
function big_apple_get_tax_class( $tax_class, $product ) {
if ( WC()->cart->subtotal <= 110 )
$tax_class = 'Zero Rate';
return $tax_class;
}
I have two variations in my product. In cart page I need to choose the variations and with respective to variations I need to display the price.
How this can be done? Any track is welcome.
Kindly help me because I have searched a lot a solution for this, and I didn't anything yet that is working as I expect.
I have tried the following code in cart.php to get the variations.
<?php if( ! empty( $_product ) && $_product->get_type( 'variation' ) ){
$variation_attributes = $_product->get_variation_attributes();
$first_attribute = array_slice($variation_attributes, 0,1);
$second_attribute = array_slice($variation_attributes, 1,2); } ?>
By $first_attribute and $second_attribute, I populate the variations in a select box.
I have two attributes and four variations. So according to the variation I need to display the Price. I am not using third party PlugIn.
I believe this is already a part of WooCommerce's set up - as long as you have the variation set up specifically for X-license and X-plan you should be able to set the price to that variation.
Variation Data
Each variation may be assigned.
General:
Regular Price (required) – Set the price for this variation.
Source and full documentation: https://docs.woocommerce.com/document/variable-product/
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.
Is it possible to add a unique name to a specific product when it's added to the shopping cart using the woocommerce API?
For example suppose I sell pets. The user decides he wants a dog named spot. He then clicks on a particular dog breed (the product) say border collie and that product should appear in the shopping cart as a border collie named spot.
To answer your question, yes. You can change things in your own code by adding filters. So for example, say you want to change the name of the product in the cart. In wp-content/plugins/woocommerce/templates/cart/cart.php there is a filter called 'woocommerce_cart_item_name' where the title is outputted. You can modify this in your own code like so:
function your_function( $title, $cart_item, $cart_item_key ){
/*code to modify title*/
return $title;
}
add_filter('woocommerce_cart_item_name','your_function',10,3);
You could add this to the functions.php of your theme.
How you get the name spot in this function depends on your situation.
Read more about filters at https://codex.wordpress.org/Function_Reference/add_filter