Practical Module Development for Prestashop 8 - Louis AUTHIE - E-Book

Practical Module Development for Prestashop 8 E-Book

Louis AUTHIE

0,0
25,19 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

Elevate your e-commerce game and beat the competition by unleashing the power of PrestaShop 8.x with Symfony-based modules, customizable themes, and much more


Purchase of the print or Kindle book includes a free PDF eBook


Key Features


Understand the data and object architecture of the legacy and new Symfony core of PrestaShop


Create modules to add features such as content blocks, payment options, and carriers to your store


Learn to customize themes and override existing module templates


Book Description


After version 1.7, PrestaShop underwent a host of changes, including migration to a Symfony-based system from an outdated legacy code. This migration brought about significant changes for developers, from routine maintenance to module development. Practical Module Development for PrestaShop 8 is curated to help you explore the system architecture, including migrated and non-migrated controllers, with a concise data structure overview. You’ll understand how hooks enable module customization and optimize the CMS.


Through the creation of seven modules, you’ll learn about the structure of modules, hook registration, the creation of front-office controllers, and Symfony back-office controllers. By using Doctrine entities, services, CQRS, grids, and forms, you’ll be guided through the creation of standard, payment and carrier modules. Additionally, you'll customize and override themes to achieve your desired e-commerce store look.


By the end of this book, you’ll be well equipped to provide modern solutions with PrestaShop that meet client requirements.


What you will learn


Understand the structure of PrestaShop’s core


Explore hooks and their functions


Create a hello world module


Build modules to display blocks in the front office with different styles


Design a module to add fields to the category pages and manage them


Fashion a blogging module with front and modern back-office controllers


Fabricate payment and carrier modules to improve the user experience


Customize a theme by creating a child theme


Who this book is for


If you are a junior or advanced PHP developer already using PrestaShop as a simple user willing to know more or to solve online sellers' problems by creating modules as a professional, this book is definitely for you. In order to learn from this book, you should have a basic knowledge of the Symfony framework. This book will be a really good help for the module developers expecting to move from the old legacy environment to the modern one. Other CMS developers can use that book as a tool to compare and move to PrestaShop.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB

Seitenzahl: 318

Veröffentlichungsjahr: 2023

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Practical Module Development for Prestashop 8

Create modern, customizable, and dynamic online stores using efficient modules

Louis AUTHIE

BIRMINGHAM—MUMBAI

Practical Module Development for Prestashop 8

Copyright © 2023 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

Group Product Manager: Alok Dhuri

Publishing Product Manager: Himani Dewan

Content Development Editor: Rosal Colaco

Technical Editor: Jubit Pincy

Copy Editor: Safis Editing

Project Coordinator: Manisha Singh

Proofreader: Safis Editing

Indexer: Pratik Shirodkar

Production Designer: Alishon Mendonca

Developer Relations Marketing Executives: Deepak Kumar and Mayank Singh

First published: April 2023

Production reference: 1070423

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham

B3 2PB, UK.

ISBN 978-1-83763-596-2

www.packtpub.com

To my honey, Alice, and my lovely boys, Tom and Noé.

– Louis AUTHIE

Contributors

About the author

Louis AUTHIE is a freelance full-stack developer with over 25 years of experience in PHP. He graduated from the ENAC, Toulouse, France in 2011 as an engineer and from the CNAM Paris, France as an analyst programmer in 2017.

Since 2012, he’s developed and maintained modules and themes from various versions of PrestaShop. Becoming an associate for Label Naturel’s bedding e-shop in 2016 improved his awareness of online sellers’ challenges. In 2019, he started contributing to the PrestaShop open source project, first to the documentation, then to back-office migration and bug fixes. Louis co-founded Web-Helpers, a web agency, in 2020, and teaches professionals in PHP, WordPress, PrestaShop, and Symfony development as a consultant trainer.

I want to thank my girlfriend and our beloved boys who supported me during this writing adventure, even if they won’t understand a word of this book! Thanks to my family for their good care. Thanks to all the Packt Publishing staff who guided me during the whole process, and special thanks to my amazing reviewers, Hervé Hennes and Franck Lefevre.

All the royalties of this book will be donated to the Friends of PrestaShop association, which constantly contributes to the PrestaShop Project and manages a very helpful Slack channel.

About the reviewers

Hervé Hennes, the lead developer at Advisa, is a senior PrestaShop developer, and Magento certified. His famous blog at h-hennes is a must-read for all PrestaShop and Magento developers.

