订单编辑页面中的WooCommerce自定义字段 [英] WooCommerce custom field in Orders edit page

查看:69
本文介绍了订单编辑页面中的WooCommerce自定义字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

// 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
    }
    }

当不再有产品在商店时-当不再获得ID时,就会出现问题/错误.如何在获取ID之前控制是否存在产品ID?尝试过其他解决方案并没有帮助我.有人在帮助我吗?

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(混合$ object,字符串$ method_name):bool

检查给定对象中是否存在类方法.

Checks if the class method exists in the given object.

您碰巧使用了较早版本的WooCommerce或Wordpress吗?

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);

这篇关于订单编辑页面中的WooCommerce自定义字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