问题描述
目标是在商品传递到我们的支付网关时更改商品名称,但保持原样以显示在我们的产品页面上.
The goal is to change the name of the item as it gets passed to our payment gateway, but leave it as-is for display on our product pages.
我在我的functions.php中试过这个:
I've tried this in my functions.php:
function change_item_name( $item_name, $item ) {
$item_name = 'mydesiredproductname';
return $item_name;
}
add_filter( 'woocommerce_order_item_name', 'change_item_name', 10, 1 );
但它似乎对我不起作用.我觉得我应该传递一个实际的物品 ID 什么的……我有点迷茫.
But it doesn't seem to be working for me. I feel like I should be passing in an actual item ID or something… I'm a little lost.
任何有关我在这里做错的信息将不胜感激.
Any information on what I'm doing wrong here would be greatly appreciated.
推荐答案
woocommerce_order_item_name
过滤钩子是前端钩子,位于:
The woocommerce_order_item_name
filter hook is a front-end hook and is located in:
1) WooCommerce 模板:
- emails/plain/email-order-items.php
- templates/order/order-details-item.php
- templates/checkout/form-pay.php
- templates/emails/email-order-items.php
2)WooCommerce 核心文件:
- includes/class-wc-structured-data.php
他们每个人都有 $item_name 共同的第一个参数,其他参数不同.
有关详细信息,请参阅此处……
您在函数中设置了 2 个参数(第二个参数不适用于所有模板)并且您在钩子中只声明了一个.我已经测试了下面的代码:
You have 2 arguments set in your function (the second one is not correct for all templates) and you are declaring only one in your hook. I have tested the code below:
add_filter( 'woocommerce_order_item_name', 'change_orders_items_names', 10, 1 );
function change_orders_items_names( $item_name ) {
$item_name = 'mydesiredproductname';
return $item_name;
}
它适用于
- 订单接收(谢谢)页面,
- 电子邮件通知
- 和我的帐户订单 > 单个订单详情
但不在购物车、结帐和后端订单编辑页面中.
所以如果你需要让它在 Cart 和 Checkout 上工作,你应该使用其他钩子,比如 woocommerce_before_calculate_totals
.
然后你可以使用 WC_Product 方法
(setter和吸气剂).
这是您的新代码
add_action( 'woocommerce_before_calculate_totals', 'custom_cart_items_prices', 10, 1 );
function custom_cart_items_prices( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {
// Get an instance of the WC_Product object
$product = $cart_item['data'];
// Get the product name (Added Woocommerce 3+ compatibility)
$original_name = method_exists( $product, 'get_name' ) ? $product->get_name() : $product->post->post_title;
// SET THE NEW NAME
$new_name = 'mydesiredproductname';
// Set the new name (WooCommerce versions 2.5.x to 3+)
if( method_exists( $product, 'set_name' ) )
$product->set_name( $new_name );
else
$product->post->post_title = $new_name;
}
}
代码位于活动子主题(或主题)的任何 php 文件或任何插件 php 文件中.
Code goes in any php file of your active child theme (or theme) or also in any plugin php file.
现在除了商店档案和产品页面之外,您已经更改了所有地方的名称......
此代码经过测试,适用于 WooCommerce 2.5+ 和 3+
This code is tested and works on WooCommerce 2.5+ and 3+
如果您希望仅在购物车中保留原始商品名称,您应该添加此 条件 WooCommerce 标签 函数内:
if( ! is_cart() ){
// The code
}
此答案已于 2017 年 8 月 1 日更新,以获得与之前版本的 woocommerce 兼容性......
This answer has been updated on August 1st 2017 to get a woocommerce compatibility prior versions…
这篇关于更改 WooCommerce 购物车项目名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!