Franck Lèfevre has been a self-made PHP developer for more than 15 years. Working as a freelance developer, he helps businesses to solve their technical problems. He is a big expert on PrestaShop and remains the main PrestaShop open source project contributor. He is also an indie maker and the creator of the Skypaper application.

Table of Contents

Preface

Part 1 – Understanding How PrestaShop is Structured and How It Works

1

Quick Overview of PrestaShop

Technical requirements

The data model–how and where data is stored

Browsing the data

Reverse engineering the contact entity

Manipulating data by extending the ObjectModel class

Using the Db class

The core classes – how the MVC works

Presenting the legacy core MVC pattern

Discovering the core migration

Summary

2

The Configuration and Initialization of PrestaShop

Technical requirements

The legacy FO controller initialization

Initializing a Category FO controller

The legacy BO and Symfony admin controller initialization

The legacy BO controller initialization

The Symfony BO controller initialization

The most important constants to set as a developer

The Dispatcher class

Summary

3

The Front Office

Technical requirements

Discovering how an FO controller works

Discovering the ControllerCore object

Discovering the FrontControllerCore object

Discovering the implementation of CmsController

Using the model

Using the views from the theme

Summary

4

The Back Office

Technical requirements

Discovering how a legacy-based BO controller works

Discovering the AdminController features

Exploring the AdminCartsController class

The most useful tools for recurrent HTML generation: the Helper classes

Generating an edition form with HelperForm

Generating an element list table with HelperList

Discovering how a migrated Symfony-based BO controller works

Handling the form submission

Using the CQRS design pattern

Using a Command object and its CommandHandler class in the CommandBus

Using a Query and its QueryHandler in the QueryBus

Generating table views by using Grid components

Summary

5

The Hooks

Technical requirements

Quick presentation of a hook

Discovering the hook ObjectModel child object

Presenting the two types of hook

Registering/unregistering a module to a hook and implementing a module’s behavior on a hook execution

Executing a hook

Executing a hook from a legacy controller

Executing a hook from a Symfony controller

Executing a hook from a Smarty template

Executing a hook from a Twig template

Summary

6

The Themes

Technical requirements

Understanding what a theme is and its structure

The FO Smarty templates

The most used Smarty constants

The CSS/JS assets

Embedding the CSS

Embedding the JavaScript

Summary

Part 2 – How to Create Your Own Modules

7

What Are Modules? Let’s Create a Hello World Module

Technical requirements

Defining a module

Creating our first hello world module

Overriding the install() and uninstall() methods

Defining the hookDisplayHome($params) function

Providing the module logo

Adding an index.php file for security

Installing our first module

Summary

8

A Reinsurance Block Module

Technical requirements

Defining our module design

Creating the module’s initial structure

Defining the module class

Choosing our hooks

Defining the install() and uninstall() methods

Defining the hook execution handlers

Adding a Smarty template view

Adding CSS styles to our module

Testing our module

Summary

9

A Customer Callback Request Module

Technical requirements

Defining our module design

Creating the module’s initial structure

Managing the model the modern way

Creating the callback request entity

Handling inserting a callback request into the database

Sending a notification email

Creating a modern BO controller containing a grid to list all the requests

Creating a modern admin controller

Creating a route in the Symfony router

Using the Grid component to generate our callback requests listing view

Handling a callback request delete action from the grid

Adding the BO controller to the BO menu

Creating a configuration page for our module and adding a Symfony form

Defining our form type

Defining our form data provider

Generating the form and displaying it

Handling form submission with a form handler

Summary

10

Category Extension Module

Technical requirements

Presenting the most useful hooks to extend a form and handle it

Extending the Category Symfony-based form on the BO Category controller

Handling new input from the extended form

Cleaning our saved content on Category deletions

Defining our module design

Creating the module’s initial structure

Creating the entities

Extending the Category form in creation mode

Improving the Category form in edition mode

Handling the extended form submission

Handling Category deletion

Displaying saved content on the FO Category page

Testing our module

Summary

11

A Simple Blogging Module

Technical requirements

Defining our module design

Designing the general architecture of this module

Defining the data structure

Designing the BO controllers

Designing the FO controllers

Creating entities and relationships, repositories, and database tables

Creating commands, queries, and data transfer objects

Creating BO controllers and routes

Creating AdminWHBlogCategoryController and its routes

Creating AdminWHBlogPostController and its routes

Adding forms for creating and editing entities

Creating Grids

Handling deletions

Creating the FO controllers and displaying the data

Summary

12

A Cash Payment Module

