Viewed   103 times

I had this code in function.php of my theme to display the percentage after price and it was working fine in WooCommerce v2.6.14.

But this snippet doesn't work anymore on WooCommerce version 3.0+.

How can I fix that?

Here is that code:

// Add save percent next to sale item prices.
add_filter( 'woocommerce_sale_price_html', 'woocommerce_custom_sales_price', 10, 2 );
function woocommerce_custom_sales_price( $price, $product ) {
    $percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 );
    return $price . sprintf( __(' Save %s', 'woocommerce' ), $percentage . '%' );
}

 Answers

1

Updated - 2019 (avoid rounding price issue) - 2017 (avoid NAN% percentage value)

woocommerce_sale_price_html hook has been replaced by a different hook in WooCommerce 3.0+, that has now 3 arguments (but not the $product argument anymore).

add_filter( 'woocommerce_format_sale_price', 'woocommerce_custom_sales_price', 10, 3 );
function woocommerce_custom_sales_price( $price, $regular_price, $sale_price ) {
    // Getting the clean numeric prices (without html and currency)
    $_reg_price = floatval( strip_tags($regular_price) );
    $_sale_price = floatval( strip_tags($sale_price) );

    // Percentage calculation and text
    $percentage = round( ( $_reg_price - $_sale_price ) / $_reg_price * 100 ).'%';
    $percentage_txt = ' ' . __(' Save ', 'woocommerce' ) . $percentage;

    $formatted_regular_price = is_numeric( $regular_price ) ? wc_price( $regular_price ) : $regular_price;
    $formatted_sale_price    = is_numeric( $sale_price )    ? wc_price( $sale_price )    : $sale_price;

    echo '<del>' . $formatted_regular_price . '</del> <ins>' . $formatted_sale_price . $percentage_txt . '</ins>';
}

This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
The code is tested and works. For WooCommerce version 3.0+ (thanks to @Mikebcn and @AsifRao)

For rounding the percentage you can use round(), number_format() or number_format_i18n():

$percentage = number_format_i18n( ( $_reg_price - $_sale_price ) / $_reg_price * 100, 0 ).'%';

$percentage = number_format( ( $_reg_price - $_sale_price ) / $_reg_price * 100, 0 ).'%';

Original answer code: Here is that functional similar code:

// Only for WooCommerce version 3.0+
add_filter( 'woocommerce_format_sale_price', 'woocommerce_custom_sales_price', 10, 3 );
function woocommerce_custom_sales_price( $price, $regular_price, $sale_price ) {
    $percentage = round( ( $regular_price - $sale_price ) / $regular_price * 100 ).'%';
    $percentage_txt = ' ' . __(' Save ', 'woocommerce' ) . $percentage;
    $price = '<del>' . ( is_numeric( $regular_price ) ? wc_price( $regular_price ) : $regular_price ) . '</del> <ins>' . ( is_numeric( $sale_price ) ? wc_price( $sale_price ) . $percentage_txt : $sale_price . $percentage_txt ) . '</ins>';
    return $price;
}

This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
The code is tested and works. For WooCommerce version 3.0+.

Thursday, December 15, 2022
2

Hooked functions will never override other hooked functions that are using the same action or filter hook.

They are added to a kind of "hook queue" with an execution order based on priority rules:

  • If a priority is specified, they will be ordered in the queue first by hook priority and by declaration priority.
  • If there is no priority specified, they take the default priority of 10 and they will be ordered in the queue by declaration.

So you can have many hooked functions on the same hook, like for example in the Woocommerce template file content-single-product.php

Illustrated example:

In the below commented code example, you can see the execution order in the hook queue for each hooked function for the woocommerce_thankyou action hook:

// No defined priority (default priority is 10)
add_action( 'woocommerce_thankyou', 'first_custom_function_no_priority' );
function first_custom_function_no_priority( $order_id ) {
    // ==> Triggered in third position ==> [3]
}

## Default Hook "woocommerce_order_details_table" (default priority is 10)
    // ==> Triggered in second position ==> [2]

// Defined priority is 10
add_action( 'woocommerce_thankyou', 'order_created_get_skus', 10 );
function order_created_get_skus( $order_id ) {
    // ==> Triggered in Fourth position ==> [4] 
}

// Defined priority is 5
add_action( 'woocommerce_thankyou', 'third_custom_function', 5 );
function third_custom_function( $order_id ) {
    // ==> Triggered in first position ==> [1]
}

// Defined priority is 20
add_action( 'woocommerce_thankyou', 'fourth_custom_function', 20 );
function fourth_custom_function( $order_id ) {
    // ==> Triggered at last (sixth) ==> [6]
}

// No defined priority (default priority is 10)
add_action( 'woocommerce_thankyou', 'last_custom_function_no_priority' );
function last_custom_function_no_priority( $order_id ) {
    // ==> Triggered in fifth position ==> [5]
}

Lower priority is executed (or triggered) before, higher priority is executed (or triggered) after. If no priority is specified, the default priority is 10.

The hooked functions can only be removed with remove_action() or remove_filter() with a mandatory defined priority.

To see how many hooked functions are hooked on a specific hook with all necessary details, you can use the following that will give you a raw output:

