WC_Product::set_global_unique_idpublicWC 9.1.0

Set global_unique_id

Method of the class: WC_Product{}

No Hooks.

Returns

null. Nothing (null).

Usage

$WC_Product = new WC_Product();
$WC_Product->set_global_unique_id( $global_unique_id );
$global_unique_id(string) (required)
Unique ID.

Changelog

Since 9.1.0 Introduced.

WC_Product::set_global_unique_id() code WC 10.9.1

public function set_global_unique_id( $global_unique_id ) {
	// Strip characters that are never valid (digits, hyphens, and X/x for the ISBN-10 check digit).
	$global_unique_id = preg_replace( '/[^0-9Xx\-]/', '', (string) $global_unique_id );

	// X is only valid as the final ISBN-10 check digit character (ISO 2108) — reject anywhere else.
	if ( $this->get_object_read() && ! empty( $global_unique_id ) && ! preg_match( '/^[0-9\-]*[0-9Xx]?$/', $global_unique_id ) ) {
		$this->error(
			'product_invalid_global_unique_id_format',
			__( 'Invalid GTIN, UPC, EAN, or ISBN. The letter X is only valid as the final ISBN-10 check digit.', 'woocommerce' ),
			400
		);
	}

	if ( $this->get_object_read() && ! empty( $global_unique_id ) && ! wc_product_has_global_unique_id( $this->get_id(), $global_unique_id ) ) {
		$global_unique_id_found = wc_get_product_id_by_global_unique_id( $global_unique_id );

		$this->error(
			'product_invalid_global_unique_id',
			__( 'Invalid or duplicated GTIN, UPC, EAN or ISBN.', 'woocommerce' ),
			400,
			array(
				'resource_id' => $global_unique_id_found,
			)
		);
	}
	$this->set_prop( 'global_unique_id', $global_unique_id );
}