Woocommerce: display crossed price for free products - woocommerce

I can't display crossed price for 0 priced variable products.
That is my code. Where I'm going wrong ?
add_filter( 'woocommerce_get_price_including_tax_html', 'woocommerce_get_regular_price_html', 'woocommerce_get_price_html', 'price_free_zero_empty', 9998, 2 );
function price_free_zero_empty( $price, $product ){
if ( '' === $product->get_price_including_tax() || 0 == $product->get_price_including_tax() ) {
$regular_price = $product->get_price_including_tax();
$price = '<del>' . wc_price( $regular_price ) . '</del> '. '<span class="woocommerce-Price-amount amount">'.__("Free", "woocommerce").'</span>';
}
return $price;
}

function wpglorify_price_free_zero_empty( $price, $product ) {
if ( $product->get_price() == 0 ) {
if ( $product->is_on_sale() && $product->get_regular_price() ) {
$regular_price = wc_get_price_to_display( $product, array( 'qty' => 1, 'price' => $product->get_regular_price() ) );
$price = wc_format_price_range( '<del>' . wc_price($regular_price) . '</del>', __( 'Omaggio!', 'woocommerce' ) );
} else {
$price = '<span class="amount">' . __( 'Omaggio!', 'woocommerce' ) . '</span>';
}
}
return $price;
}
add_filter( 'woocommerce_get_price_html', 'wpglorify_price_free_zero_empty', 10, 2 );

Related

Woocommerce: show the discounted price next to the variation price when the variation is selected

