WC_Cart::generate_cart_id()
Generate a unique ID for the cart item being added.
Method of the class: WC_Cart{}
Hooks from the method
Return
String
. cart item key
Usage
$WC_Cart = new WC_Cart(); $WC_Cart->generate_cart_id( $product_id, $variation_id, $variation, $cart_item_data );
- $product_id(int) (required)
- - id of the product the key is being generated for.
- $variation_id(int)
- of the product the key is being generated for.
- $variation(array)
- data for the cart item.
Default: array() - $cart_item_data(array)
- other cart item data passed which affects this items uniqueness in the cart.
Default: array()
WC_Cart::generate_cart_id() WC Cart::generate cart id code WC 9.7.1
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 ); }