Product Disclaimer Management with ACF

Serious beared men gathered around vintage computers

The client’s woocommerce products require specific disclaimers, with predefined disclaimers available for certain product types and categories.

Setting Up Disclaimers

We manage these disclaimers using Advanced Custom Fields (ACF) within the theme settings, where different disclaimers are defined under theme options. Specifically, the disclaimers for different product types are defined in the ACF options under theme settings. The available options include:

  • None
  • Disclaimer 1
  • Disclaimer 2
  • Disclaimer 3
  • Disclaimer 4
  • Custom Disclaimer
  • Generic Disclaimer

These options appear as radio buttons on the product editing page, allowing the person setting up the product to select the appropriate disclaimer. There is also an option to enter a custom disclaimer if none of the predefined options are suitable.

Disclaimers 1 through 4 and the Generic Disclaimer appear in the admin under Theme Settings, which is an ACF options interface. These disclaimers are set up using ACF Option Pages, which let us store global values that can be accessed throughout the site. In this case, they’ve been set up using StoutLogic.

Code Sample: Setting Up ACF Fields. Below is the code for setting up these fields within ACF using the ACF Builder library.

include_once('acf-blocks.php');
use StoutLogic\AcfBuilder\FieldsBuilder;
if( function_exists('acf_add_options_page') ) {

    acf_add_options_page(array(
        'page_title' 	=> 'General Theme Settings',
        'menu_title'	=> 'Theme Settings',
        'menu_slug' 	=> 'theme-general-settings',
        'capability'	=> 'edit_posts',
        'redirect'		=> false,
        'update_button'		=> __('Save Theme Options', 'acf'),
        'updated_message'	=> __("Theme Update", 'acf'),

    ));

}

$theme_settings = new FieldsBuilder('settings');
$theme_settings
...
    ->addTab('product_inner_settings')
        ->addRepeater('disclaimers', ['layout' => 'block'])
            ->addWysiwyg('disclaimer1')
            ->addWysiwyg('disclaimer2')
            ->addWysiwyg('disclaimer3')
            ->addWysiwyg('disclaimer4')
            ->addWysiwyg('generic_disclaimer')
        ->endRepeater()
        ->addRepeater('custom_tooltips', ['layout' => 'block'])
            ->addTextarea('title')
            ->addWysiwyg('tooltip_content')
        ->endRepeater()
    ->setLocation('options_page', '==', 'theme-general-settings');

add_action('acf/init', function() use ($theme_settings) {
    acf_add_local_field_group($theme_settings->build());
});

Rendering Disclaimers on the Product Page

When a product page is rendered, a disclaimer field appears within an accordion component. This field outputs the disclaimer selected during product setup.

Code Sample: Determining Which Disclaimer to Render

function custom_display_product_disclaimer() {
	$product_disclaimer         = get_field( 'product_disclaimer' );
	$display_product_disclaimer = false;
	$disclaimer_text            = '';

	if ( $product_disclaimer !== 'none' ) {
		$display_product_disclaimer = true;

		switch ( $product_disclaimer ) {
			case 'disclaimer1':
				$disclaimer_text = get_option( 'options_disclaimers_0_disclaimer1_disclaimer' );
				break;
			case 'disclaimer2':
				$disclaimer_text = get_option( 'options_disclaimers_0_disclaimer2_disclaimer' );
				break;
			case 'custom':
				$disclaimer_text = get_field( 'custom_product_disclaimer' );
				break;
			case 'generic':
				$disclaimer_text = get_option( 'options_disclaimers_0_generic_disclaimer' );
				break;
			case 'disclaimer3':
				$disclaimer_text = get_option( 'options_disclaimers_0_disclaimer3_disclaimer' );
				break;
			case 'disclaimer4':
				$disclaimer_text = get_option( 'options_disclaimers_0_disclaimer4_disclaimer' );
				break;
			default:
				$disclaimer_text = '';
				break;
		}
	}

	return array(
		'show_disclaimer' => $display_product_disclaimer,
		'disclaimer'      => $disclaimer_text,
	);
}

Code Sample: Rendering the Disclaimer

if ( $disclaimer['show_disclaimer'] ) :
    echo wp_kses_post( $disclaimer['disclaimer'] );
endif;

Handling Empty or Null Disclaimers

If one of the predefined product type disclaimers is selected but no disclaimer text has been entered in the theme settings (resulting in a null or empty string), the disclaimer field will not output anything.

Code Sample: Handling Empty or Null Disclaimers

if ( strip_tags( $disclaimer_text ) == '' || $disclaimer_text == null ) {
    $display_product_disclaimer = false;
}

New Requirement: Displaying a Generic Disclaimer by Default

After implementing the initial setup, a new requirement has emerged. The boss wants all product pages to display a generic disclaimer by default. This means that if “none” is selected or if the selected disclaimer is empty, the generic disclaimer should be displayed instead.

Code Sample: Displaying the Generic Disclaimer by Default

function plk_display_product_disclaimer() {
	$product_disclaimer         = get_field( 'product_disclaimer' );
	$display_product_disclaimer = false;
	$disclaimer_text            = '';

	if ( 'none' !== $product_disclaimer ) {
		$display_product_disclaimer = true;

		switch ( $product_disclaimer ) {
			case 'disclaimer1':
				$disclaimer_text = get_option( 'options_disclaimers_0_disclaimer1_disclaimer' );
				break;
			case 'disclaimer2':
				$disclaimer_text = get_option( 'options_disclaimers_0_disclaimer2_disclaimer' );
				break;
			case 'custom':
				$disclaimer_text = get_field( 'custom_product_disclaimer' );
				break;
			case 'generic':
				$disclaimer_text = get_option( 'options_disclaimers_0_generic_disclaimer' );
				break;
			case 'disclaimer3':
				$disclaimer_text = get_option( 'options_disclaimers_0_disclaimer3_disclaimer' );
				break;
			case 'disclaimer4':
				$disclaimer_text = get_option( 'options_disclaimers_0_disclaimer4_disclaimer' );
				break;
			default:
				$disclaimer_text = '';
				break;
		}
	}

	// Fallback to generic disclaimer if the selected one is empty or null.
	if ( strip_tags( $disclaimer_text ) == '' || $disclaimer_text == null ) {
		$disclaimer_text = get_option( 'options_disclaimers_0_generic_disclaimer' );
	}

	// Ensure the disclaimer is only shown if there is valid text.
	$display_product_disclaimer = ! empty( $disclaimer_text );

	return array(
		'show_disclaimer' => $display_product_disclaimer,
		'disclaimer'      => $disclaimer_text,
	);
}

Leave a Reply

Your email address will not be published. Required fields are marked *