Changing WooCommerce cart item names

前端 未结 1 1221
遇见更好的自我
遇见更好的自我 2020-12-01 20:17

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.

I\'ve tried this in my func

相关标签:
1条回答
  • 2020-12-01 20:28

    The woocommerce_order_item_name filter hook is a front-end hook and is located in:

    1) WooCommerce templates:

    • 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 file:

    • includes/class-wc-structured-data.php

    Each of them has $item_name common first argument, and different for the other arguments.
    See Here for more details…

    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;
    }
    

    And it works on

    • Order-receive (Thank you) page,
    • email notifications
    • and My Account Orders > Single order details

    But NOT in Cart, Checkout and Backend Order edit pages.

    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 use WC_Product methods (setter and getters).

    Here is your new code

    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;
        }
    }
    

    Code goes in any php file of your active child theme (or theme) or also in any plugin php file.

    Now you have change the names everywhere except on Shop archives and product pages…

    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
    }
    

    This answer has been updated on August 1st 2017 to get a woocommerce compatibility prior versions…

    0 讨论(0)
提交回复
热议问题