WC_Cart::generate_cart_id()publicWC 1.0

Generate a unique ID for the cart item being added.

Метод класса: WC_Cart{}

Хуки из метода

Возвращает

Строку. cart item key

Использование

$WC_Cart = new WC_Cart();
$WC_Cart->generate_cart_id( $product_id, $variation_id, $variation, $cart_item_data );
$product_id(int) (обязательный)
- id of the product the key is being generated for.
$variation_id(int)
of the product the key is being generated for.
$variation(массив)
data for the cart item.
По умолчанию: array()
$cart_item_data(массив)
other cart item data passed which affects this items uniqueness in the cart.
По умолчанию: array()

Код WC_Cart::generate_cart_id() WC 8.7.0

public function generate_cart_id( $product_id, $variation_id = 0, $variation = array(), $cart_item_data = array() ) {
	$id_parts = array( $product_id );

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

	if ( is_array( $variation ) && ! empty( $variation ) ) {
		$variation_key = '';
		foreach ( $variation as $key => $value ) {
			$variation_key .= trim( $key ) . trim( $value );
		}
		$id_parts[] = $variation_key;
	}

	if ( is_array( $cart_item_data ) && ! empty( $cart_item_data ) ) {
		$cart_item_data_key = '';
		foreach ( $cart_item_data as $key => $value ) {
			if ( is_array( $value ) || is_object( $value ) ) {
				$value = http_build_query( $value );
			}
			$cart_item_data_key .= trim( $key ) . trim( $value );

		}
		$id_parts[] = $cart_item_data_key;
	}

	return apply_filters( 'woocommerce_cart_id', md5( implode( '_', $id_parts ) ), $product_id, $variation_id, $variation, $cart_item_data );
}