Jump to content

How to add, remove or edit column to customer information table.


Skuiz

Recommended Posts

Good day,

I am currently using prestashop 1.7.7.4.
There is a field on the table from backoffice if we go to  customer > customer that its social title.
I was able to remove it from the edit user fields if i delete it from the customer config >titles.
Is there a way to remove it from the customer table grid?

I want to add a new column on the same table, so i was checking if its possible to edit the social title one and add mine isntead.

Have those two options. already did some research and find a helpful document on dev docs from prestashop about using Hooks, but its not very clear to me since im new to using prestashop and it doesnt tell you very clear where to edit on the php files or where to use the code.

If someone is able to guide me or help me to add that new field it would be great.

Thanks for having the time to read this and i hope you have a great day.

Link to comment
Share on other sites

Good morning,

I would like to Remove Social Title column from grid and also add a new one that is named CustomerID.

Since its a field i have for the customer invoices needed for informative and fast access to them on the same customer grid table.

Link to comment
Share on other sites

Quick update:

After doing some research, i was able to find some information to install a module using hooks.

This is the example i found:

<?php
/**
 * After install clean prestashop cache
 */

use PrestaShop\PrestaShop\Core\Domain\Customer\Exception\CustomerException;
use Symfony\Component\Form\Extension\Core\Type\TextType;

if (!defined('_PS_VERSION_')) {
    exit;
}

class ps_customercedula extends Module
{
    // const CLASS_NAME = 'ps_customercedula';

    public function __construct()
    {
        $this->name = 'ps_customercedula';
        $this->version = '1.0.0';
        $this->author = 'wfpaisa';
        $this->need_instance = 0;

        parent::__construct();

        $this->displayName = $this->getTranslator()->trans(
            'Campo Raro',
            [],
            'Modules.ps_customercedula.Admin'
        );

        $this->description =
            $this->getTranslator()->trans(
                'Customer cedula',
                [],
                'Modules.ps_customercedula.Admin'
            );

        $this->ps_versions_compliancy = [
            'min' => '1.7.6.0',
            'max' => _PS_VERSION_,
        ];
    }
    /**
     * This function is required in order to make module compatible with new translation system.
     *
     * @return bool
     */
    public function isUsingNewTranslationSystem()
    {
        return true;
    }

    /**
     * Install module and register hooks to allow grid modification.
     *
     * @see https://devdocs.prestashop.com/1.7/modules/concepts/hooks/use-hooks-on-modern-pages/
     *
     * @return bool
     */
    public function install()
    {
        return parent::install() &&
            $this->registerHook('actionCustomerGridDefinitionModifier') &&
            $this->registerHook('actionCustomerGridQueryBuilderModifier') &&
            $this->registerHook('additionalCustomerFormFields') &&
            $this->registerHook('actionCustomerFormBuilderModifier') &&
            $this->registerHook('actionAfterCreateCustomerFormHandler') &&
            $this->registerHook('actionAfterUpdateCustomerFormHandler') &&
            $this->alterCustomerTable()
        ;
    }

    public function uninstall()
    {
        return parent::uninstall() && $this->uninstallAlterCustomerTable();
    }

    /**
     * Alter customer table, add module fields
     *
     * @return bool true if success or already done.
     */
    protected function alterCustomerTable()
    {
        $sql = 'ALTER TABLE `' . pSQL(_DB_PREFIX_) . 'customer` ADD `cedula` VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL';
        // CLEAN_INSTALATION 1/2  (if you want to delete all data after an installation)
        // comment:
        Db::getInstance()->execute($sql);
        return true;
        // and uncomment:
        // return Db::getInstance()->execute($sql);
    }

    /**
     * Uninstalls sample tables required for demonstration.
     *
     * @return bool
     */
    private function uninstallAlterCustomerTable()
    {
        // CLEAN_INSTALATION 2/2 (if you want to delete all data after an installation)
        // uncomment:
        // $sql = 'ALTER TABLE `' . pSQL(_DB_PREFIX_) . 'customer` DROP `cedula`';
        // return Db::getInstance()->execute($sql);
        // 
        // and comment:
        return true;
    }


    /**
     * Hook allows to modify Customers form and add additional form fields as well as modify or add new data to the forms.
     * FRONT_END
     * @param array $params
     */
    public function hookAdditionalCustomerFormFields($params)
    {
        return [
            (new FormField)
                ->setName('cedula')
                ->setType('text')
                ->setRequired(true) // is Required
                ->setLabel($this->l('Cédula'))
        ];
    }


    /**
     * Hook allows to modify Customers form and add additional form fields as well as modify or add new data to the forms.
     * BACK_END
     * @param array $params
     */
    public function hookActionCustomerFormBuilderModifier(array $params)
    {
        /** @var FormBuilderInterface $formBuilder */
        $formBuilder = $params['form_builder'];
        $formBuilder->add('cedula', TextType::class, [
            'label' => $this->getTranslator()->trans('Cedula', [], 'Modules.ps_customercedula.Admin'),
            'required' => false,
        ]);
        
        $customer = new Customer($params['id']);
        $params['data']['cedula'] = $customer->cedula;
        
        $formBuilder->setData($params['data']);

    }


    /**
     * Hook allows to modify Customers form and add additional form fields as well as modify or add new data to the forms.
     *
     * @param array $params
     *
     * @throws CustomerException
     */
    public function hookActionAfterUpdateCustomerFormHandler(array $params)
    {
        $this->updateCustomerCedula($params);
    }

    /**
     * Hook allows to modify Customers form and add additional form fields as well as modify or add new data to the forms.
     *
     * @param array $params
     *
     * @throws CustomerException
     */
    public function hookActionAfterCreateCustomerFormHandler(array $params)
    {
        $this->updateCustomerCedula($params);
    }

    /**
     * Update / Create 
     * 
     * @param array $params
     *
     * @throws \PrestaShop\PrestaShop\Core\Module\Exception\ModuleErrorException
     */
    private function updateCustomerCedula(array $params)
    {
        $customerId = (int)$params['id'];
        /** @var array $customerFormData */
        $customerFormData = $params['form_data'];
        $cedula = $customerFormData['cedula'];
        
        try {

            $customer = new Customer($customerId);
            $customer->cedula= $cedula;
            $customer->update();

        } catch (ReviewerException $exception) {
            throw new \PrestaShop\PrestaShop\Core\Module\Exception\ModuleErrorException($exception);
        }
    }

}

So with this im able to add the CustomerID to the customer Edit Form, the only thing left its to add it to the customer grid table but im not able to do it.

i just need to know how to add this here with a hook, its supposed to be a normal text data display.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...