DrMTR
DrMTR

Reputation: 509

WooComerce Custom Field show only in default site language?

I have one issue that dont know how to resolve myself, so need your help. I have inserted Custom Field into my WooCommerce site product page. I have used this code to show my custom field bellow "Add to Cart" page, and also bellow product into cart page.

// Display Product settings Fields
add_action( 'woocommerce_product_options_general_product_data', 
'woo_add_custom_general_fields' );
function woo_add_custom_general_fields() {
woocommerce_wp_text_input( array(
    'id'          => '_pd_number',
    'label'       => __( 'Delivery Time', 'woocommerce' ),
    'placeholder' => 'Some text here',
    'desc_tip'    => 'true',
    'description' => __( 'Enter some text', 'woocommerce' )
));
}

// Save Product settings Fields
add_action( 'woocommerce_process_product_meta', 
'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields_save( $post_id ){
$pd_number = $_POST['_pd_number'];
if( !empty( $pd_number ) )
    update_post_meta( $post_id, '_pd_number', esc_attr( $pd_number ) );
}
// Show text in Product Page
add_action( 'woocommerce_after_add_to_cart_button', 
'add_cf_before_addtocart_in_single_products', 1, 0 );
 function add_cf_before_addtocart_in_single_products()
{
 global $product;

 $pd_number = get_post_meta( $product->get_id(), '_pd_number', true );
 if( !empty( $pd_number ) )
    echo '<div class="pd-number">'. $pd_number .'</div><br>';

}
// Displaying the product custom field in the Cart items
add_filter( 'woocommerce_cart_item_name', 'add_cf_after_cart_item_name', 10, 
 3 );
function add_cf_after_cart_item_name( $name_html, $cart_item, $cart_item_key 
)
{
$product_id = $cart_item['product_id'];
if( $cart_item['variation_id'] > 0 )
    $product_id = $cart_item['variation_id'];

 $pd_number = get_post_meta( $product_id, '_pd_number', true );;
 if( !empty( $pd_number ) )
    $name_html .= '<br><span class="pd-number">'.$pd_number .'</span>';

 return $name_html;
}

but show only on my default site language Dansk. If choise English from top bar, custom field is not shown. I use WPML as language plugin. Any help ?

Upvotes: 1

Views: 158

Answers (1)

Ajay Ghaghretiya
Ajay Ghaghretiya

Reputation: 822

Please see In this image, if you put the blank custom field in other languages then it does not appear in front end

Upvotes: 2

Related Questions