在前端和后端(以及电子邮件)上解码产品自定义

I've got a prestashop setup that has a small 'customization form' that currently saves the information to the products default customization text input. I did this to save time on having to write a complete custom module to add additional customization form fields and such.

Currently all the inputs are serialized (json) and entered as a long string into the text input like this:

Client Customization: %5B%5B%7B%22name%22%3A%22trophy%5B1%5D%5Bline1%5D%22%2C%22engraving%22%3A%22Test%20Trophy%22%7D%2C%7B%22name%22%3A%22trophy%5B1%5D%5Bline2%5D%22%2C%22engraving%22%3A%22test%20trophy%22%7D%2C%7B%22name%22%3A%22trophy%5B1%5D%5Bline3%5D%22%2C%22engraving%22%3A%221111111%22%7D%5D%5D

On the front end - when the customized data is displayed I can use PHP to decode & display it appropriately.

Is there a way where I can change that globally somewhere so I don't have to try and find every place where it might display and add that PHP code?

I'm running into the issue that I can't seem to find where to add the PHP code to 'decode' that string for the emails that are being sent out - so the long ugly string is being seen instead of the nice few lines of customization the user entered.

Any thoughts on how to handle this? Is there a spot where I can globally assign the decoded string to the products customization?

You could either try the PaymentModule class to decode the string just before the emails are sent, or Product's method called "getAllCustomizedDatas" for a more "global" approach. And then test a lot, of course :)

Here's a quick draft of the second approach:

<?php
class Product extends ProductCore
{
    public static function getAllCustomizedDatas($id_cart, $id_lang = null, $only_in_cart = true, $id_shop = null)
    {
        $datas = parent::getAllCustomizedDatas($id_cart, $id_lang, $only_in_cart, $id_shop);

        /*
         * Iterate over $datas, you're looking for
         * [id_product][id_product_attribute][id_address_delivery][id_customization][datas]
         * Datas will contain an array of fields broken by their type. You can then decode
         * the ones that need to be decoded and return the result:
         */

        return $datas;
    }
}