Get Base Price in WooCommerce Bookings - php

I have a product in WooCommerece that has a display and base price. The following code is used:
global $woocommerce;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $cart_item ) {
$productID = $cart_item['product_id'];
break; //Take the first as an example
}
$product = new WC_Product($productID);
$base_price= $product->get_price();
$display_price = $product->get_display_price();
My issue is, the base and display price come back as the same value but they are maintained differently in the back end.
Update: Tax Settings
I understand this issue may be related to Tax settings. Here are mine:
Taxes Enabled
Prices entered inclusive of tax
Calculate tax based on shop address
Shipping tax class based on cart items
No rounding
No additional tax classes
Display prices in shop excluding tax
Display prices in cart / checkout excluding tax
No suffix
Display tax totals as itemised
There is also a blanket standard rate which is zero-rate.
And for the Product:
Taxable
Tax Class is Standard
Update
The issue stems from the fact I am using the WooCommerence Booking plugin. To get the base price of a booking:
global $woocommerce;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $cart_item ) {
$productID = $cart_item['product_id'];
break;//Take the first as an example
}
$product = new WC_Product($productID);
$admission = $product->wc_booking_cost;

$product->get_regular_price() returns the regular price.
$product->get_sale_price() returns the sale price if product is on sale.
$product->get_price() returns the price of product (sale or regular depending on what is current).
$product->get_display_price() Returns the price including or excluding tax, based on the 'woocommerce_tax_display_shop' setting.

Related

Woocommerce - Apply 10% discount to the lowest priced product in the cart based on several conditions

