Skip to content

Commit 4d739d6

Browse files
agmurariAishu Murari
andauthored
add coupons column to promo feed file (#502)
* add coupons column to promo feed file * serialize coupon data * fix static tests * move lambda function inline --------- Co-authored-by: Aishu Murari <[email protected]>
1 parent 11c7199 commit 4d739d6

File tree

1 file changed

+54
-0
lines changed
  • app/code/Meta/Promotions/Model/Promotion/Feed

1 file changed

+54
-0
lines changed

app/code/Meta/Promotions/Model/Promotion/Feed/Builder.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
namespace Meta\Promotions\Model\Promotion\Feed;
2222

23+
use Magento\SalesRule\Api\Data\CouponInterface;
24+
use Magento\SalesRule\Model\Coupon;
2325
use Meta\BusinessExtension\Helper\FBEHelper;
2426
use Meta\BusinessExtension\Model\System\Config as SystemConfig;
2527
use Magento\SalesRule\Model\ResourceModel\Rule\CollectionFactory as RuleCollection;
@@ -53,6 +55,7 @@ class Builder
5355
private const ATTR_USE_AUTO_GENERATION = 'use_auto_generation';
5456
private const ATTR_USES_PER_COUPON = 'uses_per_coupon';
5557
private const ATTR_PRIMARY_COUPON = 'primary_coupon';
58+
private const ATTR_COUPONS = 'coupons';
5659
private const ATTR_SIMPLE_FREE_SHIPPING = 'simple_free_shipping';
5760
private const ATTR_STORE_LABELS = 'store_labels';
5861
private const ATTR_WEBSITE_IDS = 'website_ids';
@@ -158,6 +161,7 @@ public function buildPromoEntry(Rule $rule): array
158161
self::ATTR_USE_AUTO_GENERATION => $useAutoGeneration,
159162
self::ATTR_USES_PER_COUPON => $rule->getUsesPerCoupon(),
160163
self::ATTR_PRIMARY_COUPON => $rule->getCouponCode(),
164+
self::ATTR_COUPONS => $this->getAllCouponsSerialized($rule),
161165
self::ATTR_SIMPLE_FREE_SHIPPING => $this->getSimpleFreeShippingAsString($rule),
162166
self::ATTR_STORE_LABELS => json_encode($rule->getStoreLabels()),
163167
self::ATTR_WEBSITE_IDS => json_encode($rule->getWebsiteIds()),
@@ -216,6 +220,55 @@ private function getSimpleFreeShippingAsString(Rule $rule): string
216220
}
217221
}
218222

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+
219272
/**
220273
* Get header fields
221274
*
@@ -248,6 +301,7 @@ public function getHeaderFields(): array
248301
self::ATTR_USE_AUTO_GENERATION,
249302
self::ATTR_USES_PER_COUPON,
250303
self::ATTR_PRIMARY_COUPON,
304+
self::ATTR_COUPONS,
251305
self::ATTR_SIMPLE_FREE_SHIPPING,
252306
self::ATTR_STORE_LABELS,
253307
self::ATTR_WEBSITE_IDS,

0 commit comments

Comments
 (0)