gpt4 book ai didi

wordpress - 错误 Woocommerce REST API 扩展订单 "line_items"响应

转载 作者:行者123 更新时间:2023-12-04 07:50:50 24 4
gpt4 key购买 nike

我正在尝试为 Orders 端点中的 line_items 创建一个新值,但它显示了一个我不知道如何解决的错误!
代码来自:Woocommerce REST API extending order response
其他有用的链接:

  • https://developer.wordpress.org/reference/functions/register_rest_field/
  • https://francescocarlucci.com/woocommerce/woocommerce-api-custom-data-default-endpoints/
  • Is it possible to extend the woocommerce products rest endpoint?

  • 代码
    function get_product_order_image( $response, $object, $request ) {

    if( empty( $response->data ) )
    return $response;
    $order_pid= $response->data['line_items'][0]['product_id'];
    $l_w_product_meta = get_post_meta($response->data['line_items'][0]['product_id']);
    $order_imgUrl= wp_get_attachment_url( $l_w_product_meta['_thumbnail_id'][0], 'full' );

    $response->data['line_items'][0]['cover_image'] = $order_imgUrl;

    return $response;
    }

    add_filter( "woocommerce_rest_prepare_shop_order_object", array( $this, "get_product_order_image"), 10, 3 );
    错误:
    由于文件 wp-content/themes/store-child/functions.php 的第 336 行出错,您的 PHP 代码更改已回滚。请修复并再次尝试保存。
    Uncaught Error: Using $this when not in object context in wp-content/themes/store-child/functions.php:336
    Stack trace:
    #0 wp-settings.php(528): include()
    #1 wp-config.php(89): require_once('/kunden/631823_...')
    #2 wp-load.php(37): require_once('/kunden/631823_...')
    #3 wp-admin/admin.php(34): require_once('/kunden/631823_...')
    #4 wp-admin/theme-editor.php(10
    谢谢!!

    最佳答案

    如果该函数需要添加到主题的 functions.php 而不是在类中,请尝试替换:

    add_filter( "woocommerce_rest_prepare_shop_order_object", array( $this, "get_product_order_image"), 10, 3 );
    和:
    add_filter( "woocommerce_rest_prepare_shop_order_object", "get_product_order_image", 10, 3 );
    它应该可以解决您发布的错误。

    关于wordpress - 错误 Woocommerce REST API 扩展订单 "line_items"响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66987696/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com