Tag: presta

  • Increase Prestashop performance

    First of all you have to move your shop from shared hosting to VPS or dedicate server. Here you can read how to run Prestashop with docker. With this setup (own server) you can modify many things.

    One of this thing is to setup Redis as a Php session handler. Store and manage data in memory is signified faster then on hard drive (even if it’s SSD).

    Run Redis

    One of the easiest way to run Redis is to use docker image. Just run a command:

    docker run -d -p 6379:6379 redis

    If you prefer install and run Redis on your local machine, refer to the documentation.

    Php session handler

    To tell Php to use Redis instead of regular files as session handler you have to add those lines to your php.ini file:

    session.save_handler = redis
    session.save_path = "tcp://${REDIS_HOST}:6379?auth=${REDIS_HOST_PASSWORD}"

    With this setup it will works but you can get other issues with session. Redis session handler does not locking sessions by default. This causes a race condition problem. To prevent an issue add next lines to php.ini:

    redis.session.locking_enabled = 1
    redis.session.lock_retries = -1
    redis.session.lock_wait_time = 10000

    Now just restart you server (if using apache) just php-fpm and you’re done.

    Easy way to use this feature is to run this stock.

  • 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.