Tag: shop

  • GRID – modify/extend existing column(s)

    Prestashop has very nice mechanism to create and display a table (grid) with some data. For example go and look at orders list. You have there a columns (with different data type), filters per each column, row actions, bulk actions, etc.

    You can modify (existing columns) or extends that grid (add new column(s)). To do that you have to implements some (not all) hooks:

    where DefinitionId you have to find on specific page with the grid. Open source code of that page and look for div element with id attribute which looks like DefinitionId_grid_panel.

    Lets say you want to add new column with information if it’s person of business order.

    Extend grid definition

    First we have to add our new column definition and allow to filter with that column:

    use PrestaShop\PrestaShop\Core\Grid\Column\Type\DataColumn;
    use PrestaShop\PrestaShop\Core\Grid\Filter\Filter;
    use PrestaShopBundle\Form\Admin\Type\YesAndNoChoiceType;
    
    public function hookActionOrderGridDefinitionModifier(array $params): void
    {
        $definition = $params['definition'];
    
        $definition->getColumns()->addAfter(
            'customer',
            (new DataColumn('is_business'))
                ->setName('Business')
                ->setOptions([
                    'field' => 'is_business',
                ])
        );
    
        $definition->getFilters()->add(
            (new Filter('is_business', YesAndNoChoiceType::class))
                ->setAssociatedColumn('is_business')
        );
    }

    Modify grid query

    With „new” grid definition we have to modify grid query builder:

    public function hookActionOrderGridQueryBuilderModifier(array $params): void
    {
        $searchCriteria = $params['search_criteria'];
        $isBusiness = $searchCriteria->getFilters()['is_business'] ?? null;
        $queryBuilders = ['count_query_builder', 'search_query_builder'];
    
        foreach ($queryBuilders as $qbName) {
            $qb = $params[$qbName];
    
            if ($qbName === 'search_query_builder') {
                $qb->addSelect('IF (ai.company != '', 'Yes', '') AS is_business');
            }
    
            $qb->leftJoin(
                'o',
                _DB_PREFIX_ . 'address',
                'ai',
                'o.id_address_invoice = ai.id_address'
            );
    
            if ($isBusiness !== null) {
                $qb
                    ->andWhere('IF (ai.company != '', 1, 0) = :isBusiness')
                    ->setParameter('isBusiness', $isBusiness)
                ;
            }
        }
    }

    We have to modify 2 queries.

    • count_query_builder count rows and display this number next to grid title,
    • search_query_builder select data.

    In our example we assumed that if customer provide company name then „mark” this order as business.

  • Run Prestashop with docker

    Fast response time of the server is the one of the main requirements which each e-commerce shop has to pass. There are many shared hosting options which cost lost of money. To reduce this cost to minimum good option is to choose a VPS (Virtual Private Server). It requires some technical knowledge but price to performance profit are big.

    At the beginning you should find some cheep VPS. First advantage of VPS is that you can easily upgrade they „hardware” by 1-click. Almost all providers give that option.

    Nginx server

    When you have yours VPS installed and configured you have to install nginx server. It will be our reverse-proxy.

    apt update
    apt install nginx

    Here you can fine a good starter config for nginx server. Of course you have to change a domain name. On this page you have an instruction how to install this configuration and how to setup SSL certificate for your domain (it’s totally FREE!).

    You have to add new file in /etc/nginx/conf.d directory. Name it upstreams.conf. As a content of the file put:

    upstream prestashop_extalion_com {
        server 127.0.0.1:1234;
    }

    This will allow to forward a request from our nginx server to our docker-compose Prestashop stack. Port 1234 has to be the same as the port in docker-compose stack.

    Reload nginx service (nginx -t && nginx -s reload) and go to next step!

    Prestashop docker

    There is a nice docker-compose stack, click here! On your VPS go to /var/www/prestashop.extalion.com and clone this repository:

    git clone git@github.com:eXtalionLab/prestashop_docker.git .

    or download a *.zip file and unzip it:

    wget https://github.com/eXtalionLab/prestashop_docker/archive/refs/heads/master.zip
    unzip master.zip

    After that create and update config in .env file:

    cp .env.dist .env
    • If you want to run production environment uncomment COMPOSE_FILE env.
    • PS_DOMAIN has to be the same as your domain name in /etc/nginx/sites-available/prestashop.extalion.com.
    • PRESTASHOP_PORT env has to have the same port as in /etc/nginc/conf.d/upstrems.conf file.
    • More about other envs you can read here.

    When you’re done it’s time to build and run our new Prestashop instance:

    docker-compose up -d && docker-compose logs -f

    First docker will download and build a new images. After that Prestashop scripts will install and setup new instance base on env vars. Be patient!

    When you will see this line in logs:

    NOTICE: ready to handle connections

    you’re ready to enjoy with your shop.