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, $cart_items) {

    $composite_data = isset($item['composite_data']) ? $item['composite_data'] : null;

    if ($composite_data) {

    $_composite_data = $composite_data;
    $composite_data = array();

    foreach ($_composite_data as $composite_item_id => $composite_item_data) {

        //restore origin view of composite data
        $composite_item_id = isset($composite_item_data['key']) ? $composite_item_data['key'] : $composite_item_id;

        //to fix apply changes of missing attributes
        if (isset($composite_item_data['attributes']) && $composite_item_data['attributes']) {

        $cart_item = array_filter($cart_items, function ($item) use ($composite_item_id) {
            return isset($item['composite_item']) && $item['composite_item'] == $composite_item_id;
        });

        $cart_item = array_shift($cart_item);

        if ($cart_item && $cart_item['missing_variation_attributes']) {
            foreach ($cart_item['missing_variation_attributes'] as $missing_attribute) {
            $attribute = 'attribute_' . $missing_attribute['key'];
            if (isset($composite_item_data['attributes'][$attribute])) {
                $composite_item_data['attributes'][$attribute] = $missing_attribute['value'];
            }
            }
        }
        }

        $composite_data[$composite_item_id] = $composite_item_data;
    }
    }

    return array_merge($cart_item_meta, array(
    'composite_data' => $composite_data,
    'readonly_price' => isset($item['readonly_price']) ? $item['readonly_price'] : null,
    ));
}, 10, 3);

add_filter('wpo_update_cart_loaded_product', function ($loaded_product, $item) {

    $data = array();

    if (isset($item['composite_item'])) {
    $data['composite_item'] = $item['composite_item'];
    }

    $composite_data = isset($item['composite_data']) ? $item['composite_data'] : null;

    //to fix origin order of composite items, number keys defaults sorts
    if ($composite_data) {
    $_composite_data = $composite_data;
    $composite_data = array();
    foreach ($_composite_data as $key => $value) {
        $value['key'] = $key;
        $composite_data[] = $value;
    }
    }

    $readonly_price = isset($item['readonly_price']) ? $item['readonly_price'] : null;

    //fix recalculte composite product price
    if (isset($item['composite_children'])) {

    $product_id = $item['variation_id'] ? $item['variation_id'] : $item['product_id'];
    $product = wc_get_product($product_id);

    $price = apply_filters('woocommerce_cart_item_price', WC()->cart->get_product_price($product), $item, $item['wpo_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, $price, $matches)) {
        $readonly_price = str_replace(
            array(wc_get_price_thousand_separator(), wc_get_price_decimal_separator()),
            array('', '.'),
            $matches[1]
        );
    }
    }

    return array_merge($loaded_product, $data, array(
    'composite_data' => $composite_data,
    'readonly_price' => $readonly_price,
    ));
}, 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'] = str_replace(
            array(wc_get_price_thousand_separator(), wc_get_price_decimal_separator()),
            array('', '.'),
            $matches[1]
        );
    }
    }

    return $item_data;
}, 10, 4);