php - 订单编辑页面中的 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?尝试过不同的解决方案对我没有帮助。有人帮我吗?
解决方案
我无法重现该错误,但从错误开始,我想您可以使用以下内容。
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);
推荐阅读
- javascript - 如何在JS中将数字转换为布尔值?
- sql - SQL 错误 "Argument not specified for parameter" 添加此 Fillby3 引起了问题,我并不真正精通 SQL。有什么建议么?
- python - 如何在 Python 中禁用 chromedriver“Chrome 正在由自动测试软件控制”通知?
- reactjs - 反应 useEffect 和 setInterval
- reactjs - 使用 docker-compose 的 ASP.NET Core 5.0 React app npm 错误
- security - 我的身份验证流程是否安全/最佳实践?
- python - 从 Jenkins 收集所有日志到 excel 文件
- python - 如何使用线程加速读取 zip 文件
- numpy - Numpy vs Eigen vs Xtensor 线性代数基准奇数
- scala - org.specs2.specification.core.AsExecution[scala.util.Try[org.specs2.matcher.MatchResult[String]]] 类型的证据参数的 specs2 隐式值