I'm struggled with one problem and the problem is as it follows. In our store users could create a package from 3 products. I need to apply a 10% discount to 2 products in cart.
PRODUCT required to get a discount = 10 euros
PRODUCT = 15 euros - you could get a discount from this product
PRODUCT = 12 euros - you could get a discount from this product
Discount should apply always to required product and the lowest price product 2 or 3.
Cart examples
If there is product 1, 2 and 3 in cart you will get a 10% discount to product number 1 and product number 3.
If there is a product number 1 and 3 in cart you will get a 10% discount to the both of them
If there is a product number 1 and 2 in cart you will get a 10% discount to the both of them
If there is a product number 1 only, product number 2 only, product number 3 only, then no discount is applied to your cart
If there is some of the combinations and other products which are not these 3 mentioned products above, the discount will be applied only for the mentioned products as it follows from cart examples above.
If there are more sets - if you want to order 2 products from (product number 1 and product number 2) or (product number 1 and product number 3) you have to add always 1 more to the both of them to get another 10% sale from another set so quantity will be 2/2. If theres quantity 2 (ID1)/1 (ID2) no discount will be applied for the second set only for the first one.
My code so far but I'm totally lost
function apply_discount_ten( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Product ID needs to be in cart to get a discount
$product_id = 2530;
// Products in cart
$products_in_cart = count( $cart->get_cart() );
//Set conditions
if ( $products_in_cart && (matched_cart_items($product_id)) {
// Set array
$product_prices = array();
// Loop though cart items
foreach ( $cart->get_cart() as $cart_item ) {
// Product
$product = $cart_item['data'];
// Get price
$product_price = $product->get_price();
// Push
$product_prices[] = $product_price;
}
// Sort: low to high
asort( $product_prices );
// Number of products receive a discount
$products_receive_discount = What to do here;
// Set variable
$total = 0;
// Loop trough
foreach ( array_slice( $product_prices, 0, $products_receive_discount ) as $product_price ) {
// Calculate
$total += $product_price;
}
// Calculate discount
$discount = ( $total * 10 ) / 100;
// Add fee
$cart->add_fee( 'Discount', $discount, true );
}
}
add_action( 'woocommerce_cart_calculate_fees', 'apply_discount_ten', 10, 1 );
Thank you in advance

Coupon with 2 different percentage discounts based on product category in Woocommerce

I am looking for a Woocommerce hook that will help to change the discount percentage based on 2 different product category restrictions when a specific coupon is applied.
For example if customer add a specific coupon I will like to:
If a cart item is from product category A then it will give 10% discount on that item.
if it's in product category B it will give 20% discount on that item
Update total cart price
Is there any hook that could be available to achieve this? Any available action hook or filter hook?
This is my code so far:
add_filter( 'woocommerce_get_discounted_price', 'apply_coupon', 10);
function apply_coupon($price) {
global $woocommerce;
$product=$woocommerce->cart->product;
if(has_term( 'duplex-blinds', 'A' ,$product->id)){
get_product_cart_price;
10% DISCOUNT
}
if(has_term( 'duplex-blinds', 'A' ,$product->id)){
20% DISCOUNT
}
upadte total_discunt_incart($new_discount);
upadte new_price_in_cart($new_price);
upadte new_price_in_checkout($new_price);
return $price;
}
The important thing is i need to modify the total cart price , total checkout price , total discount price and discount price need to send to Paypal.
My shop have many hooks that's why woo commerce default coupon calculation is going to wrong. And i noticed that in cart page discount price is coming correctly based on the custom product value, but it not get updated from the original cart amount, so the total price remain the same.
But in checkout page discount price is calculated based on the product original price not the product custom price so the discount is coming wrong and also it is not minimize from the total price also...
The following is a completely different way to make that works… This answer code will enable a coupon code with 2 different discounts percentage based on 2 specific product categories.
Lest say for example that your related product categories are:
For the coupon discount of 10%, the product category slug will be 'hoodies'
For the coupon discount of 20%, the product category slug will be 't-shirts'
(you can use product category Ids, slugs or Names in the code)
That will need 2 Steps:
Coupon settings (Set correctly your coupon code):
Discount type: Percentage
Amount: 10
Restrictions > Product categories (names displayed): "Hoodies" and "T shirts"
You can have other settings if you need
The settings inside the code function:
Coupon code: set your coupon code in lowercase
The 't-shirts' product category slug (for 20% of discount).
Now Here comes the code (where you will add your settings):
add_filter( 'woocommerce_coupon_get_discount_amount', 'alter_shop_coupon_data', 20, 5 );
function alter_shop_coupon_data( $round, $discounting_amount, $cart_item, $single, $coupon ){
## ---- Your settings ---- ##
// Related coupons codes to be defined in this array (you can set many)
$coupon_codes = array('10percent');
// Product categories at 20% (IDs, Slugs or Names) for 20% of discount
$product_category20 = array('hoodies'); // for 20% discount
$second_percentage = 0.2; // 20 %
## ---- The code: Changing the percentage to 20% for specific a product category ---- ##
if ( $coupon->is_type('percent') && in_array( $coupon->get_code(), $coupon_codes ) ) {
if( has_term( $product_category20, 'product_cat', $cart_item['product_id'] ) ){
$original_coupon_amount = (float) $coupon->get_amount();
$discount = $original_coupon_amount * $second_percentage * $discounting_amount;
$round = round( min( $discount, $discounting_amount ), wc_get_rounding_precision() );
}
}
return $round;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Here is an illustrated real working example (with screen shots):
The 1st cart item (from 'hoodies' product category) get 10% of discount $40 x 10% = $4
The 2nd cart item (from 't-shirts' product category) get 20% of discount $30 x 20% = $6
So the total discount is $4 + $6 = $10 … That works!
I using this code, but it give 100% on the 20% category, but the code you provide does work if you change the 0.2 to 0.02
$second_percentage = 0.2; // 20 %
change too:
$second_percentage = 0.02; // 20 %
or:
$second_percentage = 0.015; // 15 %
If you need 15% discount

Adding a custom cart negative fee tax issue in woocommerce

I have a shipping discount in my woocommerce site, I would like to show it on cart page and checkout page, for that I used add_fee() method.
WC()->cart->add_fee( 'Shipping Discount', -20, false );
It subtract 20 from total amount, but when I go to check order in orders inside admin it gives discount on tax too according to tax rate I configured. Is there any alternative to add discount
How can I do this in woocommerce?
For negative cart fee there is a bug related to taxes as they are applied on all tax classes even if the it's not taxable.
Now you can make a custom global cart discount This way where you will be able to define the type of discount you want, the discount amount and the source price to be discounted.
This code will display the he correct cart discounted gran total
Once submitted this discounted gran total will be passed to the order.
The code:
// Apply a discount globally on cart
add_filter( 'woocommerce_calculated_total', 'discounted_calculated_total', 10, 2 );
function discounted_calculated_total( $total, $cart ){
$amount_to_discount = $cart->cart_contents_total;
$percentage = 10; // 10% of discount
$discounted_amount = $amount_to_discount * $discount / 100;
$new_total = $total - $discounted_amount;
return round( $new_total, $cart->dp );
}
Code goes in function.php file of the active child theme (or active theme).
Tested and works.
You should need to make some additional code to display the discount amount (in cart and checkout pages and order-view) and to pass it in the order as meta data and so on…

How to set woocommerce order total to calculate ONLY shipping costs

I'm using woocommerce as a platform for booking products for delivery, People pay only for the shipping costs but not for the products.
How do I get the total amount to reflect ONLY the shipping costs and not the product prices?
I know I can set the individual product price to zero but I prefer to leave the prices there so that people can sort the items by price. Hopefully it's just a simple hack.
Thanks!
Add shipping role and assign them to all.
Or create a function that remove the product cost from the total in function.php file.
This might help:
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );
function add_custom_price( $cart_object ) {
$custom_price = $shipping; // Add the shipping cost
foreach ( $cart_object->cart_contents as $key => $value ) {
$value['data']->price = $custom_price;
}
}

WooCommerce - Make a set of coupons adding a fixed fee to an order

A client has a very odd request: they would like for a set of coupon codes to add a fee to an the order. Is this possible?
The problem they are trying to solve is this:
Currently the store offers free shipping to the lower 48 United States, for which I have shipping classes and table rates properly configured. However, the coupons the customer has sold on a daily deal site are supposed to remove the free shipping benefit and add a fixed shipping fee.
How can achieve this?
Yes you can do it with that snippet code (for a set of coupons):
function coupon_add_cart_fee( $bookable_total = 0 ) {
$has_coupon = false;
// Set here your specials coupons slugs (one by line - last one have no coma)
$coupon_codes = array (
'your_coupon_code_slug1',
'your_coupon_code_slug2',
'your_coupon_code_slug3'
);
// Set here your fee amount or make fees calculation (see the links in reference)
$fee = 20;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// checking if that "special" coupon is applied to customer cart
foreach ($coupon_codes as $coupon_code) {
if ( WC()->cart->has_discount( $coupon_code ) ) {
// If yes apply the fee to the cart
if ( !$has_coupon ) {
$has_coupon = true;
break;
}
}
}
if ( $has_coupon ) {
WC()->cart->add_fee( 'Fees: ', $fee, false );
}
}
add_action( 'woocommerce_cart_calculate_fees','coupon_add_cart_fee' );
This code is tested and it works. It goes on function.php file of your active child theme or theme.
TAX OPTIONS with add_fee() method
IMPORTANT: The fact that TAX is working or not with add_fee() method depends first of your tax settings in woocommerce.
Class WC_Cart add_fee() method, adds additional fee to the cart.
add_fee( string $name, float $amount, boolean $taxable = false, string $tax_class = '' )> <!-- language: lang-css -->
Parameters:
$name Unique name for the fee. Multiple fees of the same name cannot be added.
$amount Fee amount.
$taxable (default: false) Is the fee taxable?
$tax_class (default: '') The tax class for the fee if taxable. A blank string is standard tax class.
Reference:
Add tax free fees to WooCommerce cart programmatically
Class WC_Cart - add_fee() method
Class WC_Cart - has_discount() method

Categories