Automattic\WooCommerce\Internal\ShopperLists

ShopperListItem::generate_keyprivate staticWC 1.0

Compute a deterministic item key. Mirrors WC_Cart::generate_cart_id() so the same product+variation always hashes to the same key, regardless of the input key order for variation attributes.

Method of the class: ShopperListItem{}

No Hooks.

Returns

null. Nothing (null).

Usage

$result = ShopperListItem::generate_key( $product_id, $variation_id, $variation ): string;
$product_id(int) (required)
Product ID.
$variation_id(int) (required)
Variation ID, or 0.
$variation(array) (required)
Variation attributes.

ShopperListItem::generate_key() code WC 11.0.1

private static function generate_key( int $product_id, int $variation_id, array $variation ): string {
	$id_parts = array( $product_id );

	if ( $variation_id ) {
		$id_parts[] = $variation_id;
	}

	if ( ! empty( $variation ) ) {
		ksort( $variation );
		$variation_key = '';
		foreach ( $variation as $k => $v ) {
			$variation_key .= trim( (string) $k ) . trim( (string) $v );
		}
		$id_parts[] = $variation_key;
	}

	return md5( implode( '_', $id_parts ) );
}