# E-commerce

## Product

<!-- panels:start -->
<!-- div:title-panel -->

### List

<!-- div:left-panel -->

Get all products with content.

> [!NOTE] > **Parameters**<br>
>
> -   **paginate**
>     -   **Type:** Integer
>     -   **Default:** 10
>     -   **Required:** false

?> **Response Type:** Illuminate\Support\Collection<br>
**Response Content:** App\Models\Products

<!-- div:right-panel -->

<!-- tabs:start -->

#### **Code**

```php
get_products($paginate = 10)
```

#### **Inspect**

```php
function get_products($paginate = 10)
{
    $query = Products::with('first_image', 'all_images', 'content')->whereHas('content')->paginate($paginate);
    return $query;
}
```

<!-- tabs:end -->

<!-- panels:end -->

<!-- panels:start -->
<!-- div:title-panel -->

### Showroom

<!-- div:left-panel -->

Get showroom products with first image, all images and content.

?> **Response Type:** Illuminate\Support\Collection<br>
**Response Content:** App\Models\Products

<!-- div:right-panel -->

<!-- tabs:start -->

#### **Code**

```php
get_products_showroom()
```

#### **Inspect**

```php
function get_products_showroom()
{
    $query = Products::where('showroom', 1)->with('first_image', 'all_images', 'content')->whereHas('content')->get();
    return $query;
}
```

<!-- tabs:end -->

<!-- panels:end -->

<!-- panels:start -->
<!-- div:title-panel -->

### Similar Products

<!-- div:left-panel -->

Get similar products list.

> [!NOTE] > **Parameters**<br>
>
> -   **product**
>     -   **Type:** Integer or App\Models\Products
>     -   **Required:** true

?> **Response Type:** Illuminate\Support\Collection<br>
**Response Content:** App\Models\Products

!> If the product parameter is integer, it searches the Products table with its id value.

<!-- div:right-panel -->

<!-- tabs:start -->

#### **Code**

```php
get_similar_products(int|Products $product, $similar_product_limit = 8)
```

#### **Example**

```php
@foreach (get_similar_products($product) as $similar_product)
    <div class="slick-single-layout">
        <div class="axil-product product-style-eight">
            <div class="thumbnail">
                <a
                    href="{{ route('product.detail.old', ['permalink' => $similar_product->content->permalink]) }}">
                    <img data-sal="zoom-out" data-sal-delay="100" data-sal-duration="800"
                        loading="lazy" class="main-img"
                        src="{{ Common::get_product_medium_image($similar_product->id, $similar_product->first_image->name ?? '') }}"
                        alt="Product Images">
                </a>
            </div>
            <div class="product-content">
                <div class="inner">
                    <h5 class="title"><a
                            href="{{ route('product.detail.old', ['permalink' => $similar_product->content->permalink]) }}">
                            {{ $similar_product->content->name ?? '' }}</a></h5>
                    <div class="product-rating">
                        <span class="icon">
                            @php
                                $reviewaverage = $similar_product->rate;
                            @endphp
                            @foreach (range(1, 5) as $i)
                                @if ($reviewaverage > 0)
                                    @if ($reviewaverage > 0.5)
                                        <i class="fas fa-star"></i>
                                    @elseif ($reviewaverage <= 0.5 && $reviewaverage > 0)
                                        <i class="fas fa-star-half-alt"></i>
                                    @endif
                                @else
                                    <i class="far fa-star"></i>
                                @endif
                                @php
                                    $reviewaverage--;
                                @endphp
                            @endforeach
                        </span>
                        <span class="rating-number">{{ $similar_product->review_count }}</span>
                    </div>
                    <div class="product-price-variant">
                        <span
                            class="price current-price">{{ $similar_product->get_price(false) ?? '' }}</span>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endforeach
```

#### **Inspect**

