挂钩 woocommerce_variation_price_html wordpress [英] hook into woocommerce_variation_price_html wordpress

查看:23
本文介绍了挂钩 woocommerce_variation_price_html wordpress的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 woocommerce 开发一个插件,并遇到了woocommerce_variation_price_html"这个钩子,它允许你挂钩单个产品的变化价格下降.所以创建了一个快速功能来测试和播放:

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 '';

}

上述效果很好,并删除了所有数据.但是我正在尝试添加自定义 meta_data 来替换默认值.

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

于是我做了以下事情:

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;

}

这出于某种原因不起作用?有没有人有在woocommerce中使用这个钩子的经验?任何地方都没有关于该钩子的太多文档.

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.

任何帮助将不胜感激!

推荐答案

如前所述,您可以阅读 此处 关于 html 过滤器.也许您已经为变化设置了销售价格?然后在链接中您可以看到总共有 4 个过滤器.对于:价格、促销价、免费和无价.

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.

此代码有效

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;
}

截图:

第一个例子

第二个例子

像魔术一样工作!

附言你可以在屏幕截图中看到这样的价格,因为我的设计是这样显示的.

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

编辑 2016-09-10

EDIT 2016-09-10

2.6+ 版

由于 woocommerce_price 在 2.6 中已弃用,因此对于较新的版本,请使用 wc_price.

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

这篇关于挂钩 woocommerce_variation_price_html wordpress的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