|
| 1 | +--- |
| 2 | +title: "Magento 2" |
| 3 | +date: 2023-06-07T15:00:02+02:00 |
| 4 | +draft: false |
| 5 | +type: "plugins" |
| 6 | +description: "Read from and write to a Magento 2 site." |
| 7 | +weight: 2 |
| 8 | +--- |
| 9 | + |
| 10 | +- [What is it?](#what-is-it) |
| 11 | +- [Installation](#installation) |
| 12 | +- [Usage](#usage) |
| 13 | + - [Building an extractor](#building-an-extractor) |
| 14 | + - [Building a lookup](#building-a-lookup) |
| 15 | +--- |
| 16 | + |
| 17 | +> Magento 2 is an e-commerce platform. |
| 18 | +
|
| 19 | +## What is it? |
| 20 | + |
| 21 | +This package includes classes to extract data from Magento, using a [custom connector](../custom). |
| 22 | + |
| 23 | +## Installation |
| 24 | + |
| 25 | +```shell |
| 26 | +composer require "php-etl/magento2-flow:*" |
| 27 | +``` |
| 28 | + |
| 29 | +This package includes classes and code that you will be able to use in your custom connector. |
| 30 | + |
| 31 | +## Usage |
| 32 | + |
| 33 | +### Building an extractor |
| 34 | +The package includes the following extractor classes: `CustomerExtractor`, `InvoiceExtractor`, `OrderExtractor`, `ProductExtractor`. |
| 35 | + |
| 36 | +```yaml |
| 37 | +custom: |
| 38 | + extractor: |
| 39 | + use: 'Kiboko\Component\Flow\Magento2\CustomerExtractor' |
| 40 | + services: |
| 41 | + Kiboko\Component\Flow\Magento2\CustomerExtractor: |
| 42 | + public: true |
| 43 | + arguments: |
| 44 | + - '@Monolog\Logger' |
| 45 | + - '@Kiboko\Magento\V2_1\Client' # Client to use depending on the Magento version. |
| 46 | + # Available clients are: |
| 47 | + # V2_1, V2_2, V2_3, V2_4 |
| 48 | + - 500 # Page size |
| 49 | + - [] # Optional filter groups |
| 50 | + |
| 51 | + Kiboko\Magento\V2_1\Client: |
| 52 | + factory: |
| 53 | + class: 'Kiboko\Magento\V2_1\Client' |
| 54 | + method: 'create' |
| 55 | + arguments: |
| 56 | + - '@Http\Client\Common\PluginClient' |
| 57 | + Http\Client\Common\PluginClient: |
| 58 | + arguments: |
| 59 | + - '@GuzzleHttp\Client' |
| 60 | + - [ '@Http\Client\Common\Plugin\BaseUriPlugin', '@Http\Client\Common\Plugin\AuthenticationPlugin' ] |
| 61 | + GuzzleHttp\Client: ~ |
| 62 | + Http\Client\Common\Plugin\BaseUriPlugin: |
| 63 | + arguments: |
| 64 | + - '@GuzzleHttp\Psr7\Uri' |
| 65 | + Http\Client\Common\Plugin\AuthenticationPlugin: |
| 66 | + arguments: |
| 67 | + - '@Http\Message\Authentication\Bearer' |
| 68 | + GuzzleHttp\Psr7\Uri: |
| 69 | + arguments: |
| 70 | + - 'http://example-magento.com' # URL of the website |
| 71 | + Http\Message\Authentication\Bearer: |
| 72 | + arguments: |
| 73 | + - '12345abcde' # Access token |
| 74 | + |
| 75 | + Monolog\Logger: |
| 76 | + arguments: |
| 77 | + - 'app' |
| 78 | + - [ '@Monolog\Handler\StreamHandler' ] |
| 79 | + Monolog\Handler\StreamHandler: |
| 80 | + arguments: |
| 81 | + - 'var/dev.log' # Path to the log file |
| 82 | + - 300 # Log level. 300 for Warning, 200 for Info... |
| 83 | +``` |
| 84 | +
|
| 85 | +#### With filters |
| 86 | +Filters and filter groups can be specified. |
| 87 | +Filters in a group are chained with `OR`. Groups are chained with `AND`. |
| 88 | +```yaml |
| 89 | +# ... |
| 90 | + Kiboko\Component\Flow\Magento2\CustomerExtractor: |
| 91 | + public: true |
| 92 | + arguments: |
| 93 | + - '@Monolog\Logger' |
| 94 | + - '@Kiboko\Magento\V2_1\Client' |
| 95 | + - 500 |
| 96 | + - [ '@date_filter_group', '@id_filter_group' ] |
| 97 | + # updated_at >= 1985-10-26 11:25:00 AND (entity_id = 12 OR entity_id = 64) |
| 98 | + |
| 99 | + date_filter_group: |
| 100 | + class: Kiboko\Component\Flow\Magento2\FilterGroup |
| 101 | + calls: |
| 102 | + - withFilter: [ '@last_execution' ] |
| 103 | + last_execution: |
| 104 | + class: Kiboko\Component\Flow\Magento2\Filter |
| 105 | + arguments: |
| 106 | + - 'updated_at' |
| 107 | + - 'gteq' |
| 108 | + - '1985-10-26 11:25:00' |
| 109 | + |
| 110 | + id_filter_group: |
| 111 | + class: Kiboko\Component\Flow\Magento2\FilterGroup |
| 112 | + calls: |
| 113 | + - withFilter: [ '@id_to_check', '@id_that_doesnt_work' ] |
| 114 | + id_to_check: |
| 115 | + class: Kiboko\Component\Flow\Magento2\Filter |
| 116 | + arguments: |
| 117 | + - 'entity_id' |
| 118 | + - 'eq' |
| 119 | + - '12' |
| 120 | + id_that_doesnt_work: |
| 121 | + class: Kiboko\Component\Flow\Magento2\Filter |
| 122 | + arguments: |
| 123 | + - 'entity_id' |
| 124 | + - 'eq' |
| 125 | + - '64' |
| 126 | +# ... |
| 127 | +``` |
| 128 | + |
| 129 | +### Building a lookup |
| 130 | +There is a lookup class for Categories, and one for product attributes. |
| 131 | + |
| 132 | +{{< tabs name="lookup">}} |
| 133 | + |
| 134 | +{{< tab name="Category" codelang="yaml">}} |
| 135 | +custom: |
| 136 | + transformer: |
| 137 | + use: 'Kiboko\Component\Flow\Magento2\CategoryLookup' |
| 138 | + services: |
| 139 | + Kiboko\Component\Flow\Magento2\CategoryLookup: |
| 140 | + public: true |
| 141 | + arguments: |
| 142 | + - '@Monolog\Logger' |
| 143 | + - '@Kiboko\Magento\V2_3\Client' # Client to use depending on the Magento version. |
| 144 | + # Available clients are: |
| 145 | + # V2_1, V2_2, V2_3, V2_4 |
| 146 | + - '@Symfony\Component\Cache\Psr16Cache' |
| 147 | + - 'category.%s' |
| 148 | + - '@Acme\Magento\LookupMapper' # Your custom mapper class |
| 149 | + - 'category_name' # Index of the search criteria. |
| 150 | + # In the case of the CategoryLookup, it should be the category ID. |
| 151 | + # Here we temporarily store the category id in this field. |
| 152 | + # LookupMapper will then replace it with the actual name. |
| 153 | + |
| 154 | + Kiboko\Magento\V2_3\Client: |
| 155 | + factory: |
| 156 | + class: 'Kiboko\Magento\V2_3\Client' |
| 157 | + method: 'create' |
| 158 | + arguments: |
| 159 | + - '@Http\Client\Common\PluginClient' |
| 160 | + Http\Client\Common\PluginClient: |
| 161 | + arguments: |
| 162 | + - '@GuzzleHttp\Client' |
| 163 | + - [ '@Http\Client\Common\Plugin\BaseUriPlugin', '@Http\Client\Common\Plugin\AuthenticationPlugin' ] |
| 164 | + GuzzleHttp\Client: ~ |
| 165 | + Http\Client\Common\Plugin\BaseUriPlugin: |
| 166 | + arguments: |
| 167 | + - '@GuzzleHttp\Psr7\Uri' |
| 168 | + Http\Client\Common\Plugin\AuthenticationPlugin: |
| 169 | + arguments: |
| 170 | + - '@Http\Message\Authentication\Bearer' |
| 171 | + GuzzleHttp\Psr7\Uri: |
| 172 | + arguments: |
| 173 | + - 'http://example-magento.com' # URL of the website |
| 174 | + Http\Message\Authentication\Bearer: |
| 175 | + arguments: |
| 176 | + - '12345abcde' # Access token |
| 177 | + |
| 178 | + Symfony\Component\Cache\Psr16Cache: |
| 179 | + arguments: |
| 180 | + - '@Symfony\Component\Cache\Adapter\ApcuAdapter' |
| 181 | + Symfony\Component\Cache\Adapter\ApcuAdapter: ~ |
| 182 | + |
| 183 | + # Your custom mapper class |
| 184 | + Acme\Magento\LookupMapper: ~ |
| 185 | + |
| 186 | + Monolog\Logger: |
| 187 | + arguments: |
| 188 | + - 'app' |
| 189 | + - [ '@Monolog\Handler\StreamHandler' ] |
| 190 | + Monolog\Handler\StreamHandler: |
| 191 | + arguments: |
| 192 | + - 'var/dev.log' # Path to the log file |
| 193 | + - 300 # Log level. 300 for Warning, 200 for Info... |
| 194 | +{{< /tab >}} |
| 195 | + |
| 196 | +{{< tab name="Product attribute" codelang="yaml">}} |
| 197 | +custom: |
| 198 | + transformer: |
| 199 | + use: 'Kiboko\Component\Flow\Magento2\Lookup' |
| 200 | + services: |
| 201 | + Kiboko\Component\Flow\Magento2\Lookup: |
| 202 | + public: true |
| 203 | + arguments: |
| 204 | + - '@Monolog\Logger' |
| 205 | + - '@Kiboko\Magento\V2_3\Client' # Client to use depending on the Magento version. |
| 206 | + # Available clients are: |
| 207 | + # V2_1, V2_2, V2_3, V2_4 |
| 208 | + - '@Symfony\Component\Cache\Psr16Cache' |
| 209 | + - 'collection.%s' # Cache key |
| 210 | + - '@Acme\Magento\LookupMapper' # Your custom mapper class |
| 211 | + - 'Collection' # Index of the search criteria. |
| 212 | + - 'qv_collection' # attribute code |
| 213 | + |
| 214 | + Kiboko\Magento\V2_3\Client: |
| 215 | + factory: |
| 216 | + class: 'Kiboko\Magento\V2_3\Client' |
| 217 | + method: 'create' |
| 218 | + arguments: |
| 219 | + - '@Http\Client\Common\PluginClient' |
| 220 | + Http\Client\Common\PluginClient: |
| 221 | + arguments: |
| 222 | + - '@GuzzleHttp\Client' |
| 223 | + - [ '@Http\Client\Common\Plugin\BaseUriPlugin', '@Http\Client\Common\Plugin\AuthenticationPlugin' ] |
| 224 | + GuzzleHttp\Client: ~ |
| 225 | + Http\Client\Common\Plugin\BaseUriPlugin: |
| 226 | + arguments: |
| 227 | + - '@GuzzleHttp\Psr7\Uri' |
| 228 | + Http\Client\Common\Plugin\AuthenticationPlugin: |
| 229 | + arguments: |
| 230 | + - '@Http\Message\Authentication\Bearer' |
| 231 | + GuzzleHttp\Psr7\Uri: |
| 232 | + arguments: |
| 233 | + - 'http://example-magento.com' # URL of the website |
| 234 | + Http\Message\Authentication\Bearer: |
| 235 | + arguments: |
| 236 | + - '12345abcde' # Access token of the website |
| 237 | + |
| 238 | + Symfony\Component\Cache\Psr16Cache: |
| 239 | + arguments: |
| 240 | + - '@Symfony\Component\Cache\Adapter\ApcuAdapter' |
| 241 | + Symfony\Component\Cache\Adapter\ApcuAdapter: ~ |
| 242 | + |
| 243 | + # Your custom mapper class |
| 244 | + Acme\Magento\LookupMapper: ~ |
| 245 | + |
| 246 | + Monolog\Logger: |
| 247 | + arguments: |
| 248 | + - 'app' |
| 249 | + - [ '@Monolog\Handler\StreamHandler' ] |
| 250 | + Monolog\Handler\StreamHandler: |
| 251 | + arguments: |
| 252 | + - 'var/dev.log' # Path to the log file |
| 253 | + - 300 # Log level. 300 for Warning, 200 for Info... |
| 254 | +{{< /tab >}} |
| 255 | + |
| 256 | +{{< /tabs >}} |
| 257 | + |
| 258 | +Next, you will need to create the `LookupMapper`, a class that implements `Kiboko\Contract\Mapping\CompiledMapperInterface`. |
| 259 | +Its purpose is to merge the result of the lookup back into the line. |
| 260 | + |
| 261 | +In the case of a CategoryLookup, the line has a field `category_name` that contains the category ID. |
| 262 | +We want to replace that ID with the actual category name that the lookup has found. |
| 263 | + |
| 264 | +`$output` is your line, and `$input` is the result of the Magento lookup. |
| 265 | + |
| 266 | +```php |
| 267 | +<?php |
| 268 | +
|
| 269 | +declare(strict_types=1); |
| 270 | +
|
| 271 | +namespace Acme\Magento; |
| 272 | +
|
| 273 | +use Kiboko\Contract\Mapping\CompiledMapperInterface; |
| 274 | +
|
| 275 | +class LookupMapper implements CompiledMapperInterface |
| 276 | +{ |
| 277 | + public function __invoke($input, $output = null) |
| 278 | + { |
| 279 | + $output['category_name'] = $input->getName(); |
| 280 | +
|
| 281 | + return $output; |
| 282 | + } |
| 283 | +} |
| 284 | +``` |
0 commit comments