Jump to content

Edit History

Ray UK

Ray UK

HI @Vincent Decaux,

That seems to be an old post.

I have the fields both appearing in the orders list, and the customer edit page, I just can't get them to fill with data.

customer_profile.png.a99fc0bd43a35124a4c0bd2154777df3.png

It's probably the SQL statement that I cannot get right.

Here is my code now.

<?php

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

use PrestaShop\PrestaShop\Core\Grid\Column\Type\DataColumn;
use PrestaShop\PrestaShop\Core\Grid\Filter\Filter;
use Symfony\Component\Form\Extension\Core\Type\TextType;

class AgeCheck extends Module
{
	/**
     * List of hooks used
     */
    const HOOKS = [
        'actionCustomerGridDefinitionModifier',
        'actionCustomerGridQueryBuilderModifier',
        'actionCustomerFormBuilderModifier',
        'actionOrderGridDefinitionModifier',
        'actionOrderGridQueryBuilderModifier',
    ];
	
    public function __construct()
    {
        $this->name = 'agecheck';
        $this->tab = 'administration';
        $this->version = '1.0.0';
        $this->author = 'Ray Rigby';

        parent::__construct();

        $this->displayName = $this->trans('Age Check for Customer');
        $this->description = $this->trans('Add the extra B.O fields for the Age Verification records');
    }

    public function install()
    {
        return parent::install()
			&& $this->registerHook(static::HOOKS);
    }

    public function uninstall()
    {
        return parent::uninstall();
    }

	// ****************************** CUSTOMER LIST ****************************** 
	
	// Create new column in Customer List
    public function hookActionCustomerGridDefinitionModifier(array $params)
    {
				
        $definition = $params['definition'];

        $definition
            ->getColumns()
            ->addAfter(
                'email',
                (new DataColumn('agecheck'))
                    ->setName($this->l('18?'))
                    ->setOptions([
                        'field' => 'agecheck',
                    ])
		);
		
		/** Remove the following columns */
		$columns = $definition->getColumns();
		$columns->remove('social_title')
			->remove('active')
			->remove('optin')
			->remove('newsletter')
			;
		
		/** Remove the following filters */
		$filters = $definition->getFilters();
		$filters->remove('social_title')
			->remove('active')
			->remove('optin')
			->remove('newsletter')
			;

    }
	
	    /**
     * Add data to the new field in Customer List
     */
    public function hookActionCustomerGridQueryBuilderModifier(array $params)
    {
        $searchQueryBuilder = $params['search_query_builder'];

        $searchCriteria = $params['search_criteria'];
		
		$searchQueryBuilder->addSelect( 'c.`agecheck`' );
    }
	
	
		// ****************************** CUSTOMER PAGE ****************************** 
	
	// Create new column in Customer List
    public function hookActionCustomerFormBuilderModifier(array $params)
    {
    $formBuilder = $params['form_builder'];
		
    $allFields = $formBuilder->all();
    foreach ($allFields as $inputField => $input) {
        $formBuilder->remove($inputField);
    }
		
    foreach ($allFields as $inputField => $input) {
        $formBuilder->add($input);
        if ($inputField == 'email') {
            /** @var TextType::class \Symfony\Component\Form\Extension\Core\Type\TextType */
            $formBuilder->add(
                'agecheck', 
                 TextType::class, 
                 ['label' => 'Age Verified ?']
            );
			$formBuilder->add(
                'agecheckdate', 
                 TextType::class, 
                 ['label' => 'Date of Check']
            );
			
        }
     }
		
		$formBuilder->get('agecheck')->setRequired(false);
		$formBuilder->get('agecheckdate')->setRequired(false);
  }
	
	    
     // Add data to the new field in Customer List
    
    public function hookActionCustomerFormQueryBuilderModifier(array $params)
    {
        $searchQueryBuilder = $params['search_query_builder'];

        $searchCriteria = $params['search_criteria'];
		
		$searchQueryBuilder->addSelect( 'c.`agecheck`' );
		$searchQueryBuilder->addSelect( 'c.`agecheckdate`' );
    } 
	
	
	// ****************************** ORDER LIST ******************************

		// Create hook in Order List
    public function hookActionOrderGridDefinitionModifier(array $params)
    {
        /** @var GridDefinitionInterface $definition */
        $definition = $params['definition'];

        $definition
            ->getColumns()
            ->addAfter(
                'customer',
                (new DataColumn('agecheck'))
                    ->setName($this->l('18?'))
                    ->setOptions([
                        'field' => 'agecheck',
                    ])
		);
    }
	
		    /**
     * Add data to the new field in Order List
     */
    public function hookActionOrderGridQueryBuilderModifier( array $params ) {

		$searchQueryBuilder = $params['search_query_builder'];

        $searchCriteria = $params['search_criteria'];
		
		/** $searchQueryBuilder->addSelect( 'c.`agecheck`' );*/
		$searchQueryBuilder->leftJoin(
            'c',
            '`' . pSQL(_DB_PREFIX_) . 'customer`',
            'pa',
            'pa.`id_customer` = c.`id_customer`
                 = (
                    SELECT
                      pa2.`agecheck`
                    FROM
                      `'. pSQL(_DB_PREFIX_) .'customer` pa2
                    WHERE pa2.`id_customer` = pa.`id_customer`
                    ORDER BY LENGTH(pa2.`agecheck`) DESC
                    LIMIT 1
                )'
        );

    }

	


	
}

I'm not sure what the 'c', 'pa' & 'pa2' refer to, so I'm stuck there at the moment.

I'm also struggling to find the code to make this into a date picker field

