How can i change the product name on a WooCommerce checkout page?

I tried using this code to change the name of the product on the checkout page and it does change the name but then my checkout page has a circle over the product display area and another circle over the credit card form that spins endlessly.

foreach ( $cart->get_cart() as $cart_item ) {
        // Only for the defined product ID (or variation ID)
        if ( in_array( $defined_product_id, [ $cart_item['product_id'], $cart_item['variation_id'] ] ) ) {
            $new_prod = strval( $_GET['new_product_name'] );
            $cart_item['data']->set_price( $new_price );
            $cart_item['data']->set_name( $new_prod );   
        }

Thanks,

I tested set_name( “abc” ); and that worked without the spinning but how do I get a value from the URL to work properly?

The issue you’re encountering might be related to the way you’re retrieving and using the $_GET parameter. If the value isn’t being correctly fetched or sanitized, it could be causing issues in the checkout process.

Here’s how you can update the code to properly retrieve the product name from the URL and ensure it doesn’t interfere with the checkout process:

php

Copy code

foreach ( $cart->get_cart() as $cart_item ) {
    // Only for the defined product ID (or variation ID)
    if ( in_array( $defined_product_id, [ $cart_item['product_id'], $cart_item['variation_id'] ] ) ) {
        // Ensure that the new product name is retrieved from the URL and properly sanitized
        if ( isset( $_GET['new_product_name'] ) && !empty( $_GET['new_product_name'] ) ) {
            $new_prod = sanitize_text_field( $_GET['new_product_name'] );
            $cart_item['data']->set_price( $new_price );
            $cart_item['data']->set_name( $new_prod );
        }
    }
}

Key Changes:

  1. Sanitizing Input: The sanitize_text_field() function is used to clean the input from the URL. This ensures that the value retrieved from $_GET['new_product_name'] is safe and does not contain harmful code or invalid characters.
  2. Check if Parameter Exists and is Not Empty: Before attempting to set the product name, we first check if the $_GET['new_product_name'] parameter exists and is not empty.

Possible Causes of the Spinning Issue:

  • Unvalidated Input: If the input from the URL isn’t properly validated or sanitized, it could cause issues with how the checkout page handles the product information.
  • JavaScript Errors: The spinning circles could indicate a JavaScript issue on the page, potentially triggered by invalid data or conflicts.
  • Caching Issues: If your site uses aggressive caching, the updated product name might not be reflecting properly, leading to the spinning circles. Clear your cache after making changes.

Test this updated code and see if it resolves the issue. If the spinning persists, inspect the browser console for any JavaScript errors, as they could provide more insight into the root cause.