|
20 | 20 |
|
21 | 21 | namespace Meta\Promotions\Model\Promotion\Feed;
|
22 | 22 |
|
| 23 | +use Magento\SalesRule\Api\Data\CouponInterface; |
| 24 | +use Magento\SalesRule\Model\Coupon; |
23 | 25 | use Meta\BusinessExtension\Helper\FBEHelper;
|
24 | 26 | use Meta\BusinessExtension\Model\System\Config as SystemConfig;
|
25 | 27 | use Magento\SalesRule\Model\ResourceModel\Rule\CollectionFactory as RuleCollection;
|
@@ -53,6 +55,7 @@ class Builder
|
53 | 55 | private const ATTR_USE_AUTO_GENERATION = 'use_auto_generation';
|
54 | 56 | private const ATTR_USES_PER_COUPON = 'uses_per_coupon';
|
55 | 57 | private const ATTR_PRIMARY_COUPON = 'primary_coupon';
|
| 58 | + private const ATTR_COUPONS = 'coupons'; |
56 | 59 | private const ATTR_SIMPLE_FREE_SHIPPING = 'simple_free_shipping';
|
57 | 60 | private const ATTR_STORE_LABELS = 'store_labels';
|
58 | 61 | private const ATTR_WEBSITE_IDS = 'website_ids';
|
@@ -158,6 +161,7 @@ public function buildPromoEntry(Rule $rule): array
|
158 | 161 | self::ATTR_USE_AUTO_GENERATION => $useAutoGeneration,
|
159 | 162 | self::ATTR_USES_PER_COUPON => $rule->getUsesPerCoupon(),
|
160 | 163 | self::ATTR_PRIMARY_COUPON => $rule->getCouponCode(),
|
| 164 | + self::ATTR_COUPONS => $this->getAllCouponsSerialized($rule), |
161 | 165 | self::ATTR_SIMPLE_FREE_SHIPPING => $this->getSimpleFreeShippingAsString($rule),
|
162 | 166 | self::ATTR_STORE_LABELS => json_encode($rule->getStoreLabels()),
|
163 | 167 | self::ATTR_WEBSITE_IDS => json_encode($rule->getWebsiteIds()),
|
@@ -216,6 +220,55 @@ private function getSimpleFreeShippingAsString(Rule $rule): string
|
216 | 220 | }
|
217 | 221 | }
|
218 | 222 |
|
| 223 | + /** |
| 224 | + * Get coupon creation type string |
| 225 | + * |
| 226 | + * @param Coupon $coupon |
| 227 | + * @return string |
| 228 | + */ |
| 229 | + private function getCouponCreationTypeAsString(Coupon $coupon): string |
| 230 | + { |
| 231 | + $type = $coupon->getType(); |
| 232 | + switch ($type) { |
| 233 | + case CouponInterface::TYPE_MANUAL: |
| 234 | + return 'type_manual'; |
| 235 | + case CouponInterface::TYPE_GENERATED: |
| 236 | + return 'type_generated'; |
| 237 | + default: |
| 238 | + return 'type_unknown'; |
| 239 | + } |
| 240 | + } |
| 241 | + |
| 242 | + /** |
| 243 | + * Get all coupons |
| 244 | + * |
| 245 | + * @param Rule $rule |
| 246 | + * @return string |
| 247 | + */ |
| 248 | + private function getAllCouponsSerialized(Rule $rule): string |
| 249 | + { |
| 250 | + $coupons = $rule->getCoupons(); |
| 251 | + array_unshift($coupons, $rule->getPrimaryCoupon()); |
| 252 | + |
| 253 | + $coupons = array_map( |
| 254 | + function (Coupon $coupon): array { |
| 255 | + return [ |
| 256 | + 'id' => $coupon->getCouponId(), |
| 257 | + 'code' => $coupon->getCode(), |
| 258 | + 'usage_limit' => $coupon->getUsageLimit(), |
| 259 | + 'usage_per_customer' => $coupon->getUsagePerCustomer(), |
| 260 | + 'times_used' => $coupon->getTimesUsed(), |
| 261 | + 'is_primary' => $this->boolFlagToString((int)$coupon->getIsPrimary()), |
| 262 | + 'created_at' => $coupon->getCreatedAt(), |
| 263 | + 'type' => $this->getCouponCreationTypeAsString($coupon), |
| 264 | + 'extension_attributes' => $coupon->getExtensionAttributes() |
| 265 | + ]; |
| 266 | + }, |
| 267 | + $coupons |
| 268 | + ); |
| 269 | + return json_encode($coupons); |
| 270 | + } |
| 271 | + |
219 | 272 | /**
|
220 | 273 | * Get header fields
|
221 | 274 | *
|
@@ -248,6 +301,7 @@ public function getHeaderFields(): array
|
248 | 301 | self::ATTR_USE_AUTO_GENERATION,
|
249 | 302 | self::ATTR_USES_PER_COUPON,
|
250 | 303 | self::ATTR_PRIMARY_COUPON,
|
| 304 | + self::ATTR_COUPONS, |
251 | 305 | self::ATTR_SIMPLE_FREE_SHIPPING,
|
252 | 306 | self::ATTR_STORE_LABELS,
|
253 | 307 | self::ATTR_WEBSITE_IDS,
|
|
0 commit comments