$formBuilder->add(
                'agecheckdate', 
                 TextType::class, 
                 ['label' => 'Date of Check']
            );
			
        

 

Ray UK

Ray UK

HI @Vincent Decaux,

That seems to be an old post.

I have the fields both appearing in the orders list, and the customer edit page, I just can't get them to fill with data.

Its probably the SQL statement that i cant get right.

Here is my code now.

<?php

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

use PrestaShop\PrestaShop\Core\Grid\Column\Type\DataColumn;
use PrestaShop\PrestaShop\Core\Grid\Filter\Filter;
use Symfony\Component\Form\Extension\Core\Type\TextType;

class AgeCheck extends Module
{
	/**
     * List of hooks used
     */
    const HOOKS = [
        'actionCustomerGridDefinitionModifier',
        'actionCustomerGridQueryBuilderModifier',
        'actionCustomerFormBuilderModifier',
        'actionOrderGridDefinitionModifier',
        'actionOrderGridQueryBuilderModifier',
    ];
	
    public function __construct()
    {
        $this->name = 'agecheck';
        $this->tab = 'administration';
        $this->version = '1.0.0';
        $this->author = 'Ray Rigby';

        parent::__construct();

        $this->displayName = $this->trans('Age Check for Customer');
        $this->description = $this->trans('Add the extra B.O fields for the Age Verification records');
    }

    public function install()
    {
        return parent::install()
			&& $this->registerHook(static::HOOKS);
    }

    public function uninstall()
    {
        return parent::uninstall();
    }

	// ****************************** CUSTOMER LIST ****************************** 
	
	// Create new column in Customer List
    public function hookActionCustomerGridDefinitionModifier(array $params)
    {
				
        $definition = $params['definition'];

        $definition
            ->getColumns()
            ->addAfter(
                'email',
                (new DataColumn('agecheck'))
                    ->setName($this->l('18?'))
                    ->setOptions([
                        'field' => 'agecheck',
                    ])
		);
		
		/** Remove the following columns */
		$columns = $definition->getColumns();
		$columns->remove('social_title')
			->remove('active')
			->remove('optin')
			->remove('newsletter')
			;
		
		/** Remove the following filters */
		$filters = $definition->getFilters();
		$filters->remove('social_title')
			->remove('active')
			->remove('optin')
			->remove('newsletter')
			;

    }
	
	    /**
     * Add data to the new field in Customer List
     */
    public function hookActionCustomerGridQueryBuilderModifier(array $params)
    {
        $searchQueryBuilder = $params['search_query_builder'];

        $searchCriteria = $params['search_criteria'];
		
		$searchQueryBuilder->addSelect( 'c.`agecheck`' );
    }
	
	
		// ****************************** CUSTOMER PAGE ****************************** 
	
	// Create new column in Customer List
    public function hookActionCustomerFormBuilderModifier(array $params)
    {
    $formBuilder = $params['form_builder'];
		
    $allFields = $formBuilder->all();
    foreach ($allFields as $inputField => $input) {
        $formBuilder->remove($inputField);
    }
		
    foreach ($allFields as $inputField => $input) {
        $formBuilder->add($input);
        if ($inputField == 'email') {
            /** @var TextType::class \Symfony\Component\Form\Extension\Core\Type\TextType */
            $formBuilder->add(
                'agecheck', 
                 TextType::class, 
                 ['label' => 'Age Verified ?']
            );
			$formBuilder->add(
                'agecheckdate', 
                 TextType::class, 
                 ['label' => 'Date of Check']
            );
			
        }
     }
		
		$formBuilder->get('agecheck')->setRequired(false);
		$formBuilder->get('agecheckdate')->setRequired(false);
  }
	
	    
     // Add data to the new field in Customer List
    
    public function hookActionCustomerFormQueryBuilderModifier(array $params)
    {
        $searchQueryBuilder = $params['search_query_builder'];

        $searchCriteria = $params['search_criteria'];
		
		$searchQueryBuilder->addSelect( 'c.`agecheck`' );
		$searchQueryBuilder->addSelect( 'c.`agecheckdate`' );
    } 
	
	
	// ****************************** ORDER LIST ******************************

		// Create hook in Order List
    public function hookActionOrderGridDefinitionModifier(array $params)
    {
        /** @var GridDefinitionInterface $definition */
        $definition = $params['definition'];

        $definition
            ->getColumns()
            ->addAfter(
                'customer',
                (new DataColumn('agecheck'))
                    ->setName($this->l('18?'))
                    ->setOptions([
                        'field' => 'agecheck',
                    ])
		);
    }
	
		    /**
     * Add data to the new field in Order List
     */
    public function hookActionOrderGridQueryBuilderModifier( array $params ) {

		$searchQueryBuilder = $params['search_query_builder'];

        $searchCriteria = $params['search_criteria'];
		
		/** $searchQueryBuilder->addSelect( 'c.`agecheck`' );*/
		$searchQueryBuilder->leftJoin(
            'c',
            '`' . pSQL(_DB_PREFIX_) . 'customer`',
            'pa',
            'pa.`id_customer` = c.`id_customer`
                 = (
                    SELECT
                      pa2.`agecheck`
                    FROM
                      `'. pSQL(_DB_PREFIX_) .'customer` pa2
                    WHERE pa2.`id_customer` = pa.`id_customer`
                    ORDER BY LENGTH(pa2.`agecheck`) DESC
                    LIMIT 1
                )'
        );

    }

	


	
}

I'm not sure what the 'c', 'pa' & 'pa2' refer to, so I'm stuck there at the moment.

×
×
  • Create New...