WC_Cart::generate_cart_id() – Generate a unique ID for the cart item being added.

You appear to be a bot. Output may be restricted

Description

Generate a unique ID for the cart item being added.

Usage

$string = WC_Cart::generate_cart_id( $product_id, $variation_id, $variation, $cart_item_data );

Parameters

$product_id
( int ) required – id of the product the key is being generated for.
$variation_id
( int ) optional – of the product the key is being generated for.
$variation
( array ) optional – data for the cart item.
$cart_item_data
( array ) optional – other cart item data passed which affects this items uniqueness in the cart.

Returns

string cart item key

Source

File name: woocommerce/includes/class-wc-cart.php
Lines:

1 to 29 of 29
  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 );
  }
 

 View on GitHub View on Trac