通过WooCommerce中的过滤器更改相关产品名称
问题描述:
我的WooCommerce产品设置为按照需要将产品数据提供给Jet.com。这是类似的:通过WooCommerce中的过滤器更改相关产品名称
Product Name - A bunch of attributes - A bunch of other descriptors - Etc.
但我不希望产品的长表格标题出现在网站上他们在Jet.com上的方式。
因此,使用woocommerce_cart_item_name
过滤器,我创建了一小段代码来抓取产品的第一个主要名称Product Name
,并在结帐时显示该购物车(它是购物车/结帐页面的组合)。
我无法弄清的是如何在购物车/结帐页上出现的相关产品(加售和交叉销售)。 WooCommerce文档似乎没有特定的过滤器,例如woocommerce_related_item_name
。
如何修改相关产品文本的方式与购物车中的相同?
欲了解更多的上下文,这里是工作车名称截断代码,我想要与追加销售/交叉销售相同。
// Display only short product name on website, leaving long product name for Jet.com to consume
add_filter('woocommerce_cart_item_name', 'shorten_woo_product_title', 10, 2);
function shorten_woo_product_title($title, $cart_item, $cart_item_key) {
$_product = $cart_item['data'] ;
if (is_checkout() || is_shop()) {
$title = $_product->get_title();
// Use as the product name the characters up to but not including the first dash character
$n = 1; // 1st dash
$pieces = explode(' - ', $title); // Break up the title into an array delimited by the "space dash space" characters
$shortname = implode(' - ', array_slice($pieces, 0, $n)); // Grab the short name in front of the first dash character
return $shortname; // Return it back
} else {
return $title; // Give the full product name
}
}
答
改为使用Wordpress原生wp_title过滤器。它可以影响网站中的所有标题。所以你只需添加product_type条件,让它只影响产品。
function change_product_titles($title, $id = null) {
$prod=get_post($id);
if (!empty($prod->ID) and $prod->post_type=='product') {
return $title.'blablabla';
}
return $title;
}
add_filter('the_title', 'change_product_titles', 10, 2);
谢谢@ Elvin85。使用wp_title在我们的例子中不起作用。我们的商店没有使用传统商店设置和产品页面作为动态插入WooCommerce产品内容的帖子。那些是静态的。所以我们唯一能够以WooCommerce的方式动态显示产品名称的地方就是结账。 get_post($ id)不适用于此,因此使用wp_title只会使购物车页面变为空白。 –