0
votes

i have two codes to change the product name on my woocommerce variable product. The first one works fine everywhere, but not in my "floating cart plugin" (it use the default product name).

    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;

    foreach ( $cart->get_cart() as $cart_item ) {

        $product = $cart_item['data'];
        $modell = $product->get_attribute('pa_modell');
        $reparatur = $product->get_attribute('pa_reparatur');

        $original_name = method_exists( $product, 'get_name' ) ? $product->get_name() : $product->post->post_title;

        $new_name = '<class="new-product-name">' . $original_name . __( " ", "woocommerce") . $modell . __( " - ", "woocommerce") . $reparatur;

        if( method_exists( $product, 'set_name' ) )
            $product->set_name( $new_name );
        else
            $product->post->post_title = $new_name;
    }
}

The second one are working on my "floating cart plugin", but if i use it too, i have the same name 2 times on cart and checkout page table.

    function show_model_in_cart_items( $item_name, $cart_item, $cart_item_key ) {

    $product = $cart_item['data'];
    $modell = $product->get_attribute('pa_modell');
    $reparatur = $product->get_attribute('pa_reparatur');

    {
        $item_name = '<class="product-model">' . $item_name . __( " ", "woocommerce") . $modell . __( " - ", "woocommerce") . $reparatur;
    } 

    return $item_name;
}
add_filter( 'woocommerce_cart_item_name', 'show_model_in_cart_items', 99, 3 );

Some parts of this codes are found on the internet, others are from myself. Does anyone has an idea how to combine it so that it works anywhere without the same name 2 times in cart?

1

1 Answers

0
votes

I've found a solution. And sometimes it's so simple... like a simple if and else.

add_filter( 'woocommerce_cart_item_name', 'show_model_in_cart_items', 99, 3 );
function show_model_in_cart_items( $item_name, $cart_item, $cart_item_key ) {

       if ( is_checkout() ) {
       return $item_name;
       }
       else {
    $product = $cart_item['data'];
    $modell = $product->get_attribute('pa_modell');
    $reparatur = $product->get_attribute('pa_reparatur');

    {
        $item_name = '<class="reparatur-beschreibung">' . $item_name . __( " ", "woocommerce") . $modell . __( " - ", "woocommerce") . $reparatur;
    } 
        return $item_name;
}}

Now the floating cart plugin can use it anywhere, except the checkout where i have the other code to correct the product title.