Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unique_id field to product #47364

Draft
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 23 additions & 1 deletion plugins/woocommerce/includes/abstracts/abstract-wc-product.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class WC_Product extends WC_Abstract_Legacy_Product {
'description' => '',
'short_description' => '',
'sku' => '',
'unique_id' => '',
'price' => '',
'regular_price' => '',
'sale_price' => '',
Expand Down Expand Up @@ -251,7 +252,7 @@ public function get_short_description( $context = 'view' ) {
}

/**
* Get SKU (Stock-keeping unit) - product unique ID.
* Get SKU (Stock-keeping unit).
*
* @param string $context What the value is for. Valid values are view and edit.
* @return string
Expand All @@ -260,6 +261,17 @@ public function get_sku( $context = 'view' ) {
return $this->get_prop( 'sku', $context );
}

/**
* Get Unique ID.
*
* @since 9.0.0
* @param string $context What the value is for. Valid values are view and edit.
* @return string
*/
public function get_unique_id( $context = 'view' ) {
return $this->get_prop( 'unique_id', $context );
}

/**
* Returns the product's active price.
*
Expand Down Expand Up @@ -835,6 +847,16 @@ public function set_sku( $sku ) {
$this->set_prop( 'sku', $sku );
}

/**
* Set unique_id
*
* @since 9.0.0
* @param string $unique_id Unique ID.
*/
public function set_unique_id( $unique_id ) {
$this->set_prop( 'unique_id', $unique_id );
}

/**
* Set the product's active price.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
protected $internal_meta_keys = array(
'_visibility',
'_sku',
'_unique_id',
'_price',
'_regular_price',
'_sale_price',
Expand Down Expand Up @@ -325,6 +326,7 @@ protected function read_product_data( &$product ) {
$post_meta_values = get_post_meta( $id );
$meta_key_to_props = array(
'_sku' => 'sku',
'_unique_id' => 'unique_id',
'_regular_price' => 'regular_price',
'_sale_price' => 'sale_price',
'_price' => 'price',
Expand Down Expand Up @@ -516,6 +518,7 @@ protected function read_downloads( &$product ) {
protected function update_post_meta( &$product, $force = false ) {
$meta_key_to_props = array(
'_sku' => 'sku',
'_unique_id' => 'unique_id',
'_regular_price' => 'regular_price',
'_sale_price' => 'sale_price',
'_sale_price_dates_from' => 'date_on_sale_from',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,11 @@ protected function prepare_object_for_database( $request, $creating = false ) {
$product->set_sku( wc_clean( $request['sku'] ) );
}

// Unique ID.
if ( isset( $request['unique_id'] ) ) {
$product->set_unique_id( wc_clean( $request['unique_id'] ) );
}

// Attributes.
if ( isset( $request['attributes'] ) ) {
$attributes = array();
Expand Down Expand Up @@ -991,6 +996,11 @@ public function get_item_schema() {
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'unique_id' => array(
'description' => __( 'Unique identifier.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'price' => array(
'description' => __( 'Current product price.', 'woocommerce' ),
'type' => 'string',
Expand Down Expand Up @@ -1662,6 +1672,10 @@ protected function get_product_data( $product, $context = 'view' ) {
$data['post_password'] = $product->get_post_password( $context );
}

if ( in_array( 'unique_id', $fields, true ) ) {
$data['unique_id'] = $product->get_unique_id( $context );
}

$post_type_obj = get_post_type_object( $this->post_type );
if ( is_post_type_viewable( $post_type_obj ) && $post_type_obj->public ) {
$permalink_template_requested = in_array( 'permalink_template', $fields, true );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -797,11 +797,42 @@ private function add_inventory_group_blocks() {
'order' => 10,
)
);
$product_inventory_inner_section->add_block(
$inventory_columns = $product_inventory_inner_section->add_block(
array(
'id' => 'product-inventory-inner-columns',
'blockName' => 'core/columns',
)
);
$inventory_columns->add_block(
array(
'id' => 'product-inventory-inner-column1',
'blockName' => 'core/column',
)
)->add_block(
array(
'id' => 'product-sku-field',
'blockName' => 'woocommerce/product-sku-field',
'order' => 10,
'disableConditions' => array(
array(
'expression' => 'editedProduct.type === "variable"',
),
),
)
);
$inventory_columns->add_block(
array(
'id' => 'product-inventory-inner-column2',
'blockName' => 'core/column',
)
)->add_block(
array(
'id' => 'product-unique-id-field',
'blockName' => 'woocommerce/product-text-field',
'attributes' => array(
'property' => 'unique_id',
'label' => __( 'GTIN, UPC, EAN or ISBN', 'woocommerce' ),
'tooltip' => __( 'Enter a barcode or any other identifier unique to this product. It can help you list this product on other channels or marketplaces.', 'woocommerce' ),
),
'disableConditions' => array(
array(
'expression' => 'editedProduct.type === "variable"',
Expand Down