Commerce Ajax Add to Cart module porting takeaways

Recently I completed Drupal 8 porting of Commerce Ajax Add to Cart module - 8.x-1.0-beta1. See my tweet. I have been working on this for around a year.


The main objective of doing the port is to learn the concepts of Drupal 8. Make this module available for Drupal 8. And also, open sourcing - giving back to the community.


I got the chance to do things hands-on which I only knew in theory.

Here I am talking about concepts Dependency Injection, Test Driven Development which I only knew theoretically.

Being more of a hands-on guy, I am not satisfied by just reading the theory, I get an itch to do that practically as well.

Object Oriented Programming in PHP

I knew the basics of OOP in PHP. But while I was working, I frequently referred the code written in the Commerce core, and it heavily inspired me. The core uses the strategy pattern. And it felt very clean to me - You use an interface to define the behavior of a class. And you can utilize the benefits of type hinting using the interface.

Dependency Injection in PHP

I read about the Dependency Injection concept but never got the chance to implement it. While porting the module, I used services (also see Symfony services) to make commonly used methods available across the module, and they will be loaded at the time of object instantiation.

I am a fan of loosely coupled architecture. Dependency Injection helps you to make your code more loosely coupled and clean. It helps you maintain the state of the execution, and is more OOP aligned.

Acquia’s article Dependency Injection mentions the benefits of using services and loading them via Dependency Injection.

Test Driven Development (TDD)

I have so much read about TDD, and this time I got the chance to follow the process.

At the initial stages of the porting, since because Drupal 8 and its test framework was not very much clear to me, thus it wasn’t possible to follow the TDD process.

Once the base of the module (with tests) was complete, I started following TDD. I followed the TDD cycle while adding the features/integrations in the module.

I now feel more confident following the TDD process :smile:

Although there are still many bugs in the module for a stable release, still I learned a lot.


Leave a Comment