更改WooCommerce购物车项目名称 [英] Changing WooCommerce cart item names
问题描述
目标是在商品传递到我们的付款网关时更改其名称,但将其保留原样显示在我们的产品页面上.
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 Core文件:
- 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;
}
它适用于
- 订单接收(谢谢)"页面,
- 电子邮件通知
- 和我的帐户订单">单个订单详细信息"
但不在购物车,结帐和后端订单编辑页面中.
因此,如果您需要使其在购物车和结帐上运行,则应使用其他挂钩,例如
woocommerce_before_calculate_totals
.
然后您可以使用
So if you need to make it work on Cart and Checkout, you should use other hooks like
woocommerce_before_calculate_totals
.
Then you can useWC_Product methods
(setter and getters).
这是您的新代码
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+
If you want to keep original item names in cart only you should add this conditional WooCommerce tag inside the function:
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购物车项目名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!