Search code examples
phpwordpresswoocommercecode-snippets

PHP Code Snippet Not Working in CartFlows (WooCommerce Plugin)


I followed THIS tutorial to change the coupon field and button text, using the following script in a Code Snippet:

/**
 * Change the CartFlows Coupon Field text and Button text.
 *
 * @param array $coupon_field array of field strings/texts.
 * @return array
 */
add_filter( 'cartflows_coupon_field_options', 'change_cartFlows_coupon_field_strings', 10, 1 );

function change_cartFlows_coupon_field_strings( $coupon_field ){

    $coupon_field = array(
        'field_text'  => __( 'Coupon Code', 'cartflows' ), // Coupon input field text/placeholder.
        'button_text' => __( 'Apply', 'cartflows' ), // Apply coupon button text.
        'class'       => '', // Coupon input field class.
    );

    return $coupon_field; // Returning the modified data with new strings.
}

This script worked as expected. However, the coupon codes are collapsible when clicking on "Have a coupon?". To change the title "Have a coupon?" to another language, I attempted to use this code:

// Attempted script to change the coupon title
function cartFlows_rename_coupon_message_on_checkout() {
    return 'Have a Promo Code?' . ' <a href="#" class="showcoupon">' . __( 'Click here to enter your code', 'cartFlows' ) . '</a>';
}
add_filter( 'cartFlows_checkout_coupon_message', 'cartFlows_rename_coupon_message_on_checkout' );

However, this script doesn't seem to work in this scenario. I'm unable to determine the cause of this issue. Below, I'm including a snippet of the HTML for reference:

<div class="wcf-custom-coupon-field wcf-hide-field" id="wcf_custom_coupon_field"><a href="#" id="wcf_optimized_wcf_custom_coupon_field"><div class="dashicons dashicons-arrow-right"></div> Have a coupon?</a>
            <div class="wcf-coupon-col-1">
                <span>
                    <input type="text" name="coupon_code" class="input-text wcf-coupon-code-input" placeholder="Coupon code" id="coupon_code" value="">
                </span>
            </div>
            <div class="wcf-coupon-col-2">
                <span>
                    <button type="button" class="button wcf-submit-coupon wcf-btn-small" name="apply_coupon" value="Apply">Apply</button>
                </span>
            </div>
        </div>

Solution

  • There is no 'cartFlows_checkout_coupon_message' filter in CartFlows.

    I think you meant to use the 'woocommerce_checkout_coupon_message' filter

    add_filter( 'woocommerce_checkout_coupon_message', 'cartFlows_rename_coupon_message_on_checkout' );
    
    function cartFlows_rename_coupon_message_on_checkout( $html ){
    
        return 'Have a Promo Code?' . ' <a href="#" class="showcoupon">' . __( 'Click here to enter your code', 'cartFlows' ) . '</a>';
    
    }