如何解决从WooCommerce中的apply_filters'prefix_xml_feeds_productname_variant'函数获取数据
我对WordPress和WooCommerce还是陌生的,所以为解释不当道歉。
我有此代码:
$text = apply_filters( 'prefix_xml_feeds_productname_variant',$text,$product_item->ID,$vars->ID );
并且需要在我的函数中显示$vars->ID
:
所以我得到了
function custom_product_name() {
global $product;
$product_name = $product->get_name();
$sku = $product->get_sku();
$text = 'Example.com ' . $sku . ' ' . $product_name . ' ' . $vars->ID;
return $text;
}
add_filter( 'prefix_xml_feeds_productname_variant','custom_product_name' );
如何在回调函数中访问$vars
变量值?
解决方法
您会看到apply_filters( 'prefix_xml_feeds_productname_variant',$text,$product_item->ID,$vars->ID );
包含3个参数
所以您可以像使用它
function custom_product_name( $text,$product_id,$vars_id ) {
// Get product object
$product = wc_get_product( $product_id );
// Is a product
if ( is_a( $product,'WC_Product' ) ) {
// Get product name
$product_name = $product->get_name();
// Get product sku
$sku = $product->get_sku();
// Output
$text = 'Example.com ' . $sku . ' ' . $product_name . ' ' . $vars_id;
}
return $text;
}
add_filter( 'prefix_xml_feeds_productname_variant','custom_product_name',10,3 );
更多信息:https://docs.woocommerce.com/document/introduction-to-hooks-actions-and-filters/
使用滤清器钩
在整个代码中使用apply_filter( 'filter_name',$variable );
调用过滤器挂钩。要操作传递的变量,您可以执行以下操作:
add_filter( 'filter_name','your_function_name' );
function your_function_name( $variable ) {
// Your code
return $variable;
}
使用过滤器,您必须返回一个值。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。