From 1730578a23e38a6afbc5bbd2b55b24d06e2eb41b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Lys=C3=BD?= Date: Tue, 17 Jan 2023 18:06:46 +0100 Subject: [PATCH] Use coupon id instead of text [MAILPOET-4761] --- .../js/src/newsletter_editor/blocks/coupon.ts | 18 ++++++++++++++---- .../lib/Newsletter/Renderer/Blocks/Coupon.php | 2 +- mailpoet/lib/WooCommerce/Helper.php | 7 +++++-- .../templates/blocks/coupon/settings.hbs | 2 +- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/mailpoet/assets/js/src/newsletter_editor/blocks/coupon.ts b/mailpoet/assets/js/src/newsletter_editor/blocks/coupon.ts index 61c653f6ee..4fcf5c4cad 100644 --- a/mailpoet/assets/js/src/newsletter_editor/blocks/coupon.ts +++ b/mailpoet/assets/js/src/newsletter_editor/blocks/coupon.ts @@ -177,7 +177,9 @@ Module.CouponBlockSettingsView = base.BlockSettingsView.extend({ availableDiscountTypes: App.getConfig() .get('coupon.discount_types') .toJSON(), - availableCoupons: App.getConfig().get('coupon.available_coupons'), + availableCoupons: App.getConfig() + .get('coupon.available_coupons') + .toJSON(), }, ); }, @@ -238,9 +240,10 @@ Module.CouponBlockSettingsView = base.BlockSettingsView.extend({ }) .on({ 'select2:select': function (event) { - const coupon = event.params.data.text; - model.set('existingCoupon', coupon); - model.set('code', coupon); + const couponId = event.params.data.id; + const couponCode = event.params.data.text; + model.set('couponId', couponId); + model.set('code', couponCode); }, }) .trigger('change'); @@ -258,6 +261,7 @@ Module.CouponBlockSettingsView = base.BlockSettingsView.extend({ ); // reset code placeholder this.model.set('code', App.getConfig().get('coupon.code_placeholder')); + this.model.set('couponId', null); } else if (value === 'useExisting') { this.$('.mailpoet_field_coupon_source_create_new').addClass( 'mailpoet_hidden', @@ -268,6 +272,12 @@ Module.CouponBlockSettingsView = base.BlockSettingsView.extend({ // set selected code from available this.model.set( 'code', + this.$('.mailpoet_field_coupon_existing_coupon') + .find(':selected') + .html(), + ); + this.model.set( + 'couponId', this.$('.mailpoet_field_coupon_existing_coupon') .find(':selected') .val(), diff --git a/mailpoet/lib/Newsletter/Renderer/Blocks/Coupon.php b/mailpoet/lib/Newsletter/Renderer/Blocks/Coupon.php index 33cc51a6ff..d20c0ead03 100644 --- a/mailpoet/lib/Newsletter/Renderer/Blocks/Coupon.php +++ b/mailpoet/lib/Newsletter/Renderer/Blocks/Coupon.php @@ -23,7 +23,7 @@ class Coupon { public function render($element, $columnBaseWidth) { $couponCode = self::CODE_PLACEHOLDER; if (!empty($element['couponId'])) { - $couponCode = $this->helper->wcGetCouponCodeById($element['couponId']); + $couponCode = $this->helper->wcGetCouponCodeById((int)$element['couponId']); } $element['styles']['block']['width'] = $this->calculateWidth($element, $columnBaseWidth); $styles = 'display:inline-block;-webkit-text-size-adjust:none;mso-hide:all;text-decoration:none;text-align:center;' . StylesHelper::getBlockStyles($element, $exclude = ['textAlign']); diff --git a/mailpoet/lib/WooCommerce/Helper.php b/mailpoet/lib/WooCommerce/Helper.php index c74ca0e40f..af1b6cb78a 100644 --- a/mailpoet/lib/WooCommerce/Helper.php +++ b/mailpoet/lib/WooCommerce/Helper.php @@ -188,8 +188,11 @@ class Helper { 'post_status' => 'publish', ]); - return array_map(function(\WP_Post $post): string { - return $post->post_title; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps + return array_map(function(\WP_Post $post): array { + return [ + 'id' => $post->ID, + 'text' => $post->post_title, // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps + ]; }, $couponPosts); } } diff --git a/mailpoet/views/newsletter/templates/blocks/coupon/settings.hbs b/mailpoet/views/newsletter/templates/blocks/coupon/settings.hbs index 03c0f45fa5..9a28a9addb 100644 --- a/mailpoet/views/newsletter/templates/blocks/coupon/settings.hbs +++ b/mailpoet/views/newsletter/templates/blocks/coupon/settings.hbs @@ -50,7 +50,7 @@