Code Snippets https://cartflows.com/docs-category/cartflows/code-snippets/ Mon, 07 Jul 2025 16:22:17 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.2 https://cartflows.com/wp-content/uploads/2025/07/cropped-cartflows-logo-32x32.png Code Snippets https://cartflows.com/docs-category/cartflows/code-snippets/ 32 32 Refresh the Checkout Page After CartFlows AJAX Calls https://cartflows.com/docs/refresh-the-checkout-page-after-cartflows-ajax-calls/ Mon, 07 Jul 2025 12:16:08 +0000 https://cartflows.com/?post_type=docs&p=62415 In some cases, especially when working with dynamic products like subscriptions, the checkout page may not refresh automatically when switching between products or options. This can lead to a mismatch in available payment methods or shipping options.

To resolve this, CartFlows provides a filter that forces the checkout to refresh and trigger the necessary AJAX calls, ensuring the correct data is always loaded on the checkout page.

This is especially useful when:

  • Switching between subscription and non-subscription products
  • Dynamically changing product options that affect checkout behavior
  • Ensuring updated shipping/payment methods based on the product or variation

How to Enable Checkout Refresh After AJAX Calls

Step 1: Add the Filter to Your Site

To make the checkout page refresh automatically, add the following code to your site’s functions.php file or use a code snippet plugin:

add_filter( 'cartflows_checkout_trigger_update_order_review', '__return_true' );

Need help adding custom code? Here’s how to safely add code snippets to your site.

Step 2: Save and Test

After adding the filter, visit your checkout page and test switching between different product types or options. The page will now refresh the order review section as expected, showing the correct payment methods and other dynamically updated options.

When to Use This Filter

This filter is particularly helpful if you:

  • Use WooCommerce Subscriptions along with other types of products with CartFlows funnel checkout
  • Offer products with conditional logic affecting checkout (e.g., special payment gateways for specific items)
  • Need to ensure the checkout page always reflects the latest data without requiring manual updates/refreshes.
]]>
How to Show the Order Summary Open on Mobile Devices https://cartflows.com/docs/how-to-show-the-order-summary-open-on-mobile-devices/ Tue, 11 Oct 2022 12:33:34 +0000 https://cartflows.com/?post_type=docs&p=46384 Starting with CartFlows version 1.9.0, the Modern Checkout templates collapse the order summary by default on mobile devices to create a cleaner and more focused layout.

If you’d prefer the order summary to appear open by default on mobile (while still allowing users to collapse it), you can use the following PHP filter:

add_filter( 'cartflows_show_mobile_order_summary_collapsed', '__return_false' );

You can add this code using the Code Snippets plugin (recommended) or by placing it in your child theme’s functions.php file.

💡 Make sure to select PHP as the snippet type if you’re using the Code Snippets plugin.

For guidance, check out: How to Add Custom Code to WordPress

]]>
How to Enable the Product Tab on Store Checkout https://cartflows.com/docs/enable-product-tab-store-checkout/ Thu, 21 Jul 2022 12:13:07 +0000 https://cartflows.com/?post_type=docs&p=46372 With the release of CartFlows version 1.10.0, we introduced the new Store Checkout feature, which replaces the default WooCommerce checkout and follows a standard, streamlined checkout process.

As part of this transition, the Product tab was removed from the Store Checkout funnel to maintain simplicity and alignment with typical eCommerce flows. However, if you have a unique use case where you want to want the product tab on Store checkout, you can bring back the Product tab by using the following PHP filter:

add_filter( 'cartflows_show_store_checkout_product_tab', '__return_true' );

You can add this code using the Code Snippets plugin or by placing it in your child theme’s functions.php file.

Need help adding custom code? Here’s how to safely add code snippets to your site.

Note: The Product Options feature does not behave the same way in the Store Checkout due to the structure of the store checkout. Some features may have limitations or may not be supported when the Product tab is enabled.

]]>
VAT Field of WooCommerce EU/UK VAT Compliance (Premium) Plugin is not Displaying https://cartflows.com/docs/vat-field-of-eu-uk-vat-not-displaying/ Tue, 25 May 2021 13:43:34 +0000 https://cartflows.com/?post_type=docs&p=38862 If you’re using the WooCommerce EU/UK VAT Compliance (Premium) plugin by David Anderson and the VAT field is not appearing on your CartFlows Checkout page, this documentation will help you display it correctly.

This plugin adds a VAT number field on the WooCommerce checkout page to support tax compliance for EU and UK customers. However, due to how CartFlows customizes the checkout fields, the VAT field may not display by default.

Ensure VAT Field is Displayed on WooCommerce Checkout

