Trong bài viết này chúng ta sẽ học cách tạo một custom indexer trong Magento 2. Indexer là một tính năng quan trọng trong Magento 2. Nếu bạn vẫn chưa biết Indexer trong Magento 2 là gì, các bài viết sau có thể sẽ hữu ích cho bạn.
Indexing là cách mà Magento chuyển đổi dữ liệu chẳng hạn như products hoặc categories, nhằm cải thiện hiệu suất cửa hàng của bạn. Khi dữ liệu thay đổi thì dữ liệu trong các index cần được cập nhật hoặc lập chỉ mục lại(reindexing). Magento có một cấu trúc rất phức tạp để lưu trữ dữ liệu (bao gồm catalog data, prices, users và stores) trong nhiều bảng cơ sở dữ liệu. Để tối ưu hóa hiệu suất cửa hàng, Magento sẽ đưa dữ liệu vào các bảng đặc biệt bằng cách sử dụng indexers.
Ví dụ, nếu bạn thay đổi giá của một sản phẩm từ $4 còn $3. Magento phải reindex lại giá của product này để có thể hiển thị ở frontend.
Nếu không có indexing thì Magento phải tính toán tất cả price, catalog price rule, discount amount, bundle pricing… mà việc này sẽ ảnh hưởng lớn đến hiệu suất cửa hàng của bạn.
Bước 1: Định nghĩa Indexer trong indexer.xml
File: app/code/ViMagento/LearnIndexer/etc/indexer.xml
1 2 3 4 5 6 7 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Indexer/etc/indexer.xsd"> <indexer id="vimagento_learn_indexer" view_id="vimagento_learn_indexer" class="ViMagento\LearnIndexer\Model\Indexer\Test"> <title translate="true">ViMagento Learn Indexer</title> <description translate="true">ViMagento Indexer Description</description> </indexer> </config> |
Trong đó:
- id là một id duy nhất dùng để định danh cho indexer. Bạn có thể dùng id này để kiểm tra trạng thái hay reindex indexer thông qua command line.
- view_id sẽ được định nghĩa trong mview.xml configuration.
- class là class sẽ thực hiện xử lý logic của indexer.
Bước 2: Tạo mview.xml
mview.xml được sử dụng để theo dõi các thay đổi cơ sở dữ liệu cho một thực thể nhất định và chạy phương thức để xử lý logic (execute ()).
File: app/code/ViMagento/LearnIndexer/etc/mview.xml
1 2 3 4 5 6 7 8 |
<?xml version="1.0" encoding="UTF-8"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Mview/etc/mview.xsd"> <view id="vimagento_learn_indexer" class="ViMagento\LearnIndexer\Model\Indexer\Test" group="indexer"> <subscriptions> <table name="catalog_product_entity" entity_column="entity_id" /> </subscriptions> </view> </config> |
Trong file này, chúng ta định nghĩa một view với id được gọi từ indexer đã thực hiện ở bước 1 và một class chứa phương thức execute(). Phương thức này sẽ được chạy khi table được đăng ký trong thẻ <subscriptions> có sự thay đổi.
Để khai báo table, chúng ta sử dụng table name và column của table sẽ được gửi cho phương thức execute(). Trong ví dụ trên tôi đã khai báo table catalog_product_entity. Vì vậy, khi một trong những product được lưu thì phương thức execute() của class ViMagento\LearnIndexer\Model\Indexer\Test sẽ được gọi.
Bước 3: Tạo indexer class
File: app/code/ViMagento/LearnIndexer/Model/Indexer/Test.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<?php namespace ViMagento\LearnIndexer\Model\Indexer; use Magento\Framework\Indexer\ActionInterface; use Magento\Framework\Mview\ActionInterface as Mview; <em>/** * Class Test * </em><strong><em>@package </em></strong><em>ViMagento\LearnIndexer\Model\Indexer */ </em>class Test implements ActionInterface, <em>Mview </em>{ <em>/** * Đươc sử dụng bởi mview, * </em><strong><em>@param </em></strong><em>int[] $ids */ </em>public function execute($ids) { //code here! } <em>/** * Sẽ reindex tất cả dữ liệu * Được chạy khi reindex thông qua command line */ </em>public function executeFull() { //code here! } <em>/** * Làm việc với 1 tập hợp các entity, có thể thông qua massaction * </em><strong><em>@param </em></strong><em>array $ids */ </em>public function executeList(array $ids) { //code here! } <em>/** * Reindex cho một entity cụ thể * </em><strong><em>@param </em></strong><em>int $id */ </em>public function executeRow($id) { //code here! } } |
Và đây là kết quả:
1 |
php bin/magento indexer:status |

Và tất nhiên bạn có thể reindex cho custom indexer vừa tạo với lệnh:
1 |
php bin/magento indexer:reindex vimagento_learn_indexer |
Các bạn có thể tham khảo thêm tại https://devdocs.magento.com/guides/v2.4/extension-dev-guide/indexing-custom.html