@@ -87,10 +87,26 @@ This does not work when full page cache is enabled and Varnish returns HTML dire
87
87
88
88
## Listening to events and updating matched customers
89
89
90
- Segments needs to listen to some events related to conditions. After segment with conditions is saved,
91
- table ` magento_customersegment_event ` holds events that it listens to.
90
+ Segments needs to listen to some events related to conditions.
92
91
93
- Segment rule table holds rendered ` condition_sql ` column with placeholders:
92
+ After segment with conditions is saved, table ` magento_customersegment_event ` holds events that it listens to:
93
+
94
+ - Model\Segment::beforeSave
95
+ - Model\Segment::collectMatchedEvents
96
+ - recursively for each added conditions children:
97
+ - Model\Condition\Combine\AbstractCombine::getMatchedEvents
98
+
99
+
100
+ Segment rule table holds rendered ` condition_sql ` :
101
+
102
+ - Model\Segment::beforeSave
103
+ - Model\Rule\Condition\Combine\Root::getConditionsSql
104
+ - recursively for each added conditions children:
105
+ - Model\Condition\Combine\AbstractCombine::getConditionsSql
106
+ - ` _prepareConditionsSql `
107
+
108
+
109
+ ` condition_sql ` column placeholders:
94
110
- : customer_id
95
111
- : website_id
96
112
- : quote_id
@@ -182,11 +198,35 @@ Does 2 things:
182
198
// - clear checkout session
183
199
```
184
200
185
- ## All Depersonalize examples
186
-
187
- \Magento\Catalog\Model\Layout\DepersonalizePlugin::afterGenerateElements:
188
- - clear catalog session
189
-
201
+ ## Create segment programmatically
202
+
203
+ Doesn't have API interfaces, repository.
204
+
205
+ ```PHP
206
+ /** @var \Magento\CustomerSegment\Model\Segment $segment */
207
+ $segment = $this->segmentFactory->create();
208
+ $segment->setName($name);
209
+ $segment->setDescription(null);
210
+ $segment->setIsActive(1);
211
+ $segment->setWebsiteIds(array_keys($this->storeManager->getWebsites()));
212
+ $segment->setApplyTo(\Magento\CustomerSegment\Model\Segment::APPLY_TO_VISITORS_AND_REGISTERED);
213
+ // @see \Magento\Rule\Model\AbstractModel::loadPost
214
+ $segment->getConditions()->setConditions([])->loadArray([
215
+ 'type' => \Magento\CustomerSegment\Model\Segment\Condition\Combine\Root::class,
216
+ 'aggregator' => 'all',
217
+ 'operator' => '',
218
+ 'value' => '1',
219
+ 'conditions' => [
220
+ [
221
+ 'type' => \Magento\CustomerSegment\Model\Segment\Condition\Shoppingcart\Amount::class,
222
+ 'attribute' => 'grand_total',
223
+ 'operator' => '>=',
224
+ 'value' => '100',
225
+ ]
226
+ ],
227
+ ]);
228
+ $segment->save();
229
+ ```
190
230
191
231
192
232
## Catalog frontend action
0 commit comments