Пользовательское поле WooCommerce на странице редактирования заказов - PullRequest
2 голосов
/ 13 апреля 2020
// 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
    }
    }

Проблема / ошибка возникает, когда товара больше нет в магазине - когда больше нет идентификатора. Как контролировать, если существует идентификатор продукта, прежде чем получить идентификатор? Пробовал разные решения, ничего не помогая мне там. Кто-нибудь помогает мне в этом?

1 Ответ

1 голос
/ 14 апреля 2020

Я не могу воспроизвести ошибку, но, исходя из ошибки, я полагаю, вы можете использовать следующее.

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

Проверяет, существует ли метод класса в данном объекте.

Вы случайно не использовали более старую версию WooCommerce или 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);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...