How to add conditional price mark up in woocommerce? - php

I have been looking for this for a long time and can't seem to find an answer
I want to add mark up to my products based on their current price
e.g Products b/w £0-49 I want to £5
49-100 £10 and so on
But i want the markup to be part of the price displayed on shop and product page. (As In I don't want customer to see that I have added a markup)
any help will be appreciated.
Cheers

You can filter all the prices via woocommerce_get_price. Assuming that you want to add £5 for every £50 you could do something like:
function so_32352745_filter_price( $price ){
$factor = floor( $price / 50 );
$price = $price + $factor * 5;
return $price;
}
add_filter( 'woocommerce_get_price', 'so_32352745_filter_price' );

Related

Recalculate Taxes in Woocommerce

I got a Problem. In Woocommerce I added a custom product type (abo), where the user can choose a area size (in square meters) and there is a price per square meter and a dosage information where it says e.g. "You need 0.165kg per squaremeter" (it´s a fertilizer).
So for example if the user chooses 100 square meters and the price per square meter is 2.30€ and the dosage is 0.165kg/sqm the total price of the abo is 37.95€ and at a tax rate of 10.7% the tax amount should be 4.06€.
My Problem now is, I have normal products in the shop as well, where the standard calculation of the price and tax etc. works perfectly fine and you can buy the abo and a normal product.
So my question is, how can I recalculate the tax amount if I got an abo in cart?
I tried a few things for testing so far:
Adding a custom fee (that isn´t what I´m looking for, cause the tax has to be the same on cart, checkout and receipt):
add_action( 'woocommerce_cart_calculate_fees','custom_tax_surcharge_for_swiss', 10, 1 );
function custom_tax_surcharge_for_swiss( $cart ) {
if(current_user_can('administrator')) :
$percent = 10.7;
// Calculation
$surcharge = ( $cart->cart_contents_total + $cart->shipping_total ) * $percent / 100;
// Add the fee (tax third argument disabled: false)
$cart->add_fee( __( 'TAX', 'woocommerce')." ($percent%)", $surcharge, false );
endif;
}
calc_tax function (I think this one is the one I need) :
/ define the woocommerce_calc_tax callback
function filter_woocommerce_calc_tax( $taxes, $price, $rates, $price_includes_tax, $suppress_rounding ) {
if(current_user_can('administrator')) :
foreach(WC()->cart->cart_contents as $item) :
if($item['data']->get_type() == 'abo') :
$qty = $item['quantity'];
$ppqm = str_replace(',', '.', get_post_meta($item['product_id'], 'abo_product_price_per_kg')[0]);
$kgpqm = str_replace(',', '.', get_post_meta($item['product_id'], 'abo_product_kg_qm')[0]);
$realQty = floatval($kgpqm) * floatval($qty);
$priceCustom = $priceCustom + $realQty * floatval($ppqm);
endif;
endforeach;
return $taxes;
};
// add the filter
add_filter( 'woocommerce_calc_tax', 'filter_woocommerce_calc_tax', 10, 5 );
With the above function I got the problem, that I don´t have the cart contents directly so it could be, that the order of the items isn´t the same as the Tax order which I get from the function.
I would also note, that the woocommerce_calc_tax fires twice. I had for debug reasons an output in the console and if I have 2 items in cart, I got 4 outputs.
Hope my Problem is clear and that anyone could help me with that problem.
Thanks in advance.
I would look into the tax class for WooCommerce. You can specific target a item with a different tax rate. Unless I'm missing something this should actually work how you need it to out of the box.
When checking out it will also separate the taxes.

WooCommerce: Exclude product from threshold count for free shipping

I have set a threshold of 3 items for someone in the UK to get Free Shipping. Every time someone adds a product from a specific category, I automatically add a product to their cart which is a free gift.
My issue is that I am trying to exclude this free gift from the threshold count as currently this is being counted and people are getting free shipping without having 3 actual chargeable items in their cart.
I am unsure to how I can exclude product id 29 from counting, so any help would be greatly appreciated.
add_action( 'woocommerce_before_cart', 'ds_free_shipping_cart_notice' );
function ds_free_shipping_cart_notice() {
$threshold = 3;
$current = WC()->cart->get_cart_contents_count();
$billing_country = WC()->customer->get_billing_country();
if ( $current < $threshold && WC()->customer->get_billing_country() == 'GB' ) {
wc_print_notice( 'Nearly there! Order ' . ( $threshold - $current ) . ' more and shipping is on us', 'notice' );
}
}
I don't have woocommerce set up to be able to test this, but this is the type of logic you need:
$cart_items = WC()->cart->get_cart();
$current = WC()->cart->get_cart_contents_count();
foreach($cart_items as $item => $values) {
if ($values['product_id'] == 29) {
--$current;
}
}
This answer should help as well.
In general, though, this is a pretty fragile approach as you will need to change the hard-coded product ID every time you offer a new gift. It would be better to add a custom field to your products called "Free Gift" or something similar, set that value as needed for the items offered as free gifts, and then set up the code to exclude any of those items from the free shipping.

Custom price per month on product page in Woocommerce

Hello dear Stackoverflow. Long time reader, first time writer.
We are running a webstore (with Woocommerce on Wordpress) with high-end prices and of course then we have installment plans. I like to show this on the product page itself (namely before "add to cart form"). And I made this simple custom code which should calculate month-to-month price, however what I receive on the page is a stop in the code before "add to cart form" so there is an error here, but I cannot see it.
Is there anybody out there who can help me or point me in a direction? Really appreciate it!
By the way, no interest rate when the installment plan is 12 months or less. That's why I did not include interest rate.
(Code has been edited to reflect solution to the problem)
// Price per month
add_action( 'woocommerce_before_add_to_cart_form', 'price_per_month', 5 );
function price_per_month() {
global $product
$price = $product->get_price();
$price = $price + 295;
$months = 12;
$fee = 39;
//$register_fee = 295;
$price_per_month = ($price / $months);
$total_per_month = $price_per_month + $fee;
echo "
<div id='ppm-container'>
<div id='ppm-text'>
Pay from only ". $total_per_month ." NOK per month.
</div>
</div>
";
}
Few things
First you need to have
global $product
Second,
Use WP_DEBUG in order to see errors
Why are you writing add_action before defining the function price_per_month?

How to dynamically change the price of a product in Woocommerce - WordPress?

I need to change the price of my products in my store, with a 10% discount, if my customer is from some specific place, so, I wrote this code:
add_filter('woocommerce_price_html', 'my_price_edit');
function my_price_edit() {
$product = new WC_Product( get_the_ID() );
$price = $product->price;
echo $price * 0.9;
}
Ok, it works! But when in the checkout the price are normals without the 10% discount!
Does have some hook for change the price of the products in the checkout area or some different code to change correctly in the both (in the product page and checkout)?
New in Woocommerce too.
Your question looks really similiar to this one.
Adding Custom price with woocomerce product price in Cart & Checkout
I think you need to use the woocommerce_cart_item_subtotal hook to change the price in the cart directly (not exactly as a parameter)
I made a slightly modification to the code (changed price formula). I think that may help you.
add_filter( 'woocommerce_get_discounted_price', 'calculate_discounted_price', 10, 2 );
// Display the line total price
add_filter( 'woocommerce_cart_item_subtotal', 'display_discounted_price', 10, 2 );
function calculate_discounted_price( $price, $values ) {
// You have all your data on $values;
$price = $price*.09;
return $price;
}
// wc_price => format the price with your own currency
function display_discounted_price( $values, $item ) {
return wc_price( $item[ 'line_total' ] );
}

Woocommerce price after discount

I am not expert on WooCommerce, i cant find how to display price that calculate on it discount from coupon.
This code, display price after coupon, but also with shipping price - what i dont want, i want to display price after discount, before shipping and tax:
$woocommerce->cart->get_cart_total();
This code display the finnal price without discount and without shipping:
$woocommerce->cart->get_cart_subtotal();
So i tried something like that to get price after discount:
<?php
$totalp = $woocommerce->cart->get_cart_subtotal();
$totaldisc = $woocommerce->cart->get_total_discount();
$resultp = $totalp - $totaldisc;
echo ($resultp);
?>
But it show "0" number.
So, i guess there is simple function for what i want (i want to get total cart price after discount, before shipping), but i cant find this function.
Answer: i was need to use int's or floats without symbols, and the best way was to check WooCommerce docs here:
Woo - Docs , and to search for this variable and use it without () (not function). now it work :)
<?php
$first_number = $woocommerce->cart->subtotal;
$second_number = $woocommerce->cart->discount_total;
$sum_total = $first_number - $second_number;
print ($sum_total);
?>
Since you have the currency in the prices, you'll need to remove them.
I couldn't find WooCommerce code to just get the subtotal & discount without currency symbol, so let's just remove them manually:
function remove_currency($price) {
return (double) preg_replace('/[^0-9\.]+/', '', $price);
}
$totalp = remove_currency($woocommerce->cart->get_cart_subtotal());
$totaldisc = remove_currency($woocommerce->cart->get_total_discount());
$resultp = $totalp - $totaldisc;
echo $resultp;
This post is a bit old but I would like to inform anyone viewing that Woocommerce 3.x now uses:
WC()->cart
or you can use just
$cart
as long as you have the name_of_my_function( $cart ){} parameter callable.
Everything above is fine... I'm sure yet in hopes someone is not aware that $woocommerce global is no long in use.
There is also a way to loop foreach through the cart items (objects) to pick your price sub-totals using the cart_object.
foreach( $cart_obj->get_cart() as $key=>$value )
{
$priced = $value['subtotal'];
$new_price = $priced - ($predefined_referral_discount * 1);
$value['data']->set_price( $new_price );
}
Remember: name_of_my_function( $cart_obj ){} for this one.
Happy Coding

Categories