在任何地方添加 prefix/suffix 到 Woocommerce 产品名称,包括购物车和电子邮件
Add a prefix/suffix to Woocommerce product name everywhere, including cart and email
我正在使用 Woocommerce 并且我已经集成了一些自定义字段以允许用户指定新值,我稍后会附加到产品标题。
我正在使用 update_post_meta
/get_post_meta
来保存 new 信息。这部分工作正常。
然后我使用过滤器 woocommerce_product_title
来更新标题。此过滤器在使用 $product->get_title()
时工作正常,但在使用 $product->get_name()
时什么也不做,这不是问题,因为在某些地方我不想附加新信息。
我还为产品页面使用了过滤器 the_title
。
基本上我的代码如下所示,其中 return_custom()
将是根据产品 ID 构建新信息的函数。
function update_title($title, $id = null ) {
$prod=get_post($id);
if (empty($prod->ID) || strcmp($prod->post_type,'product')!=0 ) {
return $title;
}
return $title.return_custom($id);
}
function update_product_title($title, $product) {
$id = $product->get_id();
return $title.return_custom($id);
}
add_filter( 'woocommerce_product_title', 'update_product_title', 9999, 2);
add_filter( 'the_title', 'update_title', 10, 2 );
将产品添加到购物车时出现问题。使用的名称是默认名称,因此我的以下代码不足以更新购物车中使用的产品名称。通知邮件也一样。我想这是合乎逻辑的,因为电子邮件将使用购物车的信息。
我很确定 add_to_cart()
里面发生了一切,但我找不到任何与产品名称相关的 filter/hook。
如何确保购物车中使用的名称是正确的? filter/hook 除了我已经在使用的信息之外,我还应该考虑什么才能将我的新信息附加到购物车中的产品标题?
我想确保在整个购物过程中都能看到新标题。从产品页面到通知邮件。
尝试使用 woocommerce_cart_item_name
过滤器。
[woocommerce_cart]
简码使用cart/cart.php
模板,显示标题的代码是这样的:
if ( ! $product_permalink ) {
echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key ) . ' ' );
} else {
echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', sprintf( '<a href="%s">%s</a>', esc_url( $product_permalink ), $_product->get_name() ), $cart_item, $cart_item_key ) );
}
以下将允许您自定义购物车、结帐、订单和电子邮件通知中的产品名称,只需一个挂钩函数:
// Just used for testing
function return_custom( $id ) {
return ' - (' . $id . ')';
}
// Customizing cart item name in cart, checkout, orders and email notifications
add_action( 'woocommerce_before_calculate_totals', 'set_custom_cart_item_name', 10, 1 );
function set_custom_cart_item_name( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Required since Woocommerce version 3.2 for cart items properties changes
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {
// Get the product name and the product ID
$product_name = $cart_item['data']->get_name();
$product_id = $cart_item['data']->get_id();
// Set the new product name
$cart_item['data']->set_name( $product_name . return_custom($product_id) );
}
}
代码继续在您的活动子主题(或活动主题)的 function.php 文件中。已测试并有效。
我正在使用 Woocommerce 并且我已经集成了一些自定义字段以允许用户指定新值,我稍后会附加到产品标题。
我正在使用 update_post_meta
/get_post_meta
来保存 new 信息。这部分工作正常。
然后我使用过滤器 woocommerce_product_title
来更新标题。此过滤器在使用 $product->get_title()
时工作正常,但在使用 $product->get_name()
时什么也不做,这不是问题,因为在某些地方我不想附加新信息。
我还为产品页面使用了过滤器 the_title
。
基本上我的代码如下所示,其中 return_custom()
将是根据产品 ID 构建新信息的函数。
function update_title($title, $id = null ) {
$prod=get_post($id);
if (empty($prod->ID) || strcmp($prod->post_type,'product')!=0 ) {
return $title;
}
return $title.return_custom($id);
}
function update_product_title($title, $product) {
$id = $product->get_id();
return $title.return_custom($id);
}
add_filter( 'woocommerce_product_title', 'update_product_title', 9999, 2);
add_filter( 'the_title', 'update_title', 10, 2 );
将产品添加到购物车时出现问题。使用的名称是默认名称,因此我的以下代码不足以更新购物车中使用的产品名称。通知邮件也一样。我想这是合乎逻辑的,因为电子邮件将使用购物车的信息。
我很确定 add_to_cart()
里面发生了一切,但我找不到任何与产品名称相关的 filter/hook。
如何确保购物车中使用的名称是正确的? filter/hook 除了我已经在使用的信息之外,我还应该考虑什么才能将我的新信息附加到购物车中的产品标题?
我想确保在整个购物过程中都能看到新标题。从产品页面到通知邮件。
尝试使用 woocommerce_cart_item_name
过滤器。
[woocommerce_cart]
简码使用cart/cart.php
模板,显示标题的代码是这样的:
if ( ! $product_permalink ) {
echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key ) . ' ' );
} else {
echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', sprintf( '<a href="%s">%s</a>', esc_url( $product_permalink ), $_product->get_name() ), $cart_item, $cart_item_key ) );
}
以下将允许您自定义购物车、结帐、订单和电子邮件通知中的产品名称,只需一个挂钩函数:
// Just used for testing
function return_custom( $id ) {
return ' - (' . $id . ')';
}
// Customizing cart item name in cart, checkout, orders and email notifications
add_action( 'woocommerce_before_calculate_totals', 'set_custom_cart_item_name', 10, 1 );
function set_custom_cart_item_name( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Required since Woocommerce version 3.2 for cart items properties changes
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {
// Get the product name and the product ID
$product_name = $cart_item['data']->get_name();
$product_id = $cart_item['data']->get_id();
// Set the new product name
$cart_item['data']->set_name( $product_name . return_custom($product_id) );
}
}
代码继续在您的活动子主题(或活动主题)的 function.php 文件中。已测试并有效。