I have a WordPress site that uses WooCommerce plug-in. When a discount is applied to a product, the cart displays the price as crossed out with the discounted price at the bottom.
I would like to have a message with the last date of the discount, something like "$100-early bird rate applies up to 2015-09-30". How would I tweak the discount display on the cart or is there a plug-in for this?
Thanks in advance
Sale end date is stored as product meta, you can pull it using get_post_meta , the filter to use is get_price_html
add_filter( 'woocommerce_get_price_html', 'so32504214_price_html', 20, 2 );
function so32504214_price_html( $price, $product ){
$date = get_post_meta( $product->id, '_sale_price_dates_to', true );
if( strstr( $price, "<ins>" ) && "" != $date ) {
$date = date("Y-m-d", $date);
return str_replace( '</ins>', ' </ins> - early bird rate applies up to '. $date , $price );
}
return $price;
}
Related
I have the following code to add a suffix to carpet product prices:
add_filter( 'woocommerce_get_price_suffix', 'price_suffix', 10, 1 );
function price_suffix( $product ){
if ( has_term( 'carpet', 'product_cat' ) ) {
$suffix = ' per sqm';
update_option( 'woocommerce_price_display_suffix', $suffix );
return $suffix;
}
}
This does work but for some reason it is ignoring the products that have a variation.
On the products that have a variation, the price is always the same so I would like it to work in exactly the same manner.
Can anyone point me in the direction of how to do this please
I want to show a "free delivery" badge at every product with a price above 50$.
It should be visible on the product page and in the loop.
The problem is, that there could be more than one price. If you think about variations and sales (or even variations with variants on sale).
So I need to check the type of the product and have to search for the cheapest price to calculate with.
At the moment I'm using the following code.
It works ok sometimes. But on products with no active stock management it produces a timeout on the product page and doesn't work on archives (shows no message).
Also it produces some notices about not using the ID directly.
I don't feel safe with that code... Is there a better way to archieve it?
I tried a lot of ways but I'm not sure if I think about every possibilities about price, sales, stock or product types?!
<?php add_action( 'wgm_after_tax_display_single', 'wgm_after_tax_display_single_free_delivery', 10, 1 );
function wgm_after_tax_display_single_free_delivery( ) {
if (is_product()):
global $post, $product;
if ( ! $product->is_in_stock() ) return;
$sale_price = get_post_meta( $product->id, '_price', true);
$regular_price = get_post_meta( $product->id, '_regular_price', true);
if (empty($regular_price)){ //then this is a variable product
$available_variations = $product->get_available_variations();
$variation_id=$available_variations[0]['variation_id'];
$variation= new WC_Product_Variation( $variation_id );
$regular_price = $variation ->regular_price;
$sale_price = $variation ->sale_price;
}
if ( $sale_price >= 50 && !empty( $regular_price ) ) :
echo 'free delivery!';
else :
echo 'NO free delivery!';
endif;
endif;
} ?>
As you are using a custom hook, is difficult to test it for real (the same way as you)… Now this revisited code should work in a much better way than yours (solving error notices):
add_action( 'wgm_after_tax_display_single', 'wgm_after_tax_display_single_free_delivery', 10, 1 );
function wgm_after_tax_display_single_free_delivery() {
// On single product pages and archives pages
if ( is_product() || is_shop() || is_product_category() || is_product_tag() ):
global $post, $product;
if ( ! $product->is_in_stock() ) return;
// variable products (get min prices)
if ( $product->is_type('variable') ) {
$sale_price = $product->get_variation_sale_price('min');
$regular_price = $product->get_variation_regular_price('min');
}
// Other products types
else {
$sale_price = $product->get_sale_price();
$regular_price = $product->get_regular_price();
}
$price = $sale_price > 0 ? $sale_price : $regular_price;
if ( $price >= 50 ) {
echo __('free delivery!');
} else {
echo __('NO free delivery!');
}
endif;
}
Code goes in function.php file of your active child theme (or active theme). It should work better.
I'm building a website where i'll sell some fine art prints and i would like to set an automated discount when a customer buy the first print of a serie. Each of my prints are limited to 15 pieces and i would like to set a 30% discount for the first sell (#1/15) of each series. Then, for the next prints (#2 to #15), price goes back to normal.
Edit :
I did progress on my problem ! I did some research and tried a lot of different things and i found how to set the custom price i wanted on my product page (regular price + discount price) with the rule to only apply it if the related item available stock is 15.
regular price + discount price
And here's the code is used :
// Generating the product "regular price"
add_filter( 'woocommerce_product_get_regular_price', 'dynamic_regular_price', 10, 2 );
add_filter( 'woocommerce_product_variation_get_regular_price', 'dynamic_regular_price', 10, 2 );
function dynamic_regular_price( $regular_price, $product ) {
$stock_qte = $product->get_stock_quantity();
if( empty($regular_price) || $regular_price == 0 )
return $product->get_price();
else
return $regular_price;
}
// Generating the product "sale price"
add_filter( 'woocommerce_product_get_sale_price', 'dynamic_sale_price', 10, 2 );
add_filter( 'woocommerce_product_variation_get_sale_price', 'dynamic_sale_price', 10, 2 );
function dynamic_sale_price( $sale_price, $product ) {
$stock_qte = $product->get_stock_quantity();
if( $stock_qte == '15')
return $product-> get_regular_price() * 0.7;
else
//return $product->get_regular_price();
return $regular_price;
};
// Displayed formatted regular price + sale price
add_filter( 'woocommerce_get_price_html', 'dynamic_sale_price_html', 20, 2 );
function dynamic_sale_price_html( $price_html, $product ) {
$stock_qte = $product->get_stock_quantity();
if( $stock_qte == '15')
$price_html = wc_format_sale_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ), wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ) ) . $product->get_price_suffix();
else
$price_html = wc_price(wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ). $product->get_price_suffix());
return $price_html;
}
It's almost working but the code related to the cart values updates is not fully working. The cart sub-total is good and take count of the discount but the individual price of each product is not updated and shown as without discount. It's the same on the cart page.
cart (from mini cart)
cart (cart page)
add_action( 'woocommerce_before_calculate_totals', 'alter_price_cart', 9999 );
function alter_price_cart( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
// IF CUSTOMER NOT LOGGED IN, DONT APPLY DISCOUNT
//if ( ! wc_current_user_has_role( 'customer' ) ) return;
// LOOP THROUGH CART ITEMS & APPLY 30% DISCOUNT
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
$price = $product->get_price();
$cart_item['data']->set_price( $price * 0.7 );
}
}
I'll also have to add the stock availability logic for the mini-cart / cart / checkout page but i'll do that in a second time.
I must have made some mistakes in the loop throught the cart but i cannot catch the issue :( Any idea about how to hook the price of each product and change the displayed value ?
Have a nice day,
Quentin
I want to show the product price including and exclunding tax under each product in the catalog page of my Woocommerce shop.
Its already working, but it is not showing anything for variable products where I have only one variation. On single products it is also working.
Also I do get the notification:
Notice: WC_Product::get_price_including_tax ist seit Version 3.0 veraltet! Benutze stattdessen wc_get_price_including_tax.
Notice: WC_Product::get_price_excluding_tax ist seit Version 3.0 veraltet! Benutze stattdessen wc_get_price_excluding_tax.
But if I do so, it is not working anymore at all.
add_action( 'woocommerce_after_shop_loop_item_title', 'preise_notice', 10 );
function preise_notice() {
global $product;
if ( $price_html_incl_tax = $product->get_price_including_tax() )
if ( $price_html_excl_tax = $product->get_price_excluding_tax() ) {
echo '<div class="product-prices-excl-vat"><a>ab ' . wc_price($price_html_excl_tax) . ' netto</a></div>';
echo '<div class="product-prices-incl-vat"><a>(' . wc_price($price_html_incl_tax) . ' inkl. 19% MwSt.)</a></div>';
}
}
The wc_get_price_including_tax and wc_get_price_excluding_tax functions expect $product as an argument. So you will have to pass it like this:
wc_get_price_including_tax( $product )
Also it seems like a good idea to get the product's tax rate instead of hard coding it in. Maybe in the future you will have products that do not have a 19% tax rate. I also included the currency argument to the wc_price function so the price will be shown in the shop's currency.
You can use the following snippet that will get the product's tax rate and prints the prices including and excluding tax:
add_action( 'woocommerce_after_shop_loop_item_title', 'add_product_price_incl_and_excl_tax', 10 );
function add_product_price_incl_and_excl_tax() {
global $product;
$tax_rate = '';
$tax_rates = WC_Tax::get_rates( $product->get_tax_class() );
//Check the product tax rate
if ( !empty( $tax_rates ) ) {
$tax_rate = reset($tax_rates);
$tax_rate = sprintf( ' inkl. %.0f%% MwSt.', $tax_rate['rate'] );
}
//Print product prices including tax and tax percentage, and excluding tax
printf( '<div class="product-prices-excl-vat">ab %s netto</div>', wc_price( wc_get_price_excluding_tax( $product ), array( 'currency' => get_woocommerce_currency() ) ) );
printf( '<div class="product-prices-incl-vat">%s%s</div>', wc_price( wc_get_price_including_tax( $product ), array( 'currency' => get_woocommerce_currency() ) ), $tax_rate );
}
Using woocommerce, I have several categories but would like to add "per person" to the category "cookery classes".
I have written this function, which works on all products.
I can't seem to get it to work only on the category.
'/*---------Adds per person to price -------*/
add_filter( 'woocommerce_get_price_html', 'custom_price_html', 500, 2 );
function custom_price_html( $price, $product ) {
if (( int ) is_category( 'Cookery Classes','product_cat', $product->id ) )
$price .='per person';
$price .= '<span class="from"><BR>' . _x('per person', 'price_per_person','woocommerce') . ' </span>';
return $price;
}'
found and installed this plugin:
https://wordpress.org/plugins/woocommerce-jetpack/
Allows you to add custom labels to prices per product catagory