WooCommerce - get number of different products in cart - wordpress

There is WC()->cart->get_cart_contents_count() to show the total number of products in the WooCommerce cart.
But how do you get the number of different products?
For example:
3 * product A
2 * product B
WC()->cart->get_cart_contents_count() would return 5.
But we want to get 2.

You can simply use count(WC()->cart->get_cart()); as get_cart() returns an array of items.
Documentation

$cart = $_SESSION['wfcart'];
$count=0
foreach($cart->items as $item){
$count+= $cart->itemqtys[$item];
}

Related

Woocommerce - Exclude Specific Country Tax from Product at checkout

I would like to exclude all tax rates from a specific country inside my checkout containing a specific product (which is a custom fee (tip)) - I tried to use another Tax rates like 'Zero Tax Rates' in woocommerce to set Canada to 0%, But I have an issue with all the other countries using this method as only Standard Rate seems to calculate the rates based on the total (including the tip).
I found this code to exclude a all tax rates based on a zip code :
/**
* #snippet Remove Tax if Field Value Exists - WooCommerce Checkout
* #how-to Get CustomizeWoo.com FREE
* #author Rodolfo Melogli, BusinessBloomer.com
* #testedwith WooCommerce 4.5
* #donate $9 https://businessbloomer.com/bloomer-armada/
*/
add_action( 'woocommerce_checkout_update_order_review', 'bbloomer_taxexempt_checkout_based_on_zip' );
function bbloomer_taxexempt_checkout_based_on_zip( $post_data ) {
WC()->customer->set_is_vat_exempt( false );
parse_str( $post_data, $output );
if ( $output['billing_postcode'] === '32444' ) WC()->customer->set_is_vat_exempt( true );
}
I'm able to edit it to choose 'Billing_country' === CA' instead but I would also need this function to target a specific product so that the rest of the website would not use this function. I need Canadians customers to get no taxes rates for this specific products, but not for the others countries.
Thanks for your help.

Woocommerce tax based on subtotal amount [duplicate]

In My Wordpress e-commerce web site I use WP Hotel Booking, a plugin for hotel room bookings. The checkout process is done using WooCommerce.
The Issue: We have different rooms with different pricing.For example :
Room A price - 1500
Room B Price - 2700
Room c price - 2200
GST Tax is set at 12% for rooms wich price is below 2500 and 18% for rooms above 2500.
Since I am using WP Hotel Booking for this custom product (room Management), I am unable to use the Additional Tax Classes option in woocommerce to set different tax classes.
I need your help in writing a function to check the room value and then decide what tax needs to be set for the given room.
Thanks
This is something accessible and easy.
1°) you need to create in your WooCommerce Tax settings 2 new Tax classes. In this example I have named that tax classes "Tax 12" and "Tax 18". Then for each of them you will have to set a different percentage of 12% and 18%.
2°) Now here is a custom function hooked in woocommerce_before_calculate_totals action hook that is going to apply a tax class based on the product price. I don't use the tax class names, but the tax class slugs, that are in lowercase and spaces are replace by a hyphen.
So Here is that code:
add_action( 'woocommerce_before_calculate_totals', 'change_cart_items_prices', 10, 1 );
function change_cart_items_prices( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
foreach ( $cart->get_cart() as $cart_item ) {
// get product price
$price = $cart_item['data']->get_price();
// Set conditionaly based on price the tax class
if ( $price < 2500 )
$cart_item['data']->set_tax_class( 'tax-12' ); // below 2500
if ( $price >= 2500 )
$cart_item['data']->set_tax_class( 'tax-18' ); // Above 2500
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works on WooCommerce version 3+

How add product to cart woocomerce by hook?

I'm trying to implement the following solution:
add_action('woocommerce_calculate_totals' , 'buy3');
function buy3(WC_Cart $cart){
global $woocommerce;
$woocommerce->cart->add_to_cart(6373,1,7444);
}
but the problem after execute the code all quantity the product add To cart not one quantity
Can you Confirm that Sold individually is un-checked? Because WooCommere add_to_cart() Force the quantity to 1 if sold individually and it also check for existing item in cart
Also Parameters for add_to_cart() function are as below :
global $woocommerce;
$woocommerce->cart->add_to_cart($product_id,$quantity,$variation_id, $variation,$cart_item_data);
According to the code you provided, you have supplied quantity = 1;

How to force remove products from WooCommerce cart?

I'm building a site with Wordpress and WooCommerce for someone for the purposes of a business, but they have certain items that cannot be sold together - essentially the opposite of a force sell or chained products. If a customer puts product A in their cart, I don't want them to be able to put product C in with it, but B and D are fine. Alternatively, when it goes to checkout, separating the products into two separate orders would work as well. Is there any way to do this? This is my first time using Wordpress, so I'm a bit at a loss.
You can use this code below :
add_action('woocommerce_add_to_cart_handler','mycustomfuncion',11,2);
function mycustomfuncion($p,$q)
{
global $woocommerce;
$cartItem = $woocommerce->cart->cart_contents;
$currentProductId = $q->id;
foreach($cartItem as $item)
{
$productItemId = $item['product_id'];
///your condition will be here
}
return $q;
}
You will get all product ids which are in cart by the $productItemId. And you will get current product id (which user wants to add to cart ) by $currentProductId. After your conditions if you want to allow to add to cart the product then return $q from the function otherwise don't return any value.

woocommerce calculate price server-side

I need do sell a product whose price depends on a complex calculation over non-discrete parameters set by the customer on the product page, and also on a custom database query result.
How can i calculate the price server-side every time the customer changes parameter-values and apply that price when the customer adds to cart?
i read a similar post whose answer suggests a WC plugin, but even that plugin doesn't satisfy my needs.
Thanks
Probably, you should try to use woocommerce_get_price filter
add_filter('woocommerce_get_price', 'get_dynamically_generated_price', 10, 2);
function get_dynamically_generated_price($price, $product) {
// ... here doing your magic with $price based on $product
// ...
return $price;
}

Resources