Aller au contenu principal

Integration

Integrate sCommerce capabilities into your code using the examples below.

Tree of subcategories​

In order to get the desired category together with subcategories, use the method for recursive construction of the category tree sCommerce::getTreeActiveCategories(evo()->documentIdentifier, 10).

This method will return the searched category along with the recursively constructed subcategories. Nesting depth depends on the $dept parameter (default 10). When the sLang package is installed, the current locale is applied automatically, and you can request TV values using the third argument.

Seiger\sCommerce\Models\sCategory {#1902 ▼
#connection: "default"
#table: "site_content"
...
#attributes: array:37 [▼
"id" => "50"
"type" => "document"
...
"subcategories" => EvolutionCMS\Extensions\Collection {#1889 ▼
#items: array:4 [▼
0 => Seiger\sCommerce\Models\sCategory {#1888 ▼
#connection: "default"
#table: "site_content"
...
#attributes: array:37 [▶]
#original: array:36 [▶]
...
}
1 => Seiger\sCommerce\Models\sCategory {#1886 ▶}
2 => Seiger\sCommerce\Models\sCategory {#1884 ▶}
3 => Seiger\sCommerce\Models\sCategory {#1882 ▶}
]
#escapeWhenCastingToString: false
}
]
#original: array:36 [▶]
...
}

Call the getTreeActiveCategories() method​

namespace EvolutionCMS\Main\Controllers;

use Seiger\sCommerce\Facades\sCommerce;

class CategoryController extends BaseController
{
public function render()
{
parent::render();
...
$this->data['category'] = sCommerce::getTreeActiveCategories(
evo()->documentIdentifier,
10,
['menu_main', 'menu_footer'] // optional TV list
);
...
}
}

Arguments the getTreeActiveCategories() method​

From the example above:

evo()->documentIdentifier - The ID of the category for which you want to retrieve data. Number type integer.

10 - Nesting depth to get subcategories. Number type integer. Default 10.

['menu_main', 'menu_footer'] (optional) – Array with TV names that should be appended to each category when sLang is installed. Defaults to an empty array.

Applying the result of the getTreeActiveCategories() call​

See below for an example of using the result of the getTreeActiveCategories() method call in the Blade template.

...
<section>
<h1>{% raw %}{{$category->pagetitle}}{% endraw %}</h1>
{% raw %}{!!$category->content!!}{% endraw %}
</section>
@if($category->subcategories->count())
@foreach($category->subcategories as $subcategory)
<section>
<h2>{% raw %}{{$subcategory->pagetitle}}{% endraw %}</h2>
<p>{% raw %}{{$subcategory->introtext}}{% endraw %}</p>
</section>
@endforeach
@endif

Products by category​

To get all the products related to the current category, it is enough to call the related product models like this:

...
use Seiger\sCommerce\Models\sCategory;
...
$category = sCategory::find(evo()->documentIdentifier);
$products = $category->products()->count(); // Products count
$products = $category->products; // Products list

Category and subcategory products​

To display all products of the root category and its associated subcategories, you need to use the getCategoryProducts() method.

Call the getCategoryProducts() method​

namespace EvolutionCMS\Main\Controllers;

use Seiger\sCommerce\Facades\sCommerce;

class CategoryController extends BaseController
{
public function render()
{
parent::render();
...
$products = sCommerce::getCategoryProducts(evo()->documentIdentifier, 'base', 10);
...
}
}

Arguments the getCategoryProducts() method​

From the example above:

This method can be called with no arguments. In this case, a list of products will be returned, where the root category will be the current category. If a multi-language is used, then the data of the text fields will be presented in the current language. The nesting depth of subcategories will be 10.

evo()->documentIdentifier - The ID of the root category for which data is to be retrieved. Number type integer or null. Default null.

'base' - Language for displaying product fields. String language code or null. Default null.

10 - Nesting depth to get subcategories. Number type integer. Default 10.

Result of the getCategoryProducts() call​

Illuminate\Database\Eloquent\Collection {#1422 ▼
#items: array:3 [▼
0 => Seiger\sCommerce\Models\sProduct {#1359 ▼
#connection: "default"
#table: "s_products"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:35 [▶]
#original: array:35 [▶]
#changes: []
#casts: []
#classCastCache: []
#attributeCastCache: []
#dateFormat: null
#appends: array:2 [▶]
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
+usesUniqueIds: false
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [▶]
}
1 => Seiger\sCommerce\Models\sProduct {#1421 ▶}
2 => Seiger\sCommerce\Models\sProduct {#1370 ▶}
]
#escapeWhenCastingToString: false
}

Product attribute​

You can get the value of a product attribute by passing its key.

Result of the attribute() call​

Seiger\sCommerce\Models\sAttribute {#1663 ▼
#connection: "default"
#table: "s_attributes"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:11 [▼
"id" => "1"
"published" => "1"
"asfilter" => "1"
"position" => "0"
"type" => "3"
"alias" => "brand"
"helptext" => "Brand"
"created_at" => "2024-11-25 20:55:13"
"updated_at" => "2024-11-25 20:58:37"
"value" => "leon"
"label" => "Leon"
]
#original: array:13 [▶]
#changes: []
#casts: []
#classCastCache: []
#attributeCastCache: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [▶]
#touches: []
+timestamps: true
+usesUniqueIds: false
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [▶]
}

Example of the attribute() call​

<?php namespace EvolutionCMS\Main\Controllers;

...

class SCommerceProductController extends BaseController
{
public function render()
{
parent::render();

$product = evo()->documentObject['product'];
$brand = $product->attribute('brand');
dd($brand);

...
}

...

}

Present attribute in the blade​

<div class="item-brand">
@lang('Brand'): <a href="@makeUrl(sCommerce::config('basic.catalog_root'))?brand={{$product->attribute('brand')?->value ?? ''}}">
{{$product->attribute('brand')?->label ?? ''}}
</a>
</div>

Order line snapshot​

Since 1.5.0, every new order line (storefront cart, quick order, manager editor and repeat orders) stores its identity in the order products JSON next to the existing keys (id, title, price, quantity and others):

KeyTypeMeaning
line_keystring"{productId}:{optionId}", for example "5:3"
product_idintproduct id (same as id)
option_idintcart option id; 0 = none
skustringeffective SKU; equals product_sku unless a sCommerce.ResolveLineIdentity listener sets a variant SKU
product_skustringthe product's own SKU
gtinstring or nullproduct GTIN
unit_price_net, tax_ratefloat or nulltax placeholders, filled only by sCommerce.ResolveOrderTaxes listeners
requires_shippingboolthe line is shipped
tracks_inventoryboolthe line counts against inventory
retail_excise_rate, retail_excise_amountfloatonly on lines with a retail excise: its rate and the amount included in the line total, for the receipt
deposit_unit, exchange_quantity, deposit_quantity, deposit_amount, deposit_returnable, deposit_terms, deposit_returned_quantitymixedonly on lines with a returnable container deposit, see Returnable container deposits

Tax listeners may also add tax_code, unit_price_gross, tax_amount and prices_include_tax. See Events.

Two cart lines of the same product with different options are now distinguishable by option_id and line_key. The meaning of an option id is defined by the project, for example by its price listeners; it does not identify a row of s_product_modifications.

Orders created before 1.5.0 do not have these keys. Consumers must fall back to:

  • id for product_id;
  • 0 for option_id;
  • sku for both sku and product_sku;
  • true for requires_shipping and tracks_inventory.

line_key is unique within an order created from the cart. A manager can add the same product twice, so consumers should also use the line index. Lines added in the manager receive their identity on the server when the order is saved; lines that already have a line_key keep it.

sCommerce::supportsIntegration('line-identity') reports support.