Technical requirements

Defining our module design

Defining a PrestaShop payment module

Creating our module structure

Implementing the hookPaymentOptions($params) method

Implementing the hookDisplayPaymentReturn($params) method

Creating a new order status for orders pending cash payment

Creating the payment validation controller

Testing our module

Summary

13

A Drive Delivery Module

Technical requirements

Defining our module design

Defining a PrestaShop Carrier module

Creating our module structure

Creating a WHRelayCart entity to save our chosen relay points

Creating a Carrier programmatically

Displaying the relay pickup points list during the checkout

Handling a relay pickup point choice via an Ajax FO controller

Displaying the pickup point on the order view BO controller

Creating a Widget to promote our module anywhere on the FO

Testing our module

Summary

Part 3 – Customizing Your Theme

14

How to Create a Child Theme

Technical requirements

Discovering what a child theme is

Creating a child theme structure

Installing and testing our child theme example

Summary

15

Overriding Some Templates

Technical requirements

Overriding a module template from a (child) theme

Overriding a module CSS asset from a (child) theme

Overriding a module JavaScript asset from a (child) theme

Summary

16

Assets Compiling with Webpack

Technical requirements

Understanding what Webpack does and its prerequisites

Discovering the structure of a Webpack config file

Building assets with Webpack

Summary

Appendix – Module Upgrade, The Hooks Discovery Tool, and Multi-Store Functions

Getting the hook parameters quickly

Making a module multistore-compliant

Upgrading a module

Index

Other Books You May Enjoy

Preface

Since version 1.7, PrestaShop began its migration from the old legacy core to a brand new Symfony-based system, this has resulted in many changes for developers, from maintenance to module development, and that’s what this book is all about.

The 16 chapters are grouped into 3 sections, each of which can be read individually. Feel free to go directly to what you need; there are no rules! However, it is best to read everything if you want to get the big picture of the system.

The first section explains how the legacy and the Symfony core work together and explores the main components of PrestaShop. There, we use many reverse engineering analyses. Even if it can be tough sometimes, the general idea is to show us how and where to search into the core objects so as to be able to find answers and go further.

The second section explains how to create various types of modules, with many hands-on examples. This practical approach will enable us to face most of our everyday requests from the simplest Hello World! to a full blogging module. Carrier and payment modules are also covered.

The third section provides the best practices and solutions to customize the themes and graphical parts of PrestaShop: child theme creation, module template and asset overrides, and asset bundling with Webpack.

As you can see, many aspects of PrestaShop are covered in this book, and our method is different from the general feature listings available in the developers’ documentation. The main target is to provide a practical approach with examples, which is lacking on the web or in the multiple available sources of information.

I hope you will enjoy your journey into the PrestaShop development world and that this book will become a useful toolbox!

Who this book is for

If you are a PHP developer already using PrestaShop as a simple user wanting to know more or solve online sellers’ problems by creating modules as a professional, either in a web agency or freelance, this book is definitely for you. In order to learn from this book, you should have a basic knowledge of the Symfony framework. This book will be a really good help for the module developers expecting to move from the old legacy environment to Symfony. Magento or WooCommerce developers can use this book as a tool to compare and move to PrestaShop.

What this book covers

Chapter 1, Quick Overview of the System, explores the structure of PrestaShop.

Chapter 2, Configuration and Initialization of PrestaShop, explains the initialization process of PrestaShop and how to configure it.

Chapter 3, The Front Office, shows how front office pages are generated in PrestaShop.

Chapter 4, The Back Office, shows how Symfony-based and legacy-based back office pages are processed.

Chapter 5, The Hooks, explains what Hooks are, and why they are so important for PrestaShop module developers.

Chapter 6, The Themes, covers the structure of a theme.

Chapter 7, What Are Modules? Let’s Create a Hello World Module, shows you how to create your first simple module.

Chapter 8, A Reinsurance Block Module, shows you how to create a module to increase the rate of conversion for your shop.

Chapter 9, A Customer Callback Request Module, shows you how to create a modern module using Symfony and find a way to get customer details to increase the rate of purchase.

Chapter 10, Category Extension Module, shows you how to create a category extension module to add fields to your category page in the back office and provide them to the front office.

Chapter 11, A Simple Blogging Module, shows you how to create a simple blog module with posts and post categories to apply all the most useful pieces of knowledge in module development.

Chapter 12, A Cash Payment Module, shows you how to create a payment module with its specificities.

Chapter 13, A Drive Delivery Module, shows you how to create a carrier module with a pickup relay point choice in the checkout process.

