简体   繁体   中英

WooCommerce custom field in Orders edit page

// Display custom field Orders edit page
add_action('woocommerce_before_order_itemmeta', 'storage_location_of_order_items', 10, 3);
function storage_location_of_order_items($item_id, $item, $product)
{
    // Only on backend order edit pages
    if (!(is_admin() && $item->is_type('line_item'))) return;

// Get your '_laoriiul' value (replace the slug by yours below)
    $custom_ladu = get_post_meta($product->get_id(), '_laoriiul', true); //Error message: "Uncaught Error: Call to a member function get_id() on bool in functions.php:211"  - when product not aviable (line 211)
     if (isset($custom_ladu)) { // only show the custom SKU if it's set
        echo "<br>" . wp_kses_post("Laoriiul: $custom_ladu"); // change this line if needed
    }
    }

The problem/error occurs when not product in shop anymore - when not get ID anymore. How to control if exist product id before get id? Have tried different solutions nothing helping me there. Anyone helping me in that?

I'm not able to reproduce the error, but starting from the error I suppose you can use the following.

method_exists ( mixed $object, string $method_name ): bool

Checks if the class method exists in the given object.

Do you happen to use an older version of WooCommerce or Wordpress?

// Display custom field Orders edit page
function storage_location_of_order_items( $item_id, $item, $product ) { 
    // Only on backend order edit pages
    if (!(is_admin() && $item->is_type('line_item'))) return;

    // Checks if the class method exists
    if ( method_exists( $product, 'get_id' ) ) {
        // Get product id
        $product_id = $product->get_id();

        // Get your '_laoriiul' value (replace the slug by yours below)
        $custom_ladu = get_post_meta( $product_id, '_laoriiul', true);

        if ( isset( $custom_ladu ) ) { // only show the custom SKU if it's set
            echo "<br>" . wp_kses_post( $custom_ladu ); // change this line if needed
        }
    }
}
add_action('woocommerce_before_order_itemmeta', 'storage_location_of_order_items', 10, 3);

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