```php
function get_similar_products(int|Products $product, $similar_product_limit = 8) {
    if(is_int($product)) {
        $product = Products::find($product);
    }

    $similar_products = Products::whereHas('content', function ($query) use ($product) {
        // ürünün adındaki 3 harften fazla olan kelimeleri al ve benzer ürünleri getir
        $product_name = explode(' ', $product->content->name);
        $product_name = array_filter($product_name, function ($value) {
            return strlen($value) > 3;
        });
        foreach ($product_name as $key => $value) {
            $query->where('name', 'LIKE', '%' . $value . '%');
        }
    })
        ->where('id', '!=', $product->id)
        ->with('content')
        ->with('first_image')
        ->limit($similar_product_limit)
        ->get();

    if (!$similar_products) {
        $similar_products_with_categories = Products::whereHas('category', function ($query) use ($product) {
            return $query->where('category_id', $product->category_id);
        })
            ->where('id', '!=', $product->id)
            ->with('content')
            ->with('first_image')
            ->limit($similar_product_limit)
            ->get();
        if ($similar_products_with_categories) {
            $similar_products = $similar_products_with_categories->merge($similar_products);
        }
    }

    if (!$similar_products || $similar_products->count() < $similar_product_limit) {
        $similar_products_others = Products::where('id', '!=', $product->id)
            ->with('content')
            ->with('first_image')
            ->limit($similar_product_limit)
            ->get();
        if ($similar_products_others) {
            $similar_products = $similar_products_others->merge($similar_products);
        }
    }
    return $similar_products;
}
```

<!-- tabs:end -->

<!-- panels:end -->

<!-- panels:start -->
<!-- div:title-panel -->

### Product Variants (product detail)

<!-- div:left-panel -->

Get product variants.<br>
Usage:

1. Add `data-variant-image` attribute to the all images of the product detail.
2. Add `data-variant-image-id` attribute to the all images of the product detail.
3. Add `data-variant-title` attribute to the title of the product detail.
4. Add `data-variant-price` attribute to the price of the product detail.
5. Add `data-cart-button` attribute to the cart button of the product detail.

?> The code on the side contains the code required to select the variants of the products.

!> data-type, data-product-id, data-variant-id, data-variant-selected attributes are required for the button tag.

<!-- div:right-panel -->

<!-- tabs:start -->

#### **Code**

```php
$product->variants
```

#### **Example**

```php
<!-- variants -->
<div class="mb-5 d-flex flex-wrap justify-content-start flex-row gap-2">
    @foreach ($product->variants as $variant)
        <button data-type="variant" style="min-height: 50px" type="button"
            data-product-id="{{ $product->id }}"
            data-variant-id="{{ $variant->id }}"
            class="axil-btn p-2 px-5 btn btn-sm mb-2 me-2 w-auto
            btn-outline-primary"
            @if (request()->variant == $variant->id) data-variant-selected @endif>
            <img src="{{ Common::get_product_medium_image($product->id, $variant->first_image->image->name ?? '') }}"
                alt="" class="mb-2 rounded" style="height: 70px">

            @foreach ($variant->options as $variant_option)
                <div>

                    {{ $variant_option->option_value->content->name ?? '' }}
                    {{ $variant_option->option->content->name ?? '' }}
                </div>
            @endforeach
        </button>
    @endforeach
</div>
<!-- #variants -->
```

<!-- tabs:end -->

You can use the following code to listen to the variant change event.

<!-- tabs:start -->

#### **Code**

```php
<script>
    window.addEventListener('variantChange', function(e) {});
</script>
```

<!-- tabs:end -->



<!-- panels:end -->

<!-- panels:start -->
<!-- div:title-panel -->

### Add Favorite Product

<!-- div:left-panel -->

Add favorite product.

<!-- div:right-panel -->

<!-- tabs:start -->

#### **Code**

```php
is_favorite_product($product)
```

#### **Example**

