2019-06-17 04:38:42 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Plugin Name: WooCommerce Admin Table Column Example
|
|
|
|
*
|
2020-08-11 19:18:47 +00:00
|
|
|
* @package WooCommerce\Admin
|
2019-06-17 04:38:42 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register the JS.
|
|
|
|
*/
|
|
|
|
function table_column_register_script() {
|
|
|
|
|
2019-08-12 21:52:09 +00:00
|
|
|
if ( ! class_exists( 'Automattic\WooCommerce\Admin\Loader' ) || ! \Automattic\WooCommerce\Admin\Loader::is_admin_page() ) {
|
2019-06-17 04:38:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
wp_register_script(
|
|
|
|
'table_column',
|
|
|
|
plugins_url( '/dist/index.js', __FILE__ ),
|
|
|
|
array(
|
|
|
|
'wp-hooks',
|
|
|
|
'wp-element',
|
|
|
|
'wp-i18n',
|
|
|
|
'wc-components',
|
|
|
|
),
|
|
|
|
filemtime( dirname( __FILE__ ) . '/dist/index.js' ),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
wp_enqueue_script( 'table_column' );
|
|
|
|
}
|
|
|
|
add_action( 'admin_enqueue_scripts', 'table_column_register_script' );
|
|
|
|
|
2019-06-27 14:47:58 +00:00
|
|
|
/**
|
|
|
|
* When adding fields to a REST API response, we need to update the schema
|
|
|
|
* to reflect these changes. Not only does this let API consumers know of
|
|
|
|
* the new fields, it also adds them to Report Exports.
|
|
|
|
*
|
|
|
|
* @param array $properties The endpoint item schema properties.
|
|
|
|
* @return array Filtered schema.
|
|
|
|
*/
|
|
|
|
function add_product_extended_attributes_schema( $properties ) {
|
|
|
|
$properties['extended_info']['average_rating'] = array(
|
|
|
|
'type' => 'number',
|
|
|
|
'readonly' => true,
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'description' => 'Average product rating.',
|
|
|
|
);
|
|
|
|
|
|
|
|
return $properties;
|
|
|
|
}
|
|
|
|
|
|
|
|
add_filter( 'woocommerce_rest_report_products_schema', 'add_product_extended_attributes_schema' );
|
|
|
|
|
2019-06-17 04:38:42 +00:00
|
|
|
/**
|
|
|
|
* Extended attributes can be used to obtain any attribute from a WC_Product instance that is
|
|
|
|
* available by a `get_*` class method. In other words, we can add `average_rating` because
|
|
|
|
* `get_average_rating` is an available method on a WC_Product instance.
|
|
|
|
*
|
|
|
|
* @param array $extended_attributes - Extra information from WC_Product instance.
|
|
|
|
* @return array - Extended attributes.
|
|
|
|
*/
|
|
|
|
function add_product_extended_attributes( $extended_attributes ) {
|
|
|
|
$extended_attributes[] = 'average_rating';
|
|
|
|
return $extended_attributes;
|
|
|
|
}
|
|
|
|
add_filter( 'woocommerce_rest_reports_products_extended_attributes', 'add_product_extended_attributes' );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|