Before checking CartFlows compatibility, make sure the VAT field is visible on the default WooCommerce checkout page. If it’s not displayed there, it won’t appear on CartFlows either.

You can confirm this by temporarily switching to the default WooCommerce checkout page and testing whether the VAT field appears as expected.

Why the Field Might Not Show on CartFlows Checkout

CartFlows customizes the WooCommerce checkout form, including billing and shipping fields, using standard WooCommerce hooks and filters. Because of this, fields added by third-party plugins may not always appear in the intended position.

Fortunately, the WooCommerce EU/UK VAT Compliance (Premium) plugin provides a filter that allows you to change the position of the VAT field.

Add the Filter to Display VAT Field on CartFlows Checkout

Add the following filter to your child theme’s functions.php file:

add_filter( 'wc_eu_vat_number_position', 'eu_change_vat_number_position', 10, 1 ); 

/**
* Function to change the position of VAT field only for the CartFlows checkout page.
* 
* @param $current_page current visited page
* @return $current_page current page.
*/
function eu_change_vat_number_position( $position ){

  /* Get global post object */
	global $post;

	/* Get current post type */
	$post_type = get_post_type();
	
	/* Check the current type of the page */
	$is_checkout = get_post_meta( $post->ID, 'wcf-step-type', true );
		
	/* Change position only of the CartFlows checkout page */
	if( 'cartflows_step' == $post_type && 'checkout' === $is_checkout){
		$position = 'woocommerce_after_order_notes';
	}

  return $position;

}

💡 Need help adding custom code? Here’s how to safely add custom snippets to your site.

After adding this filter, the VAT field will display on your CartFlows checkout page in the correct position. If you’ve followed the steps and the field still doesn’t appear, double-check your plugin settings and ensure the field is visible on the standard WooCommerce checkout.

]]>
How to Change the “Choose A variation” text? https://cartflows.com/docs/change-variation-text/ Thu, 30 Jul 2020 08:04:05 +0000 https://cartflows.com/?post_type=docs&p=32997 When using the Product Options feature in CartFlows, customers may see a “Choose a variation” link (if there is a variable product) that opens a variation selection popup. This label can be customized to better match your store’s language, branding, or product tone.

CartFlows does not provide a setting in the dashboard to change this text, but it can be easily modified using a filter we provided.

Step 1: Choose How to Add Custom Code

You can use either of the following methods to add the filter:

  • Use a child theme and place the code in the functions.php file.
  • Use a code snippet plugin like Code Snippets for safer and easier management.

Step 2: Add the Filter Code

Copy and paste the following snippet into your chosen method:

add_filter( 'cartflows_variation_popup_toggle_text', 'change_variation_toggle_text', 10, 1 );

/**
* Change Choose a variation text.
*
* @return text.
*/
function change_variation_toggle_text( $text ) {

return "Add your choice of text here";
}

Step 3: Save Changes and Test

After saving the changes, visit your checkout page where the Product Options popup appears and confirm that the new text displays as expected.

Note: For detailed guidance on adding custom code to your site, please refer to our documentation here.

]]>
How to Allow Cache Plugins to Cache the CartFlows Pages? https://cartflows.com/docs/allow-cache-plugins-to-cache-cartflows-pages/ Thu, 30 Jul 2020 08:01:45 +0000 https://cartflows.com/?post_type=docs&p=32995 By default, CartFlows prevents its pages from being cached by common caching plugins. This is done to avoid potential conflicts and ensure that funnel steps work correctly out of the box.

However, if you understand the implications and want to allow caching on CartFlows pages for performance reasons, this documentation explains how to do it.

Why CartFlows Pages Are Not Cached

CartFlows includes special constants in its code that instruct caching plugins to skip its pages:

  • DONOTCACHEPAGE
  • DONOTCACHEOBJECT
  • DONOTCACHEDB

Many popular caching plugins look for these constants, and when detected, they automatically exclude the page from being cached.

How to Allow Caching for CartFlows Pages

You can disable this restriction by using a filter provided by CartFlows. This filter allows you to override the default behavior and selectively allow caching for specific step pages.

Add This Filter to Your Child Theme

To allow caching, add the following code to your child theme’s functions.php file:

add_filter( 'cartflows_do_not_cache_step', 'cache_CartFlows_pages' );

function cache_CartFlows_pages( $post_id ) {
 
// If you want to remove more pages from the cache then add those page's id in the array.

	if( in_array( $post_id , array( 'add_your_landing_page_ids_comma_separated_id_more_than_one' ) ) ){
		return false; // Cache the Pages.
	}

	return true; // Do Not cache.
}

You can find your step page IDs by going to CartFlows > Funnels, opening the Funnel, then hovering over the Step name to view the ID in the URL.

