SOLVED: Woocommerce custom snippet display error {echo:...}

Bricks Version: 1.5 beta

Hi Bricks Brothers!

I make a php code snippet to my Bricks Builder child theme.
In Custom single product template i try to print the data.
It is printed, but wrong place.

What i do wrong?


// -----------------------------------------
// 1. Add RRP field input @ product edit page

add_action( 'woocommerce_product_options_pricing', 'bbloomer_add_RRP_to_products' );      
  
function bbloomer_add_RRP_to_products() {          
    woocommerce_wp_text_input( array( 
        'id' => 'rrp', 
        'class' => 'short wc_input_price', 
        'label' => __( 'Listaár', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ')',
        'data_type' => 'price', 
    ));      
}

// -----------------------------------------
// 2. Save RRP field via custom field

add_action( 'save_post_product', 'bbloomer_save_RRP' );
  
function bbloomer_save_RRP( $product_id ) {
    global $typenow;
    if ( 'product' === $typenow ) {
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
        if ( isset( $_POST['rrp'] ) ) {
            update_post_meta( $product_id, 'rrp', $_POST['rrp'] );
        }
    }
}

// -----------------------------------------
// 3. Display RRP field @ single product page

function bbloomer_display_RRP() {
    global $product;
    if ( $product->get_type() <> 'variable' && $rrp = get_post_meta( $product->get_id(), 'rrp', true ) ) {
        echo '<div class="woocommerce_rrp">';
        echo '<span>' . wc_price( $rrp ) . '</span>';
        echo '</div>';
    }
}

Hello @simplecreative

Thank you for your post.

This is happening because your custom function bbloomer_display_RRP used inside the Dynamic Data echo tag is already doing the echo. It should return the value.

According to the Dynamic Data academy article …the custom PHP function should return the value (do not echo the value inside of the custom function).

1 Like

Oh my good! Good solution!
I change my snippet code, and its works!

Thanks your support!

The first and second steps are same, under the changed third step!

// -----------------------------------------
// 3. Display RRP field @ single product page

function bbloomer_display_RRP() {
global $product;
if ( $product->get_type() <> ‘variable’ && $rrp = get_post_meta( $product->get_id(), ‘rrp’, true ) ) {
return ‘’ . wc_price( $rrp ) . ‘’;
}
}

1 Like