Chapter 14, How To Create a Child Theme, explains how to extend an existing theme by creating a child theme.

Chapter 15, Overriding Some Templates, teaches you how to override module templates and assets to make them fit in a theme graphical context.

Chapter 16, Assets Compiling with Webpack, offers a simple guide to use the Webpack bundler.

To get the most out of this book

You will need to be an intermediate PHP developer, having a basic knowledge of Symfony and knowing how to use PrestaShop and its back office. You will also need an operational PrestaShop v1.7.x or 8.x version installed, functional, and accessible. All the code examples have been tested using PrestaShop 1.7.8.8 and 8.1.

If you are using the digital version of this book, we advise you to type the code yourself or access the code from the book’s GitHub repository (a link is available in the next section). Doing so will help you avoid any potential errors related to the copying and pasting of code.

Download the example code files

You can download the example code files for this book from GitHub at https://github.com/PacktPublishing/Practical-Module-Development-for-Prestashop-8. If there’s an update to the code, it will be updated in the GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots and diagrams used in this book. You can download it here: https://packt.link/9XyJg

Conventions used

There are a number of text conventions used throughout this book.

Code in text: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: “Usually, handling a form submission is done with the help of the handle() method of the FormHandler instance.”

A block of code is set as follows:

$this->hookDispatcher->dispatchWithParameters('actionAfterCreate' . Container::camelize($form->getName()) . 'FormHandler', [     'id' => $id,     'form_data' => &$data,     ]);

Bold: Indicates a new term, an important word, or words that you see onscreen. For instance, words in menus or dialog boxes appear in bold. Here is an example: “Let’s install the module by browsing the Modules | Module Manager page of the PrestaShop BO.”

Tips or important notes

Appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: If you have questions about any aspect of this book, email us at [email protected] and mention the book title in the subject of your message.

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/support/errata and fill in the form.

Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Share Your Thoughts

Once you’ve read Practical Module Development for Prestashop 8, we’d love to hear your thoughts! Please click here to go straight to the Amazon review page for this book and share your feedback.

Your review is important to us and the tech community and will help us make sure we’re delivering excellent quality content.

Download a free PDF copy of this book

Thanks for purchasing this book!

Do you like to read on the go but are unable to carry your print books everywhere?

Is your eBook purchase not compatible with the device of your choice?

Don’t worry, now with every Packt book you get a DRM-free PDF version of that book at no cost.

Read anywhere, any place, on any device. Search, copy, and paste code from your favorite technical books directly into your application.

The perks don’t stop there, you can get exclusive access to discounts, newsletters, and great free content in your inbox daily

Follow these simple steps to get the benefits:

Scan the QR code or visit the link below

https://packt.link/free-ebook/9781837635962

Submit your proof of purchaseThat’s it! We’ll send your free PDF and other benefits to your email directly

Part 1 – Understanding How PrestaShop is Structured and How It Works

Mastering the mechanics of PrestaShop is a prerequisite for a good understanding of how modules are embedded in to the system. In this first part, we will explain the data structure and present the architecture of the whole system from the front-office to back-office controllers (migrated or not). Then, we will learn how Hooks work and enable modules to customize and improve the CMS. Finally, we will discover the themes, defining the graphical appearance of the front office.

This part has the following chapters:

Chapter 1, Quick Overview of The SystemChapter 2, Configuration and Initialization of PrestaShopChapter 3, The Front OfficeChapter 4, The Back OfficeChapter 5, The HooksChapter 6, The Themes

2

The Configuration and Initialization of PrestaShop

Knowing how data storage works and how the system is structured, we can dive into the initialization routines of PrestaShop. All this description of the CMS will enable us to master it, to understand how modules embed into the system, and how we will be able to use its components. By following the initialization process, we will see when and how the system is configured and how we can set some important constants to ease our development and change the database connection and cookies parameters.

In this chapter, we will cover the following main topics:

The legacy Front Office(FO) controller initializationThe legacy Back Office (BO) and Symfony admin controller initializationThe most important constants to set as a developerThe Dispatcher class

By the end of this chapter, you will know how an FO and a BO page initialize their controllers to generate the view even with a new Symfony-based one. You will understand how and when the main configuration and settings constants are set and how to manage them.

Technical requirements

As we will explore the environment of PrestaShop to understand how everything is articulated, you will have to use the following tools to do reverse engineering:

Any PHP code editorAn (S)FTP client to browse your website files (only if you work on a remote server, not necessary if you work on a local server)