# Market level product variant Metafields

This document outlines the structure and usage of market-specific metafields for product variants. These metafields enable market-specific order limit tracking, and shipping information display.

### 1. Market Preorder Count Metafield

#### Configuration

* **Key**: `market_preorder_count`
* **Type**: `json`
* **Namespace**: `restockrocket_production`
* **Owner**: Product Variant

#### Purpose

* Tracks the current number of preorders for a variant per market
* Used for market-specific inventory management
* Automatically updated when orders are processed in different markets
* Stored as a JSON object mapping market IDs to counts

#### Accessing in Liquid

```liquid

{% assign market_preorder_count = product_variant.metafields.restockrocket_production.market_preorder_count.value | json %}
```

```
Example value of market_preroder_count =>
{ 
  "market_id_1": 1,
  "market_id_2": 5,
  "market_id_3": 3
}
```

### 2. Market Preorder Max Count Metafield

#### Configuration

* **Key**: `market_preorder_max_count`
* **Type**: `json`
* **Namespace**: `restockrocket_production`
* **Owner**: Product Variant

#### Purpose

* Sets the maximum number of preorders allowed per market
* Controls market-specific inventory policy switching
* Triggers blocking orders when count reaches max in specific markets
* Stored as a JSON object mapping market IDs to maximum counts

#### Accessing in Liquid

```liquid

{% assign market_preorder_max_count = product_variant.metafields.restockrocket_production.market_preorder_max_count.value | json %}

```

```
Example value of market_preorder_max_count =>
{ 
  "market_id_1": 10,
  "market_id_2": 15,
  "market_id_3": 10
}
```

#### Example Usage: Market-Specific Preorder Count and Max Count

```liquid
{% assign variant = product.selected_or_first_available_variant %}
{% assign market_counts = variant.metafields.restockrocket_production.market_preorder_count.value | json %}
{% assign market_max_counts = variant.metafields.restockrocket_production.market_preorder_max_count.value | json %}
{% assign current_market = shop.market.id %}

{% if market_max_counts[current_market] %}
  {% assign current_count = market_counts[current_market] %}
  {% assign max_count = market_max_counts[current_market] %}
  {% assign spots_remaining = max_count | minus: current_count %}

  {% if spots_remaining > 0 %}
    <div class="preorder-availability">
      <span class="spots-left">{{ spots_remaining }} spots remaining in your market</span>
      {% if spots_remaining < 5 %}
        <span class="low-stock-warning">Almost sold out in your region!</span>
      {% endif %}
    </div>
  {% else %}
    <div class="preorder-full">
      Preorder limit reached for your market
    </div>
  {% endif %}
{% endif %}
```

### 3. Market Shipping Text Metafield

#### Configuration

* **Key**: `market_shipping_text`
* **Type**: `json`
* **Namespace**: `restockrocket_production`
* **Owner**: Product Variant

#### Purpose

* Stores market-specific shipping/delivery timeline text
* Allows different shipping information per market
* Stored as a JSON object mapping market IDs to shipping text

#### Accessing in Liquid

```liquid
{% assign market_shipping_text = product_variant.metafields.restockrocket_production.market_shipping_text.value | json %}
```

```
Example value of market_shipping_text =>
{
  "market_id_1": "Ships in 1 week",
  "market_id_2": "Ships in 2 weeks",
  "market_id_3": "Ships in 4 days"
}
```

### Accessing via GraphQL

#### Query for Market-Specific Metafields

```graphql
{
  productVariant(id: "gid://shopify/ProductVariant/YOUR_VARIANT_ID") {
    metafields(
      namespace: "restockrocket_production",
      keys: ["market_preorder_count", "market_preorder_max_count", "market_shipping_text"]
    ) {
      edges {
        node {
          id
          key
          value
          type
        }
      }
    }
  }
}
```

#### Query for Multiple Variants with Market Data

```graphql
{
  product(id: "gid://shopify/Product/YOUR_PRODUCT_ID") {
    variants(first: 10) {
      edges {
        node {
          id
          title
          metafields(
            namespace: "restockrocket_production",
            keys: ["market_preorder_count", "market_preorder_max_count", "market_shipping_text"]
          ) {
            edges {
              node {
                id
                key
                value
                type
                # Example response for market_preorder_count:
                # value: {"market_id_1": 45, "market_id_2": 20, "market_id_3": 15}
                # Example response for market_preorder_max_count:
                # value: {"market_id_1": 100, "market_id_2": 50, "market_id_3": 30}
                # Example response for market_shipping_text:
                # value: {"market_id_1": "Ships in 2 weeks", "market_id_2": "Ships in 3 weeks", "market_id_3": "Ships in 4 weeks"}
              }
            }
          }
        }
      }
    }
  }
}
```