global $wp_filter;

// HERE below you define the targeted hook name
$hook_name = 'woocommerce_widget_shopping_cart_buttons';

if( isset($wp_filter[$hook_name]) ) {
    echo '<pre>';
    print_r($wp_filter[$hook_name]);
    echo '</pre>';
} else {
    echo '<p>Hook "'.$hook_name.'" is not used yet!</p>';
}

There is 2 kind of hooks, as you have noticed may be, which are filter hooks and action hooks.

  1. Action hook:

    • Action hook execution point (trigger): with do_action()
    • Attaching a function to an action hook (triggered): with add_action(): the function is executed and can have optional arguments.
  2. Filter hook:

    • Filter hook execution point (trigger): with apply_filters()
    • Attaching a function to a filter hook (filtering / triggered): with add_filter(): a mandatory argument (a variable) is filtered and returned from the "hooked" function

Hooks and their hooked functions can be located anywhere like in the function.php file of your active child theme (or active theme) and also in any plugins php files.


Related:

  • WooCommerce action hooks and overriding templates
  • How to add custom hooks to a custom plugin for Woocommerce
  • WordPress: Difference Between Filter and Action Hooks?
Tuesday, August 16, 2022
 
4

Update

First you could try one of this 2 alternatives (to add in your code):

1) The Wordpress way using WP_User Class:

// Get an instance of the WP_User Object
$user = new WP_User( $user_id );

// Get the first name and the last name from WP_User Object 
$first_name = $user->first_name;
$last_name  = $user->last_name;

2) The Woocommerce way using WC_Customer Class and methods:

// Get an instance of the WC_Customer Object
$customer = new WC_Customer( $customer_id );

// Get the first name and the last name from WC_Customer Object 
$first_name = $customer->get_first_name();
$last_name  = $customer->get_last_name();

Then looking at the WC_Customer_Data_Store source code there is an interesting action hook that could be used: woocommerce_new_customer that has a unique argument, the $customer_id.

In the source code $customer->get_first_name() and $customer->get_last_name() appear in the code, so they exist and you can get them from the WC_Customer instance Object using the $customer_id, as described here below…


Last possibility

As the user meta data seems to be delayed and Woocommerce seems to be using WordPress functions, you could try to use the WordPress hook add_user_meta which has 3 arguments:

  • $user_id
  • $meta_key
  • $meta_value

But I don't know how to include it in your code, hoping that you could get the data from WP_User Object or WC_Customer Object.

Tuesday, August 9, 2022
 
wxs
 
wxs
4

The filter woocommerce_sale_price_html doesn't exist anymore use woocommerce_get_price_html instead. This will run for everything regardless of of the item being on sale so you'll need to check if the product is on sale in your code.

add_filter( 'woocommerce_get_price_html', 'modify_woocommerce_get_price_html', 10, 2 );

function modify_woocommerce_get_price_html( $price, $product ) {
    if( $product->is_on_sale() && ! is_admin() )
        return $price . sprintf( __('<p>Save %s</p>', 'woocommerce' ), $product->regular_price - $product->sale_price );
    else
        return $price;
}
Friday, August 19, 2022
 
1

You will have to add your own settings in the "Settings section" inside the function. For the defined product attribute name, this function will take the first corresponding term set for the current product (as an attribute can have many terms in a product) and will display all similar products just before "Upsells products" and "Related products".

The code

add_action( 'woocommerce_after_single_product_summary', 'custom_output_product_collection', 12 );
function custom_output_product_collection(){

    ## --- YOUR SETTINGS --- ##

    $attribute = "Color"; // <== HERE define your attribute name
    $limit     = "3";     // <== Number of products to be displayed
    $cols      = "3";     // <== Number of columns
    $orderby   = "rand";  // <== Order by argument (random order here)

    ## --- THE CODE --- ##

    global $post, $wpdb;

    // Formatting the attribute
    $attribute = sanitize_title( $attribute );
    $taxonomy  = 'pa_' . $attribute;

    // Get the WP_Term object for the current product and the defined product attribute
    $terms = wp_get_post_terms( $post->ID, $taxonomy );
    $term = reset($terms);

    // Get all product IDs that have  the same product attribute value (except current product ID)
    $product_ids = $wpdb->get_col( "SELECT DISTINCT tr.object_id
        FROM {$wpdb->prefix}term_relationships as tr
        JOIN {$wpdb->prefix}term_taxonomy as tt ON tt.term_taxonomy_id = tr.term_taxonomy_id
        JOIN {$wpdb->prefix}terms as t ON tt.term_id = t.term_id
        WHERE tt.taxonomy LIKE '$taxonomy' AND t.term_id = '{$term->term_id}' AND tr.object_id != '{$post->ID}'" );

    // Convert array values to a coma separated string
    $ids = implode( ',', $product_ids );

    ## --- THE OUTPUT --- ##

    echo '<section class="'.$attribute.' '.$attribute.'-'.$term->slug.' products">
        <h2>'.__( "Collection", "woocommerce" ).': '.$term->name.'</h2>';

    echo do_shortcode("[products ids='$ids' columns='$cols' limit='$limit' orderby='$orderby']");

    echo '</section>';
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

Monday, August 8, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share