hook into woocommerce_variation_price_html wordpre

2019-09-18 04:08发布

问题:

I am working on a plugin for woocommerce and came across the hook "woocommerce_variation_price_html" which allows you to hook into the variation prices drop down on single products. So created a quick function to test and have a play:

add_action( 'woocommerce_variation_price_html' , 'wholesale_variation_price' );
function wholesale_variation_price($term){

 return '';

}

The above works nicely and removes all data. However i am trying to add custom meta_data to replace the default values.

So i then did the following:

add_action( 'woocommerce_variation_price_html' , 'wholesale_variation_price' );
function wholesale_variation_price($term){

$var_price = get_post_meta( get_the_id(), '_my_price', true );

 return $var_price;

}

This for some reason does not work? Has anyone had any experience working with this hook in woocommerce? There is not much documentation on that hook anywhere.

Any help would be greatly appreciated!

回答1:

As already indicated that you can read here about filters for html. Maybe you have Sale price set for variation? Then in link you can see that there are total of 4 filters. For: price, sale price, free and no price.

This code works

add_filter( 'woocommerce_variation_sale_price_html', 'my_html', 10, 2);
add_filter( 'woocommerce_variation_price_html', 'my_html', 10, 2);
function my_html( $price, $variation ) {
    return woocommerce_price(5);
}

Also this one works too. This allows you to modify whole variation. Read in code here

add_filter( 'woocommerce_available_variation', 'my_variation', 10, 3);
function my_variation( $data, $product, $variation ) {
    $data['price_html'] = woocommerce_price(6);
    return $data;
}

Screenshots:

First example

Second example

Works like magic!

P.S. you can see prices like this in screenshots because I have design that show them like that.


EDIT 2016-09-10

Version 2.6+

As woocommerce_price is deprecated in 2.6 then use wc_price for newer versions.



回答2:

// Display Price For Variable Product With Same Variations Prices
add_filter('woocommerce_available_variation', function ($value, $object = null, $variation = null) {
if ($value['price_html'] == '') {
    $value['price_html'] = '<span class="price">' . $variation->get_price_html() . '</span>';
}
return $value;}, 10, 3);


回答3:

By looking at WooCommerce source code, I see that woocommerce_variation_price_html is a filter, not an action. I don't know how you managed to get your sample code to work like that... weird.

Try this:

function wholesale_variation_price( $price, $product ) {
    $var_price = get_post_meta( $product->variation_id, '_my_price', true );
    return $var_price;
}
add_filter( 'woocommerce_variation_price_html', 'wholesale_variation_price', 10, 2 )


回答4:

I used this to modify the variation prices to show the price for each variation product excluding tax before and including tax after :)

add_filter( 'woocommerce_available_variation', 'my_variation', 10, 3);
function my_variation( $data, $product, $variation ) {
    $data['price_html'] = "<span class='ex-vat-price'>ex. " . woocommerce_price($variation->get_price_excluding_tax()) . "</span><br>";
    $data['price_html'] .= "<span class='inc-vat-price'>inc. " . woocommerce_price($variation->get_price_including_tax()) . "</span>";
    return $data;
}