PDF Generation in Magento 2

magento-pdf-layout

The Magento core methods to generate PDF files are rather unflexible. An alternative are tools that convert HTML to PDF.

In our current Magento 2 project, which we (integer_net) develop together with Stämpfli AG, there is a requirement to dynamically create a PDF catalog based on selected products, which has almost the same layout as the product lists in the shop. So, generating this PDF based on HTML suggested itself.

In this article I present our solution, which integrates wkhtmltopdf with the Magento layout. At the end you will find a link to the base module on Github.

Continue reading “PDF Generation in Magento 2”

The week on StackExchange #40/2016

Since the last “Weeks on StackExchange” post got quite long, I’ll get back to the weekly schedule:

Hot Magento 2 answers

Open questions

5 Minute Tips: Form Usability

Learning coffee break! Today some tipps on “form usability”, a topic that even backend developers should not leave alone.

The weeks on StackExchange #37-39/2016

Here we go again, a summary of interesting posts from the last weeks:

Magento 2 CLI

Magento 2 Extension Development

Magento 2 Translation

Magento Product Import

Magento 1 Best Practices

Magento 1 Tipps

PHPUnit

Isolating Domain Logic in Magento Customizations

Lately I’ve been advocating decoupling business logic from the framework (i.e. Magento) a lot.

This has multiple advantages:

  • Benefit from test driven development (TDD) without having to mock a bunch of core classes.
  • Possible reuse in different applications (for example Magento 1 and Magento 2)
  • Having separated bounded contexts helps to view parts of the domain isolated and without distraction.

Even in chirurgic modifications that we often have to do in Magento projects, it is worth identifying the actual logic and extracting it from the actual Magento classes.

Let me demonstrate it with a real-world example:

Read more at integer-net.com

Stop using Helpers

“Helpers” are often used as convenient collection of functions. They are also a sign of bad design, and I want you to stop writing them. I’ll quote myself

In general, having classes named “Helper”, “Util” or similar just says “I have some functions that I don’t know where to put” and don’t make much sense as a class.

It’s not very object oriented. Not at all to be frank. The idea of object oriented programming is that there are objects that send each others messages. They have an active role in the system and are not just containers for data and code, which would be a very procedural way to see them.

So what would be the role of a helper? A butler maybe, that does not act on its own and will do anything you tell him. But to do that, he needs access to your whole household, your bank account and your car.

Continue reading “Stop using Helpers”

Memoize Method Calls in PHP with Cache Decorators

A “memoized” function is a function that only calculates the return value for each combination of arguments once and returns the previously calculated value if the function is called a second time with the same arguments.

In PHP, I often see this implemented with code like this:

class ProductRepository implements ProductRepositoryInterface
{
    private $products = [];
    public function product($id)
    {
        if (! isset($this->products[$id])) {
            $this->products[$id] = $this->load($id);
        }
        return $this->products[$id];
    }

    private function load($id) { ... }
}

Continue reading “Memoize Method Calls in PHP with Cache Decorators”

The week on StackExchange #36/2016

And again some new posts from Magento StackExchange that I’d like to highlight:

Magento 2

Magento 1

PHP: Using class_alias to maintain BC while moving/renaming classes

Sometimes you want to rename a class or move it to a different namespace. But as soon as it is used anywhere outside the package, this is breaking backwards compatibility and should not be done lightheartedly.

Luckily there is a way in PHP to have both, the old class and the new class, while deprecating the old one: class_alias().

How to use class_alias() without messing up class autoloading

Let’s say, the old class is OldClass and we want to rename it to NewClass.

First, we rename the class and move it from OldClass.php to a new file NewClass.php.
Continue reading “PHP: Using class_alias to maintain BC while moving/renaming classes”

The week on StackExchange #35/2016

I’m a bit late but I was quite active on the Magento StackExchange recently, so here are the best posts from last week!

Magento 1 answers

PHPUnit

New Magento 2 questions