Step ID

In the screenshot above, the Step ID is 68.

How to Add Custom Code Safely

If you’re not sure how to add code to your functions.php file, follow this guide: How to Add Custom Code to WordPress

]]>
How to Turn Off the Auto-fill of Checkout Fields? https://cartflows.com/docs/turn-off-auto-fill-checkout-fields/ Fri, 10 Jul 2020 10:36:30 +0000 https://cartflows.com/?post_type=docs&p=32887 By default, CartFlows helps improve the checkout experience by automatically filling in previously entered billing or shipping information. This feature is designed to make it faster and easier for returning users to complete their purchase.

CartFlows stores this data locally in the user’s browser—nothing is saved on the server or shared with others. This means the information will only auto-fill for the same user using the same browser and device.

While this improves convenience, there may be scenarios where you prefer to disable this auto-fill behavior—especially on shared devices or for privacy-focused setups.

How to Disable Auto-Fill Using a Filter

To turn off the auto-fill feature, you can use the following filter in your child theme’s functions.php file:

/**
* Filter to enable/disable the auto-fill of checkout fields
*
* @param string $allow having yes/no
* @return string $allow yes or no
*/
add_filter( 'cartflows_allow_persistence', 'do_not_store_persistance_data', 10, 1 );

function do_not_store_persistance_data( $allow ){
$allow = 'no';
return $allow;
}

💡 Need help adding custom code? Here’s how to safely add code snippets to your site.

Once this filter is in place, CartFlows will no longer pre-fill any checkout field data from a user’s previous session.

]]>
How to Enable the Theme’s Scripts & Styles without Changing the Page Template? https://cartflows.com/docs/enable-themes-style-for-existing-layout/ Fri, 10 Jul 2020 10:34:39 +0000 https://cartflows.com/?post_type=docs&p=32873 By default, CartFlows intentionally does not load your theme’s header, footer, or styling on funnel pages. This ensures maximum flexibility and avoids design conflicts—especially helpful when creating focused, distraction-free checkout experiences.

Why the Header & Footer Are Hidden

To maintain full design control and prevent potential layout issues, CartFlows uses its own template for funnel pages. If you’d prefer to show your theme’s header and footer, the recommended approach is to set the page template to Default in the page editor. Here’s how to do that.

However, in some cases, you may notice that the header and footer appear without styling—especially when not using a Default template. If you’d like to load your theme’s CSS and JavaScript files without changing the page template, you can use the following filters.

How to Load Theme Scripts & Styles with a Filter

Add the following code snippet to your child theme’s functions.php file:

/**
* Filter to load theme's styles & scripts.
*
* @param bool $remove_styles true/false
* @return bool $remove_styles true/false
*/

add_filter( 'cartflows_remove_theme_styles', 'wcf_load_theme_files', 10, 1 );
add_filter( 'cartflows_remove_theme_scripts', 'wcf_load_theme_files', 10, 1 );

function wcf_load_theme_files( $remove_styles ){

$remove_styles = false;

return $remove_styles;
}

💡 Need help adding custom code? Follow this guide to safely insert custom code into WordPress.

Once added, your theme’s styles and scripts will load on CartFlows pages—even if you’re not using a Default template.

]]>
How to Turn Off the Auto-fill of Address Fields on Entering Zip-Code? https://cartflows.com/docs/turn-off-auto-fill-address-fields/ Fri, 10 Jul 2020 10:17:44 +0000 https://cartflows.com/?post_type=docs&p=32870 CartFlows includes a convenient feature that automatically fills in the Country, State, and City fields on your checkout page based on the zip code entered by the customer. This functionality is powered by the Zippopotam API, which looks up location details for recognized zip codes and auto-fills the corresponding fields to streamline the checkout process.

However, in some scenarios, you may want to disable this behavior, for example, when using custom fields or serving regions where this data is unreliable.

Disable Auto-Fill Using a Filter

To turn off this auto-fill functionality, you can use the following WordPress filter:

/**
* Filter to enable/disable the address auto-fill using the zip code.
*
* @param string $allow having yes/no
* @return string $allow yes or no
*/

add_filter( 'cartflows_autocomplete_zip_data', 'wa_disable_autocomplete_zipcode', 10, 1 );

function wa_disable_autocomplete_zipcode( $allow ) {
$allow = 'no';
return $allow;
}

How to Add This Code

To apply this filter, add the snippet to your child theme’s functions.php file or use Code Snippets plugin. If you’re unsure how to safely add custom code to your site, we recommend reviewing this guide on adding custom code.

Once added, CartFlows will no longer auto-fill the address fields when a zip code is entered, allowing full manual input from the customer.

]]>