问题
I'm trying to append text to WooCommerce product title in the order meta - if products has a specific tag. I'm working from
"Append text to product title if product has product-tag on cart in WooCommerce"
"Display custom payment field in Woocommerce Admin, Orders and emails"
This is what I have so fare:
add_filter( 'woocommerce_get_order_item_totals', 'add_udstilling_below_cart_item_name', 10, 3 );
function add_udstilling_below_cart_item_name( $total_rows, $order, $tax_display ) {;
$new_total_rows = [];
foreach($total_rows as $key => $total ){
$new_total_rows[$key] = $total;
}
}
return $new_total_rows;
}
回答1:
Based on your previous question and this new question, I assume that you would like to see the product title adjusted 'everywhere'.
IMPORTANT!! You should definitely read the following post to understand that this is a general adjustment, so that you ultimately no longer need the code from your previous post / question
Changing WooCommerce cart item names
In other words, you can start using the code below to see the adjustment in the following places
- Order-receive (Thank you) page,
- email notifications
- My Account Orders> Single order details
- Cart & Checkout and Backend Order edit pages.
Now you have change the names everywhere except on Shop archives and product pages…
function add_udstilling_order_item_name( $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 product id
$product_id = $cart_item['product_id'];
if( method_exists( $product, 'set_name' ) && has_term( 'udstillingsmodel', 'product_tag', $product_id ) ) {
$product->set_name( $product->get_name() . '(test)' );
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'add_udstilling_order_item_name', 10, 1 );
来源:https://stackoverflow.com/questions/60247482/append-text-to-product-title-on-multiple-pages-in-woocommerce