```php
<button class="btn btn-sm axil-btn rounded-pill p-3 @if (is_favorite_product($product)) btn-danger @else btn-outline-primary @endif position-absolute end-0 px-4" style="width:50px;height:50px" data-product-id="{{ $product->id }}" onclick="add_favorite_product(this)">
    <i class="fa fa-heart me-0" style="color: inherit"></i>
</button>
```

#### **Inspect**

```php
function is_favorite_product(int|Products $product) {
    $product_id = $product;
    if(!is_int($product)) {
        $product_id = $product->id;
    }
    $is_favorite = false;
    if (Auth::check()) {
        $is_favorite =
            Auth::user()
                ->favorite_products->where('product_id', $product_id)
                ->count() > 0;
    }
    return $is_favorite;
}
```

<!-- tabs:end -->

<!-- panels:end -->

<!-- panels:start -->
<!-- div:title-panel -->

### Product Review Count

<!-- div:left-panel -->

Get product review count.

<!-- div:right-panel -->

<!-- tabs:start -->

#### **Code**

```php
{{ $product->review_count }}
```

#### **Example**

```php
 <div>{{ $product->review_count }} {{ __('Yorum') }}</div>
```

<!-- tabs:end -->

<!-- panels:end -->

<!-- panels:start -->
<!-- div:title-panel -->

### Product Star

<!-- div:left-panel -->

Get product star.

<!-- div:right-panel -->

<!-- tabs:start -->

#### **Code**

```php
{{ $product->rate }}
```

#### **Example**

```php
@php
    $reviewaverage = $product->rate;
@endphp
<!-- can be .5 .3 -->
@foreach (range(1, 5) as $i)
    @if ($reviewaverage > 0)
        @if ($reviewaverage > 0.5)
            <i class="fas fa-star"></i>
        @elseif ($reviewaverage <= 0.5 && $reviewaverage > 0)
            <i class="fas fa-star-half-alt"></i>
        @endif
    @else
        <i class="far fa-star"></i>
    @endif
    @php
        $reviewaverage--;
    @endphp
@endforeach
```

<!-- tabs:end -->

<!-- panels:end -->

<!-- panels:start -->
<!-- div:title-panel -->

### Product Detail Slider

<!-- div:left-panel -->

Get product detail slider.

> [!NOTE] > **Parameters**<br>
>
> -   **place_key**
>     -   **Type:** Integer
>     -   **Required:** true

?> **Response Type:** Illuminate\Support\Collection<br>
**Response Content:** App\Models\SliderPlace

!> Place_key value of 4 is used to bring the slider information of the product detail.

<!-- div:right-panel -->

<!-- tabs:start -->

#### **Code**

```php
Site::get_slider_place(4)
```

#### **Example**

```php
@foreach (Site::get_slider_place(4) as $key => $value)
    <div class="sale-banner-thumb">
        <a href="shop.html">
            <div class="w-100 my-5 rounded"
                style="height: 300px;background-image:url('{{ Common::get_slider_image($value->slider->id, $value->slider->image ?? '') }}'); background-size: cover;background-position: center;background-repeat: no-repeat;"
                alt="Sale Banner Images"></div>
        </a>
    </div>
@endforeach
```

#### **Inspect**

```php
public static function get_slider_place($place_key)
{
    $query = SliderPlace::where('key', $place_key)->with('slider')->whereHas('slider')->orderBy('position', 'DESC')->get();
    return $query;
}
```

<!-- tabs:end -->

<!-- panels:end -->

---

## Currency

<!-- panels:start -->
<!-- div:title-panel -->

### Active Currencies List

<!-- div:left-panel -->

Get all active currencies.

?> **Response Type:** Illuminate\Support\Collection<br>
**Response Content:** App\Models\Currency

<!-- div:right-panel -->

<!-- tabs:start -->

#### **Code**

```php
get_currencies()
```

#### **Inspect**

```php
function get_currencies()
{
    $query = Currency::where('status', 1)->get();
    return $query;
}
```

<!-- tabs:end -->
<!-- panels:end -->

---
