是否可以扩展woocommerce产品的其他端点?

时间:2019-09-20 07:24:37

标签: php wordpress woocommerce woocommerce-rest-api

有没有一种方法可以以无损方式扩展woocommerce rest api /wp-json/wc/v3/products中的products-object,因此使用该端点的插件不会损坏。

我目前已经尝试创建自己的剩余端点来复制对象,但是现在缺少大量数据。

我也尝试过类似的方法,但是似乎没有用。

add_filter( 'woocommerce_rest_prepare_product', 'wc_rest_api_add_custom_data_to_product', 90, 2 );
function wc_rest_api_add_custom_data_to_product( $response, $post ) {

  // retrieve a custom field and add it to API response
  $response->data['your_new_key'] = get_post_meta( $post->ID, '_your_custom_filed_key', true );

  return $response;

}
  

来源:https://francescocarlucci.com/woocommerce/woocommerce-api-custom-data-default-endpoints/

在这方面的任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

我找到了一种使用register_rest_field扩展端点的方法。

这样,我既可以扩展(添加)和/或覆盖(更新)对象。

function register_images_field() {
        register_rest_field(
            'product',
            'images',
            array(
                'get_callback'    => function ( $object ) {
                    $medium_large     = wp_get_attachment_image_src( get_post_thumbnail_id( $object['id'] ), 'medium_large' );
                    $medium_large_url = $medium_large['0'];
                    $large            = wp_get_attachment_image_src( get_post_thumbnail_id( $object['id'] ), 'large' );
                    $large_url        = $large['0'];
​
                    return array(
                        'medium_large' => $medium_large_url,
                        'large'        => $large_url,
                    );
                },
                'update_callback' => null,
                'schema'          => null,
            )
        );
    }

来源:https://developer.wordpress.org/reference/functions/register_rest_field/

答案 1 :(得分:1)

这是一种更改价格的方法。也许您可以使用相同的过程?

## The following goes inside the constructor ##

// Simple, grouped and external products
add_filter('woocommerce_product_get_price', array( $this, 'custom_price' ), 99, 2 );
add_filter('woocommerce_product_get_regular_price', array( $this, 'custom_price' ), 99, 2 );
// Variations 
add_filter('woocommerce_product_variation_get_regular_price', array( $this, 'custom_price' ), 99, 2 );
add_filter('woocommerce_product_variation_get_price', array( $this, 'custom_price' ), 99, 2 );

// Variable (price range)
add_filter('woocommerce_variation_prices_price', array( $this, 'custom_variable_price' ), 99, 3 );
add_filter('woocommerce_variation_prices_regular_price', array( $this, 'custom_variable_price' ), 99, 3 );

// Handling price caching (see explanations at the end)
add_filter( 'woocommerce_get_variation_prices_hash', array( $this, 'add_price_multiplier_to_variation_prices_hash' ), 99, 1 );


## This goes outside the constructor ##

// Utility function to change the prices with a multiplier (number)
public function get_price_multiplier() {
    return 2; // x2 for testing
}

public function custom_price( $price, $product ) {
    return $price * get_price_multiplier();
}

public function custom_variable_price( $price, $variation, $product ) {
    return $price * get_price_multiplier();
}

public function add_price_multiplier_to_variation_prices_hash( $hash ) {
    $hash[] = get_price_multiplier();
    return $hash;
}

来源:Change product prices via a hook in WooCommerce 3