Every once in a while we have some interesting feedbacks to solve. This is one of them, so I sat down to write an article about it. You know what they say, sharing is caring. I hope that it can benefit you in some way.
Our requirement was simple:
1) Switch to Avery 5162 template which is 1 1/3″ tall, leaving enough room for 6 lines.
2) Put the full country name on the very last line
3) Convert all text to CAPITAL LETTERS. This is also appropriate for domestic mail, so for simplicity just capitalize everything.
4) Implement all this for exporting expiring memberships.
The project was related to WooCommerce Memberships.
We had more complex ones before, so this was not so of a problem. But the thing that stands out from the requirement is that client wanted Avery 5162 template for the exported file. And this means we would have to export it in a Word or DOCX file.
Avery 5162 format comes from Avery.com. They are a printing service. In order to be printed from them, they have a template. They expect us to fill in the text on that template, which can be sent to them to be printed.
Sure our client could’ve filled this up by himself, but that would be tedious of a job. So, he wanted this to be automatic. That’s where we come in.
We studied the Aver 5162 template. Also, we found a PHP library named PHPWord which would give us the ability to generate a word file. We ran some tests and it was up to our requirements, so we went on with it.
We later found out that PHPWord does not support new pages, so we added this functionality. We are planning to soon contribute our code changes to the project. We were humble to have such a great library in our hands.
In order to make this easier, we saved an empty template inside our plugin’s assets directory.
Then we used some code like this:
First we checked if we have an expiring membership this month. If not, no export. If yes, we would continue with the process.
Then we set some variables to be used later.
We prepared variables for how many records we would show on each page, template docx file, output file etc.
Then we loop through possible pages. I have commented the code for you so that you can understand which line does what.
This may seem a bit complex at first, but the logic behind is simple.
Oh! And not to forget, we have also added the functionality to add pages in the DOCX (which was not there by default on PHPWord):
We have heard many on the web is searching for similar solution. If you are too, you can use the code above. Or better yet, contact us and we can help you with your very own custom implementation for Avery Templates. Sky is the limit!