简体   繁体   中英

Hide custom field label on the order edit page (backend), if it is empty in WooCommerce

I'm using the following code to display field label for "shipping_company" field, on the edit page.

// Display field value for shipping company field 
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'delivery_date_and_time', 10, 1 );
function delivery_date_and_time($order){
  echo '<p><strong>'.__('Delivery Date & Time').':</strong> ' . get_post_meta( $order->get_id(), 
'_shipping_company', true ) . '</p>';
}

Now, I'm trying to hide the label, if it was not filled by the customer, when they placed the order.

You could add an if condition to get_post_meta , if NOT empty.. echo

// Display field value for shipping company field 
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'delivery_date_and_time', 10, 1 );
function delivery_date_and_time($order) {
    $shipping_company = get_post_meta( $order->get_id(), '_shipping_company', true );
    
    // NOT empty
    if ( ! empty ( $shipping_company ) ) {
        echo '<p><strong>' . __( 'Delivery Date & Time', 'woocommerce' ) . ':</strong> ' . $shipping_company . '</p>';
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM