0

In checkout i moved "Apply Discount Code" form from afterMethods to beforeMethods.

After upgrade to 2.4.5 recaptcha was added to this form.

But doesnt load correctly

<div data-bind="{
    attr: {
        'id': getReCaptchaId() + '-wrapper'
    },
    'afterRender': renderReCaptcha()
}" id="recaptcha-checkout-coupon-apply-wrapper">
    <div class="g-recaptcha"></div>
    <!-- ko if: (!getIsInvisibleRecaptcha()) -->
    <div class="field">
        <div class="control">
            <input type="checkbox" value="" class="required-captcha checkbox" name="recaptcha-validate-" data-validate="{required:true}">
        </div>
    </div>
    <!-- /ko -->
</div>

This happen even when is disabled.

If I left the form in afterMethods works correctly.

Any suggestion?

1 Answer 1

0

The ReCaptchaCheckoutSalesRule module adds a new layoutProcessor.

In my code the form was moved using a plugin on the file Magento\Checkout\Block\Checkout\LayoutProcessor

In this way the form was moved before the recaptcha was configured using the function:

public function process($jsLayout)
{
    $key = 'coupon_code';
    if ($this->isCaptchaEnabled->isCaptchaEnabledFor($key)) {
        $jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']
        ['payment']['children']['afterMethods']['children']['discount']['children']
        ['checkout_sales_rule']['settings'] = $this->captchaUiConfigResolver->get($key);
    } else {
        if (isset($jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']
            ['payment']['children']['afterMethods']['children']['discount']['children']['checkout_sales_rule'])) {
            unset($jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']
                ['payment']['children']['afterMethods']['children']['discount']['children']['checkout_sales_rule']);
        }
    }

    return $jsLayout;
}

in Magento\ReCaptchaCheckoutSalesRule\Block\LayoutProcessor\Checkout\Onepage

So I had to create a new plugin for the latter file and move the code from the previous plugin to the new one

<?php
namespace Vendor\Module\Plugin\Checkout;
class MoveDiscountForm
{
    protected $logger;
    
    public function __construct(
        \Psr\Log\LoggerInterface $logger
    )
    {
        $this->logger = $logger;
    }

    /**
     * @param \Magento\Checkout\Block\Checkout\LayoutProcessor $subject
     * @param array $jsLayout
     * @return array
     */
    public function afterProcess(
        \Magento\ReCaptchaCheckoutSalesRule\Block\LayoutProcessor\Checkout\Onepage $subject,
        array  $jsLayout
    ) {     
        //move discount to beforeMethods

        if (isset($jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']
            ['payment']['children']['afterMethods']['children']['discount'])) {
            
            $discountFormBefore = &$jsLayout['components']['checkout']['children']['steps']
            ['children']['billing-step']['children']['payment']['children']
            ['beforeMethods']['children']['discount'];
            
            $discountFormBefore = $jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']
            ['payment']['children']['afterMethods']['children']['discount'];
             
            unset($jsLayout['components']['checkout']['children']['steps']
            ['children']['billing-step']['children']['payment']
            ['children']['afterMethods']['children']['discount']);
        }
        
        return $jsLayout;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.