如何以编程方式在woocommerce中到处更改产品名称?

埃菲

我正在尝试美化我的产品名称,并且需要将更改应用到所有地方(目录,购物车,结帐,小部件...)等

实际上,我设法:

目录(循环)单个产品:

add_filter('the_title', 'new_title', 10, 2);
function new_title($title, $id) {
    if('product' == get_post_type($id)){
        $title = rename_woo_product($title, $id); // My function to rename
    }
    return $title;
}

在产品标题标签和酵母中:

add_filter( 'pre_get_document_title', 'generate_custom_title', 10 );
add_filter('wpseo_title', 'generate_custom_title', 15);
function generate_custom_title($title) {
    if(  is_singular( 'product') ) {
        $title = get_the_title();
    }
    return $title;
}

购物车和结帐:

add_filter( 'woocommerce_cart_item_name', 'custom_variation_item_name', 10, 3 );
function custom_variation_item_name( $item_name,  $cart_item,  $cart_item_key ){
    $product_item = $cart_item['data'];

    $item_name = get_the_title( $cart_item['product_id'] );

    if(!empty($product_item) && $product_item->is_type( 'variation' ) ) {
        $item_name = $item_name . '<br>' . $cart_item['data']->attribute_summary;
    }

    if(is_cart()){
        $item_name = sprintf( '<a href="%s">%s</a>', esc_url( $cart_item['data']->get_permalink() ), $item_name );
    }

    return $item_name;
}

我不知道这是否是最好的方法,但是它可以做到这一点。但是例如,我使用woocommerce的“最近查看的产品”小部件,并且产品标题未更新。

有没有更好的方法来更新产品名称并将其应用于任何地方?

LoicTheAztec

您也可以使用复合钩子,woocommerce_product_get_name例如:

add_filter('woocommerce_product_get_name', 'filter_wc_product_get_name', 10, 2); // For "product" post type
add_filter('woocommerce_product_variation_get_name', 'filter_wc_product_get_name', 10, 2); // For "product_variation" post type
function filter_wc_product_get_name( $name, $product ){
    if ( ! is_admin() ) {
        $name = rename_woo_product($name, $product->get_id());
    }
    return $name;  
}

它应该替换您的自定义函数,该函数挂在woocommerce_cart_item_name过滤器挂钩上。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Xcode中更改产品名称后,“ ld:文件未找到”

更改相关产品标题,在WooCommerce中添加产品名称

在Woocommerce中获取产品名称

如何在API中显示产品名称?

如何在WooCommerce“我的帐户”订单表的新列中显示产品名称

WooCommerce:如何在管理产品列表中的产品名称旁边添加产品类型

更改WooCommerce中生成的可变产品的产品名称

在Woocommerce存档页面的产品名称中添加产品标签

产品名称WooCommerce的联系表7

在简短说明之前更改WooCommerce单一产品名称的位置

选择属性时,将WooCommerce产品名称更改为包括属性

将woocommerce产品名称更改为h2

选择属性时更改WooCommerce产品名称包括颜色

在Woocommerce谢谢页面中获取数组中的产品名称

WooCommerce:在购物车中的产品名称下移动价格和小计

创建一个在Woocommerce中显示简单产品名称列表的简码

在WooCommerce电子邮件模板中获取产品名称和描述

使用woocommerce中的表达式获取产品名称

将产品名称添加到WooCommerce中的电子邮件主题

在Woocommerce订单电子邮件通知中获取产品名称或ID

在Woocommerce订单详细信息中显示父分组产品名称

在Woocommerce我的帐户订单上的新列中显示产品名称和数量

在WooCommerce管理订单中添加显示产品名称的列时出现的问题

如何选择和显示数组中的产品,并按相同的产品名称排序

如何从产品名称中提取品牌

如何使Opencart 1.5.6 url使用产品名称

识别产品名称中是否存在符号

在 html 文本中查找产品名称

可以从模型中获取产品名称