从 WooCommerce 中的 apply_filters('prefix_xml_feeds_productname_variant') 函数获取数据
Get data from apply_filters('prefix_xml_feeds_productname_variant') function in WooCommerce
我是 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;
}
使用过滤器,您必须 return 一个值。
我是 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;
}
使用过滤器,您必须 return 一个值。