Home > Back-end >  How to change product name in the cart in Prestashop?
How to change product name in the cart in Prestashop?

Time:10-10

I want to change the names of some products in the cart. I was able to change the price of some products in the cart. But i can't change the names of some products in the cart.

How can i do that? Is it possible. Thanks for helps.

For your better understanding, below is some of the result from cart->getProducts() action.

enter image description here

CodePudding user response:

The name of the products, as long as they are in the cart, is read from the product object, so you will have to override the cart->getProducts() method if you want to change their names dynamically.

But keep in mind that once a cart becomes an order, product name is copied / stored inside the orderDetail object, where you can freely intervene by renaming the "product_name" field once you know the original id_cart / id_order.

CodePudding user response:

I solved the problem with the code below. If you encounter this problem, you can edit the code below for yourself. And you must do these actions after validateOrder function or validated the order.

        $order = Order::getByCartId($cart->id);

        $order_details = OrderDetail::getList($order->id);


        foreach ($order_details as $order_detail) {
            if ($order_detail['product_name'] === 'Installment' && (string)$order_detail['product_price'] == (string)$installment_fee) {
                $order_detail_id = $order_detail['id_order_detail'];
            }
        }

        if (!is_null($order_detail_id)) {
            $order_detail = new OrderDetail($order_detail_id);
            $order_detail->product_name = 'Changed product name';
            $order_detail->save();
        }
  • Related