add_filter('wpo_search_product_types', function ($types) {
  $types[] = 'composite';
  return $types;
});

add_filter('wpo_skip_add_to_cart_item', function ($skip, $item) {
  return isset($item['composite_parent']) && $item['composite_parent'] ? true : $skip;
}, 10, 2);

add_filter('wpo_is_child_cart_item', function ($is_child, $item) {
  return isset($item['composite_parent']) && $item['composite_parent'] ? true : $is_child;
}, 10, 2);

add_filter('wpo_children_cart_item', function ($children, $item) {
  return isset($item['composite_children']) ? $item['composite_children'] : $children;
}, 10, 2);

add_filter('wpo_cart_item_is_price_readonly', function ($is_readonly, $cart_item_data) {
  return isset($cart_item_data['composite_parent']) && $cart_item_data['composite_parent'] || isset($cart_item_data['composite_children']) && $cart_item_data['composite_children'] ? true : $is_readonly;
}, 10, 2);

add_filter('wpo_update_cart_cart_item_meta', function ($cart_item_meta, $item) {
  return array_merge($cart_item_meta, array(
    'composite_data' => isset($item['composite_data']) ? $item['composite_data'] : null,
    'readonly_price' => isset($item['readonly_price']) ? $item['readonly_price'] : null,
  ));
}, 10, 2);

add_filter('wpo_update_cart_loaded_product', function ($loaded_product, $item) {
  return array_merge($loaded_product, array(
    'composite_data' => isset($item['composite_data']) ? $item['composite_data'] : null,
    'readonly_price' => isset($item['readonly_price']) ? $item['readonly_price'] : null,
  ));
}, 10, 2);

add_filter( 'wpo_get_item_by_product', function ( $product, $cart_item_data ) {
  $composite_data = isset( $cart_item_data['composite_data'] ) ? $cart_item_data['composite_data'] : null;
  $readonly_price = isset( $cart_item_data['readonly_price'] ) ? $cart_item_data['readonly_price'] : null;


  if ( ! $composite_data && isset( $cart_item_data['meta_data'] ) && is_array( $cart_item_data['meta_data'] ) ) {
    foreach ( $cart_item_data['meta_data'] as $index => $meta ) {
      $d = $meta->get_data();
      if ( '_composite_data' === $d['key'] ) {
        $composite_data = $d['value'];
        break;
      }
    }
  }

  foreach ( $product['custom_meta_fields'] as $index => $meta_field ) {
    if ( '_composite_data' === $meta_field['meta_key'] ) {
      unset( $product['custom_meta_fields'][ $index ] );
      break;
    }
  }

  return array_merge( $product, array(
    'composite_data' => $composite_data,
    'readonly_price' => $readonly_price,
  ) );
}, 10, 2 );

add_filter('wpo_add_configured_products_skip_item', function ($skip, $cart_item_data) {
  return isset($cart_item_data['composite_parent']) ? true : $skip;
}, 10, 2);

add_filter('wpo_load_order_skip_item', function ($skip, $order_item, $order) {
  $item_data  = $order_item->get_data();
  if (isset($item_data['meta_data']) && is_array($item_data['meta_data'])) {
    foreach ($item_data['meta_data'] as $meta) {
      $d = $meta->get_data();
      if ( '_composite_parent' === $d['key'] ) {
          return true;
            }
        }
  }

  return $skip;
}, 10, 3);

add_filter('wpo_add_configured_products_item_data', function ($item_data, $cart_item_data, $cart_item_key, $wc_product) {
  if ( isset( $cart_item_data['composite_children'] ) ) {
    $readonly_price = apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $wc_product ), $cart_item_data, $cart_item_key );
    $available_symbols = implode("|", array('\\' . wc_get_price_decimal_separator(), '\\' . wc_get_price_thousand_separator()));

    $pattern = "/<\/span>([\d|{$available_symbols}]+)?/i";

    if (preg_match($pattern, $readonly_price, $matches)) {
      $item_data['readonly_price'] = $matches[1];
    }
  }
  return $item_data;
}, 10, 4);