Home > Net >  Display price suffix based on custom field in WooCommerce product data "general" settings
Display price suffix based on custom field in WooCommerce product data "general" settings

Time:03-08

I would like to add custom price suffix based on custom field. I tried to combine a few ready-made codes on functions.php.

add_action('woocommerce_product_options_general_product_data', 'woocommerce_product_custom_fields');
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');

function woocommerce_product_custom_fields()
{
    global $woocommerce, $post;
    echo '<div >';
    woocommerce_wp_text_input(
        array(
            'id' => '_custom_product_text_field',
            'label' => __('Taksit:', 'woocommerce'),
        )
    );
    echo '</div>';
}
function woocommerce_product_custom_fields_save($post_id)
{
        $woocommerce_custom_product_text_field = $_POST['_custom_product_text_field'];
        if (!empty($woocommerce_custom_product_text_field))
            update_post_meta($post_id, '_custom_product_text_field', esc_attr($woocommerce_custom_product_text_field));
}

add_filter( 'woocommerce_get_price_suffix', 'price_taksit_ekle', 99, 4 );
  
function price_taksit_ekle( $html, $product, $price, $qty ){
    $html = get_post_meta($post->ID, '_custom_product_text_field', true);
    return $html;
}

However this results in the following notice: Attempt to read property "ID" on null.

Any advice?

CodePudding user response:

Your code contains some minor mistakes:

  • To save fields you can use the woocommerce_admin_process_product_object hook, opposite the outdated woocommerce_process_product_meta hook
  • $post is undefined when using the woocommerce_get_price_suffix filter hook

So you get:

// 1. Add to general_product_data tab
function action_woocommerce_product_options_general_product_data() {
    // Text field
    woocommerce_wp_text_input( array(
        'id'                 => '_custom_product_text_field',
        'label'              => __( 'My sufix', 'woocommerce' ),
        'placeholder'        => '',
        'description'        => __( 'Add your custom sufix', 'woocommerce' ),
        'desc_tip'           => true,
    ));
}
add_action( 'woocommerce_product_options_general_product_data', 'action_woocommerce_product_options_general_product_data', 10, 0 );

// 2. Save custom field
function action_woocommerce_admin_process_product_object( $product ) {
    // Isset
    if ( isset( $_POST['_custom_product_text_field'] ) ) {        
        // Update
        $product->update_meta_data( '_custom_product_text_field', sanitize_text_field( $_POST['_custom_product_text_field'] ) );
    }
}
add_action( 'woocommerce_admin_process_product_object', 'action_woocommerce_admin_process_product_object', 10, 1 );

// 3a. Add sufix
function filter_woocommerce_get_price_suffix( $html, $product, $price, $qty ) {
    // Get meta
    $suffix = $product->get_meta( '_custom_product_text_field' );

    // NOT empty
    if ( ! empty ( $suffix ) ) {
        $html .= $suffix;
    // Else condition is OPTIONAL and can be REMOVED if desired
    } else {
        $html .= __( 'Default', 'woocommerce' ); 
    }

    return $html;
}
add_filter( 'woocommerce_get_price_suffix', 'filter_woocommerce_get_price_suffix', 10, 4 );

EDIT: Additional question - "Is it possible to show the suffix just only on the single product page and how can I add a class to suffix?"

Yes, use this modified (replace 3a with 3b) code instead:

// 3b. Add sufix
function filter_woocommerce_get_price_suffix( $html, $product, $price, $qty ) {
    // Only on single product page
    if ( ! is_product() ) return $html;

    // Get meta
    $suffix = $product->get_meta( '_custom_product_text_field' );

    // NOT empty
    if ( ! empty ( $suffix ) ) {
        $html .= '<span >' . $suffix . '</span>';
    // Else condition is OPTIONAL and can be REMOVED if desired
    } else {
        $html .= '<span >' . __( 'Default', 'woocommerce' ) . '</span>'; 
    }

    return $html;
}
add_filter( 'woocommerce_get_price_suffix', 'filter_woocommerce_get_price_suffix', 10, 4 );
  • Related