Jump to content

Edit History

jat

jat

J'ai créé un module, qui ajoute cette colonne et les filtres correspondants.

<?php
/**
 * Create the field `status_customer` in the `customer` table.
 * After install clean prestashop cache
 * Types of customers: Professional or private.
 * Tipos de clientes: Autonomo au particular
 * Types de clients: Professionnel / Particulier
 */

use PrestaShop\PrestaShop\Core\Domain\Customer\Exception\CustomerException;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use PrestaShop\PrestaShop\Core\Grid\Column\Type\DataColumn;
use Symfony\Component\Translation\TranslatorInterface;
use PrestaShop\PrestaShop\Core\Grid\Column\Type\Common\ToggleColumn;
use PrestaShop\PrestaShop\Core\Grid\Definition\GridDefinitionInterface;
use PrestaShop\PrestaShop\Core\Grid\Filter\Filter;
use PrestaShop\PrestaShop\Core\Search\Filters\CustomerFilters;


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

class TypeCustomers extends Module
{
    
    private $translator;

    public function __construct()
    {
        $this->name = 'typecustomers';
        $this->tab = 'Customer';
        $this->version = '1.0.0';
        $this->author = 'Jatniel Guzmán';
        $this->need_instance = 0;
        $this->bootstrap = true;
        //$this->translator = $translator;

        parent::__construct();

        $this->displayName = $this->getTranslator()->trans(
            'Type Customers',
            [],
            'Modules.Typecustomers.Admin'
        );

        $this->description =
            $this->getTranslator()->trans(
                'Type Customers List',
                [],
                'Modules.Typecustomers.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')
            // && //
            ;
    }

    /**
     * Désinstallation du module
     * @return boolean
     */
    public function uninstall()
    {
        return parent::uninstall();
    }


    /**
     * Hook allows to modify Customers grid definition.
     * This hook is a right place to add/remove columns or actions (bulk, grid).
     *
     * @param array $params
     */
    public function hookActionCustomerGridDefinitionModifier(array $params)
    {
        /** @var GridDefinitionInterface $definition */
        $definition = $params['definition'];

        $definition
            ->getColumns()
            ->addAfter(
                'optin',
                (new DataColumn('status_customer'))
                    ->setName($this->getTranslator()->trans('Type client', [], 'Modules.Typecustomers.Admin'))
                    ->setOptions([
                        'field' => 'status_customer',

                    ])
            );
        $definition->getFilters()->add(
            (new Filter('status_customer', ChoiceType::class))
                ->setTypeOptions([
                    'choices' => $this->getChoices(),
                    'expanded' => false,
                    'multiple' => false,
                    'required' => false,
//                    'choice_translation_domain' => false,
                ])
                ->setAssociatedColumn('status_customer')
        );
    }

    public function getChoices()
    {
        $choices = array("Professionnel" => "professionnel", "Particulier" => "particulier");
        return $choices;
    }

    /**
     * Hook allows to modify Customers query builder and add custom sql statements.
     *
     * @param array $params
     */
    public function hookActionCustomerGridQueryBuilderModifier(array $params)
    {
        /** @var QueryBuilder $searchQueryBuilder */
        $searchQueryBuilder = $params['search_query_builder'];

        /** @var CustomerFilters $searchCriteria */
        $searchCriteria = $params['search_criteria'];

        $searchQueryBuilder->addSelect('(c.`status_customer`) AS `status_customer`');

        if ('customer_group' === $searchCriteria->getOrderBy()) {
            $searchQueryBuilder->orderBy('gt.`name`', $searchCriteria->getOrderWay());
        }

        foreach ($searchCriteria->getFilters() as $filterName => $filterValue) {

            if ('status_customer' === $filterName) {
                $searchQueryBuilder->andWhere('c.status_customer = :' . $filterName);
                $searchQueryBuilder->setParameter($filterName, $filterValue);

                continue;
            }
        }
    }

}

 

Je dois encore modifier l'édition client dans le back-office, pour pouvoir modifier le status_customer.

editionCustomer.png.7073e29d43fb435c4b56cdb0695a1f90.png

 

Vous avez des idées ou des pistes ?

×
×
  • Create New...