So, I have a woocommerce shop where a 10% discount is applied to all products in the cart and at checkout if a certain payment gateway is used.
My shop has both simple and variable products, and they are all virtual products. I thought of showing on the product page the following information:
the lowest price (for variable products)
the 10% discounted price I could get at checkout.
To do this I used the following code for variable products:
add_filter( 'woocommerce_variable_sale_price_html', 'wpglorify_variation_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'wpglorify_variation_price_format', 10, 2 );
function wpglorify_variation_price_format( $price, $product ) {
$prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
$price = $prices[0] !== $prices[1] ? sprintf( __( 'A partire da: %1$s (' . $prices[0]*.9 . '€ se paghi online)' , 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
$prices = array( $product->get_variation_regular_price( 'min', true ), $product->get_variation_regular_price( 'max', true ) );
sort( $prices );
$saleprice = $prices[0] !== $prices[1] ? sprintf( __( 'A partire da: %1$s (' . $prices[0]*.9 . '€ se paghi online)' , 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
return $price;
}
For simple products I used this snippet instead:
add_filter( 'woocommerce_get_price_html', 'change_displayed_price_html', 10, 2 );
function change_displayed_price_html( $price, $product ) {
if( $product->is_type('simple') ){
$regular_price = (float) $product->get_regular_price();
$saving_price = $regular_price*.9;
$price .= sprintf( __(' (€%s se paghi online)', 'woocommerce' ), $saving_price );
}
return $price;
}
Everything works fine, but on variable products I have a problem. When I select a variation, I would like to give the same information next to the price being shown and related to that variation. I tried with the following snippet:
add_filter('woocommerce_available_variation', 'variation_custom_text', 10, 3 );
function variation_custom_text( $price, $product, $variation ) {
$variation_price = (float) $product->get_variation_price();
$saving_price = $variation_price*.9;
$price['availability_html'] .= sprintf( __(' (€%s se paghi online)', 'woocommerce' ), $saving_price );
return $price;
}
The problem is that the discounted price is calculated on the price of the first variation, regardless of which variation I select. For example, if variation A = $100, a discounted price of $90 is shown. If I select variation B = $120, a discounted price of $90 is always shown. Where am I doing wrong?
Thanks for your answers.

Display WooCommerce Unit Prices at Product variations

I like to display unit prices at variable products. I like it to do based on a product attribute whic contains the following: 5 kg, 2 l etc. So, I get the attribute, extract the numbers from it and do some math. The problem is, that all of the code displays the multipier number of 1. Here is my code:
function product_variation_unit_price( $data, $product, $variation ) {
$price = wc_get_price_to_display( $variation );
$unit = array_shift( wc_get_product_terms( $product->id, 'pa_unit', array( 'fields' => 'names' ) ) );
$unit_number_extracted = preg_replace('/[^0-9]/', '', $unit);
if ( (!empty($unit) ) AND $unit_number_extracted == 1 ) {
$unit_price = sprintf( __( 'Gross unit price: %s', 'loremipsum' ), wc_price($price * $unit_number_extracted) );
} else if( (!empty($unit) ) AND $unit_number_extracted > 1 ) {
$unit_price = sprintf( __( 'Gross unit price: %s', 'loremipsum' ), wc_price($price / $unit_number_extracted) );
}
$data['price_html'] .= '<span> ' . $unit_price . '</span>';
return $data;
}
add_filter( 'woocommerce_available_variation', 'product_variation_unit_price', 10, 3 );
I guess I should foreach the variations, but I don't know how to do that in this situation.
Thanks if you can help!

Reflect default price and strikethrough in mini cart item price in WooCommerce

Hello this code creates two discount classes, and then with a filter on the cart page it shows the regular price and the discounted price, how can I do to have the same visual result also on the mini cart??
this is what i am trying to achieve in the minicart
add_action( 'woocommerce_before_calculate_totals', 'bbloomer_quantity_based_pricing', 9999 );
function bbloomer_quantity_based_pricing( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
// Define discount rules and thresholds
$threshold1 = 2; // Change price if items > 100
$discount1 = 0.05; // Reduce unit price by 5%
$threshold2 = 3; // Change price if items > 1000
$discount2 = 0.1; // Reduce unit price by 10%
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item_key => $cart_item )
{
if( has_term( array('cartucce-inkjet'), 'product_cat', $cart_item['product_id'] ) ){
if ( $cart_item['quantity'] >= $threshold1 && $cart_item['quantity'] < $threshold2 )
{
$price = round( $cart_item['data']->get_price() * ( 1 - $discount1 ), 2 );
$cart_item['data']->set_price( $price );
}
elseif ( $cart_item['quantity'] >= $threshold2 )
{
$price = round( $cart_item['data']->get_price() * ( 1 - $discount2 ), 2 );
$cart_item['data']->set_price( $price );
}
}
}
}
function filter_woocommerce_cart_item_price( $price_html, $cart_item, $cart_item_key ) {
// Get the product object
$product = $cart_item['data'];
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Get reqular price
$regular_price = $product->get_regular_price();
// New price
$new_price = $cart_item['data']->get_price();
// NOT empty and NOT equal
if ( ! empty ( $regular_price ) && $regular_price != $new_price ) {
// Output
$price_html = '<del>' . wc_price( $regular_price ) . '</del> <ins>' . wc_price( $new_price ) . '</ins>';
}
}
return $price_html;
}
add_filter( 'woocommerce_cart_item_price', 'filter_woocommerce_cart_item_price', 10, 3 );

Edit Attribute Name in Additional Information tab on WooCommerce Product Page [duplicate]

Using "Reorder and customize product dimensions formatted output in WooCommerce" answer code il would like to make the output display as:
Size: D40 x W45 x H60 (cm)
Any help is appreciated.
Just add the follows code snippet in your active theme's functions.php to achieve the above -
function woocommerce_display_product_attributes( $product_attributes, $product ){
if( !isset( $product_attributes['dimensions'] ) ) return $product_attributes;
$modified_dimensions = array();
foreach ( $product->get_dimensions( false ) as $key => $value ) {
if( $key == 'length' )
$modified_dimensions[$key] = 'D'.$value;
if( $key == 'width' )
$modified_dimensions[$key] = 'W'.$value;
if( $key == 'height' )
$modified_dimensions[$key] = 'H'.$value;
}
$dimension_string = implode( ' × ', array_filter( array_map( 'wc_format_localized_decimal', $modified_dimensions ) ) );
if ( ! empty( $dimension_string ) ) {
$dimension_string .= ' (' . get_option( 'woocommerce_dimension_unit' ) . ')';
} else {
$dimension_string = __( 'N/A', 'woocommerce' );
}
// change dimensions label & value.
$product_attributes['dimensions']['label'] = __( 'Size', 'text-domain' );
$product_attributes['dimensions']['value'] = $dimension_string;
return $product_attributes;
}
add_filter( 'woocommerce_display_product_attributes', 'woocommerce_display_product_attributes', 99, 2 );
Welcome to StackOverflow!
Try a simpler code (tested):
add_filter( 'woocommerce_format_dimensions', 'custom_formated_product_dimentions', 10, 2 );
function custom_formated_product_dimentions( $dimension_string, $dimensions ){
if ( empty( $dimension_string ) )
return __( 'N/A', 'woocommerce' );
$dimensions = array_filter( array_map( 'wc_format_localized_decimal', $dimensions ) );
return 'D'.$dimensions['length'].' x W'.$dimensions['width'].' x H'.$dimensions['height'].' ('.get_option( 'woocommerce_dimension_unit' ).')';
}

Exclude user role from sale price in Woocommerce programmatically

the following code: Set product sale price programmatically in WooCommerce 3 works perfectly.
Also its continuation: Set programmatically product sale price and cart item prices in Woocommerce 3.
However I'd like to exclude an user role from this function altogether, how can I do that?
I added the following to the code above to no avail:
if ( ! wc_current_user_has_role( 'trader' ) ) return $product->get_regular_price();
Thanks
After asking around and trying it out, the following code manages to achieve what I need. For future reference if someone else needs it:
// Generating dynamically the product "regular price"
add_filter( 'woocommerce_product_get_regular_price', 'custom_dynamic_regular_price', 10, 2 );
add_filter( 'woocommerce_product_variation_get_regular_price', 'custom_dynamic_regular_price', 10, 2 );
function custom_dynamic_regular_price( $regular_price, $product ) {
if( empty($regular_price) || $regular_price == 0 )
return $product->get_price();
else
return $regular_price;
}
// Generating dynamically the product "sale price"
add_filter( 'woocommerce_product_get_sale_price', 'custom_dynamic_sale_price', 10, 2 );
add_filter( 'woocommerce_product_variation_get_sale_price', 'custom_dynamic_sale_price', 10, 2 );
function custom_dynamic_sale_price( $sale_price, $product ) {
$user = wp_get_current_user();
$rate = 0.9;
if( in_array( 'trader', (array) $user->roles ) ) {
return $product->get_regular_price() * $rate;
}
else
{
if( empty($sale_price) || $sale_price == 0 )
return $product->get_regular_price() * $rate;
else
return $sale_price;
}
};
// Displayed formatted regular price + sale price
add_filter( 'woocommerce_get_price_html', 'custom_dynamic_sale_price_html', 20, 2 );
function custom_dynamic_sale_price_html( $price_html, $product ) {
if( $product->is_type('variable') ) return $price_html;
$user = wp_get_current_user();
if( in_array( 'trader', (array) $user->roles ) ) {
$price_html = wc_price($product->get_regular_price() );
return $price_html;
}
else
{
$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();
return $price_html;
}
}
add_action( 'woocommerce_before_calculate_totals', 'set_cart_item_sale_price', 20, 1 );
function set_cart_item_sale_price( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Iterate through each cart item
foreach( $cart->get_cart() as $cart_item ) {
$price = $cart_item['data']->get_sale_price(); // get sale price
$cart_item['data']->set_price( $price ); // Set the sale price
}
}

Resources