Skip to content

Commit 1ecc894

Browse files
authored
Update Customer segments.md
1 parent 820a6fd commit 1ecc894

File tree

1 file changed

+48
-8
lines changed

1 file changed

+48
-8
lines changed

xx. Magento Commerce Features/Customer segments.md

+48-8
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,26 @@ This does not work when full page cache is enabled and Varnish returns HTML dire
8787

8888
## Listening to events and updating matched customers
8989

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.
9291

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:
94110
- :customer_id
95111
- :website_id
96112
- :quote_id
@@ -182,11 +198,35 @@ Does 2 things:
182198
// - clear checkout session
183199
```
184200
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+
```
190230

191231

192232
## Catalog frontend action

0 commit comments

Comments
 (0)