使用 function.php 的单一产品上的 Woocommerce 元数据 [英] Woocommerce meta data on single-product using function.php

查看:25
本文介绍了使用 function.php 的单一产品上的 Woocommerce 元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力通过我的 functions.php 获取元数据以正确过滤.

I am struggling with getting the meta data to filter properly through my functions.php.

如果我在我的子主题中编辑 meta.php 没有问题,我可以让它工作:(简短的故事 - 但因为我使用 Avada 主题 - 他们在升级时更改了子主题的位置,我选择了 functions.php)

I can get this to work if I edit the meta.php in my child theme with no problem: (short story - but because I am using Avada Theme - and they change the location of the child theme on an upgrade, I am opting for the functions.php)

我目前正在尝试以这种方式过滤元数据:

I am currently trying to filter the meta this way:

add_filter ('woocommerce_product_meta_start','add_pet_info' );
function add_pet_info($pet_info) {
$string ="Test Text";
return $pet_info . $string; }

我想得到这个结果:但我就是无法让它工作.

And I would like to get this result: But I just cant get it to work.

知道我在这里缺少什么吗?

Any ideas what I am missing here?

谢谢!

推荐答案

woocommerce_product_meta_start 不是过滤器.这是一个动作.所以如果你返回一个值到is,什么都不会发生.这应该在 Woo 打印任何元之前打印出测试文本".

woocommerce_product_meta_start is not a filter. It's an action. So if you return a value to is, nothing is going to happen. This should print out "Test Text" before Woo prints any of its meta.

add_action('woocommerce_product_meta_start','add_pet_info' );
function add_pet_info($pet_info) {
    _e( "Test Text", "your-textdomain" );
}

不确定您尝试添加代码的确切位置,但请记住,您可以将其添加到 WooCommerce 中可用的任何钩子(或您的主题中可用的任何钩子).

Unsure of where exactly you are trying to add the code, but keep in mind that you can add it to any hook available in WooCommerce (or any hook available in your theme).

例如,要将测试文本放在产品元下方,您可以将上面的内容修改为以下内容:

For example to put the test text below the product meta you could modify the above to the following:

add_action('woocommerce_product_meta_end','add_pet_info' );
function add_pet_info($pet_info) {
    _e( "Test Text", "your-textdomain" );
}

任何您看到 do_action 的地方,您都会看到一个动作钩子,您可以通过 add_action() 将函数附加到该钩子上.

Anywhere you see a do_action you are looking at an action hook that you can attach functions to via add_action().

这篇关于使用 function.php 的单一产品上的 Woocommerce 元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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