Working with files and folders in Magento2

Working with files and folders in Magento2

Magento® 2 offers you a bunch methods for file and folder operations, which allow you to implement quality modules in a short time. Especially if you are planning to publish modules on the Magento Marketplace you have to use those functions, otherwise you won’t meet Magento’s coding standards. In order to have access to cool functions like mkdirRecursive, deleteDirectory or changePermissionsRecursively simply inject \Magento\Framework\Filesystem\Driver\File in your constructor.

Once you have re-compiled your code, you should be able to use those functions in your modules like below.

More functions are listed below.

1. Check if the file or folder exists on the file system.

2. Returns information like ctime and mtime or sizeatimeuid and gid.

3. Check if a file is readable

4. Check if path is a file ( not folder )

5. Check if path is a directory ( not a file )

6. Get content of a regular file

7. Check if a regular file is writable ( not a folder )

8. Return the parent directory of a path

9. Create a new directory with specific permissions

10. Create a new directory recursively

11. Reads a directory and returns an array with all paths of its sub-directories

12. Search for a path

13. Rename a regular file or a directory

14. Copy a file from A to B

15. Create a symlink

16. Delete a regular file ( not a file )

17. Delete a directory recursively

18. Change permissions of a directory

19. Change permissions of a directory and file recursively

20. Manipulate the access and modification time of a file

21. Write content to a file

22. Opens a file like fopen

23. Returns a line from the given handle

24. Reads a file like fread.

25. Reads a CSV file like fgetcsv

26. Returns position of a pointer like ftell

27. Seeks to a specific offset like fseek

28. Returns true if pointer is at the end of a file

29. Close a file

30. Write to a file like fwrite

31. Write one row to a CSV file

32. Flush output of a file. See fflush.

33. Lock a file before write content to a file

34. Unlock a file after all file manipulations are done

35. Return a absolute path

36. Fixes a path separator

37. Reads a directory recursively and returns paths as an array

38. Return the real path like realpath

39. Returns a path for link


UI Components and searchResultToOutput in Magento2

Today I had to fix the following issue in a custom module. The error appeared on the UI Component list view.

I found the reason of the problem in the di.xml file. All what was missing was the following virtualType node. After adding this and re-compiling the code, the list view was fixed.

Update composer.lock without updating code

Recently I had to fix a broken composer.lock but without actually updating code. I have found the composer option ” nothing ” which updates the composer.lock file only.

Once the command ran successfully, you should see a line ” Writing lock file ” in the output.

If you have issues with dependencies you can update the composer.lock file with –ignore-platform-reqs.

Missing required argument $name of Xtento\ProductExport\Logger\Logger.

If you are using Xtento in combination with an automated product importer, you may have seen the following error before.

The error message appears right after saving a product. The reason is that, at least one of your products in Products > Catalog has no Name. To fix this, you can update the name manually via MySQL queries or simply delete the products and import again correctly.

Bulk Update Customizable Product Options in Magento2

Today I had to bulk-update the Price and Max Characters in the tab Customizable Options for hundreds of simple products based on the title.

Magento2 Customizable Product Option

I knew that it could be done programmatically but I figured out that it can be done just by updating the table catalog_product_option and catalog_product_option_price. First I got a comma separated list of option_id’s based on a specific title.

With the comma separated list I was able to update the value of the Price and Max Characters as follows.

1. Price

2. Max Characters

Possible that you have to flush the cache ( php/magento cache:flush ) and re-index everything ( php/magento indexer:reindex ) to see the results.

Change Order, Invoice, Credit Memo or Shipment Number Prefix or Suffix in Magento2

In some cases shop owners have to change their default order number with a custom prefix or suffix. Customising order numbers sounds complicated, but it can be done with only few MySQL queries.

First, backup your database. After that, get a list of meta_id’s and store_id’s with the following query for order numbers only.

The meta_id is pretty much the identifier you need for the next UPDATE query which will add the letter A as a prefix. For example: A100000001

The same query can be used to change the suffix. Also, you can change the prefix or suffix for any entity_type such as invoice, creditmemo or shipment.

That’s it. Create a new order to see the results.

Algolia and Undefined index: store_id

Today I have setup the Algolia Queue Runner ( algolia_queue_runner ) which was working fine for all projects I have worked with before. However, today it was different. The Algolia Queue Runner didn’t work at all. Here is what happened every time when the cronjob was running.

The problem was, one of the store views in Stores > Settings > All Stores was disabled for some reason. After enabling the store view, the problem was fixed.

Captcha Test For Your Contact Form

The following PHP script allows you to test your contact form captcha integration in Magento. If you receive any email after executing this script, you have to review your captcha integration.

You may need to add, remove or rename fields in the $field array. Don’t forget to replace {base_url} with your actual URL.

Automatically Update The Year In The Footer

Magento Footer Copyright

In most of the projects that I am working on, it is still common practice to have the current year of the copyright text stored in System > Configuration > General > Design > Footer.

That means, every year somebody has to change the year manually. This is very time consuming, especially if you have to manage ten, twenty or more clients. Here is a quick example of how you can make it dynamic.

1. Set a custom string such as {YEAR}

Edit your footer in System > Configuration > General > Design > Footer and place the {YEAR} somewhere.

2. Replace the {YEAR}

Edit your footer.phtml in your package and replace getCopyright as shown below. For the year I am using Mage::getModel(‘core/date’) to have the correct timezone included.

File: ./app/design/frontend/{package}/default/template/page/html/footer.phtml


We’re sorry, an error has occurred while generating this email

Today, I was dealing with the following error message on one of our Magento2 production environment.

After spending 2 hours trying to find the source of the problem in our custom modules, I was able to fix the issue by disabling the core module Magento_SampleData.

What is this module for? This module gives you different sample data ( CMS, Catalog, Customers ) which is useful during development and testing phase. For your production environment it is not necessary and also not recommended to have it installed, as you can read here.