Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public static function editions(): array
/**
* @inheritDoc
*/
public string $schemaVersion = '5.4.0.5';
public string $schemaVersion = '5.5.0';

/**
* @inheritdoc
Expand Down
3 changes: 3 additions & 0 deletions src/controllers/ProductTypesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ public function actionSaveProductType(): void
$productType->productTitleFormat = $this->request->getBodyParam('productTitleFormat');
$productType->productTitleTranslationMethod = $this->request->getBodyParam('productTitleTranslationMethod', $productType->productTitleTranslationMethod);
$productType->productTitleTranslationKeyFormat = $this->request->getBodyParam('productTitleTranslationKeyFormat', $productType->productTitleTranslationKeyFormat);
$productType->showSlugField = (bool)$this->request->getBodyParam('showSlugField', $productType->showSlugField);
$productType->slugTranslationMethod = $this->request->getBodyParam('slugTranslationMethod', $productType->slugTranslationMethod);
$productType->slugTranslationKeyFormat = $this->request->getBodyParam('slugTranslationKeyFormat', $productType->slugTranslationKeyFormat);
$productType->maxVariants = $this->request->getBodyParam('maxVariants') ?: null;
$productType->hasVariantTitleField = $this->request->getBodyParam('hasVariantTitleField', false);
$productType->variantTitleFormat = $this->request->getBodyParam('variantTitleFormat');
Expand Down
29 changes: 28 additions & 1 deletion src/elements/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,31 @@ public function getTitleTranslationKey(): string
return ElementHelper::translationKey($this, $type->productTitleTranslationMethod, $type->productTitleTranslationKeyFormat);
}

/**
* @inheritdoc
*/
public function getIsSlugTranslatable(): bool
{
return ($this->getType()->slugTranslationMethod !== Field::TRANSLATION_METHOD_NONE);
}

/**
* @inheritdoc
*/
public function getSlugTranslationDescription(): ?string
{
return ElementHelper::translationDescription($this->getType()->slugTranslationMethod);
}

/**
* @inheritdoc
*/
public function getSlugTranslationKey(): string
{
$type = $this->getType();
return ElementHelper::translationKey($this, $type->slugTranslationMethod, $type->slugTranslationKeyFormat);
}

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -1373,7 +1398,9 @@ protected function metaFieldsHtml(bool $static): string
$view = Craft::$app->getView();
$productType = $this->getType();
// Slug
$fields[] = $this->slugFieldHtml($static);
if ($productType->showSlugField) {
$fields[] = $this->slugFieldHtml($static);
}

if ($productType->isStructure && $productType->maxLevels !== 1) {
$fields[] = (function() use ($static, $productType) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace craft\commerce\migrations;

use craft\commerce\db\Table;
use craft\db\Migration;

/**
* m250731_020627_add_slug_options_to_product_types migration.
*/
class m250731_020627_add_slug_options_to_product_types extends Migration
{
/**
* @inheritdoc
*/
public function safeUp(): bool
{
// Add showSlugField column
if (!$this->db->columnExists(Table::PRODUCTTYPES, 'showSlugField')) {
$this->addColumn(Table::PRODUCTTYPES, 'showSlugField', $this->boolean()->notNull()->defaultValue(true)->after('productTitleTranslationKeyFormat'));
}

// Add slugTranslationMethod column
if (!$this->db->columnExists(Table::PRODUCTTYPES, 'slugTranslationMethod')) {
$this->addColumn(Table::PRODUCTTYPES, 'slugTranslationMethod', $this->string()->notNull()->defaultValue('site')->after('showSlugField'));
}

// Add slugTranslationKeyFormat column
if (!$this->db->columnExists(Table::PRODUCTTYPES, 'slugTranslationKeyFormat')) {
$this->addColumn(Table::PRODUCTTYPES, 'slugTranslationKeyFormat', $this->string()->after('slugTranslationMethod'));
}

return true;
}

/**
* @inheritdoc
*/
public function safeDown(): bool
{
return true;
}
}
44 changes: 44 additions & 0 deletions src/models/ProductType.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,25 @@ class ProductType extends Model implements FieldLayoutProviderInterface
*/
public ?string $productTitleTranslationKeyFormat = null;

/**
* @var bool Whether to show the Slug field
* @since 5.3.0
*/
public bool $showSlugField = true;

/**
* @var string Slug translation method
* @phpstan-var Field::TRANSLATION_METHOD_NONE|Field::TRANSLATION_METHOD_SITE|Field::TRANSLATION_METHOD_SITE_GROUP|Field::TRANSLATION_METHOD_LANGUAGE|Field::TRANSLATION_METHOD_CUSTOM
* @since 5.3.0
*/
public string $slugTranslationMethod = Field::TRANSLATION_METHOD_SITE;

/**
* @var string|null Slug translation key format
* @since 5.3.0
*/
public ?string $slugTranslationKeyFormat = null;

/**
* @var string|null SKU format
*/
Expand Down Expand Up @@ -216,6 +235,26 @@ class ProductType extends Model implements FieldLayoutProviderInterface
*/
public PropagationMethod $propagationMethod = PropagationMethod::All;

/**
* @inheritdoc
*/
public function init(): void
{
parent::init();

if ($this->productTitleTranslationKeyFormat === '') {
$this->productTitleTranslationKeyFormat = null;
}

if ($this->variantTitleTranslationKeyFormat === '') {
$this->variantTitleTranslationKeyFormat = null;
}

if ($this->slugTranslationKeyFormat === '') {
$this->slugTranslationKeyFormat = null;
}
}

/**
* @return null|string
*/
Expand Down Expand Up @@ -589,6 +628,11 @@ public function getConfig(): array
'productTitleTranslationMethod' => $this->productTitleTranslationMethod,
'productTitleTranslationKeyFormat' => $this->productTitleTranslationKeyFormat,

// Slug field
'showSlugField' => $this->showSlugField,
'slugTranslationMethod' => $this->slugTranslationMethod,
'slugTranslationKeyFormat' => $this->slugTranslationKeyFormat,

'propagationMethod' => $this->propagationMethod->value,

'skuFormat' => $this->skuFormat,
Expand Down
3 changes: 3 additions & 0 deletions src/records/ProductType.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
* @property string $productTitleFormat
* @property string $productTitleTranslationMethod
* @property string $productTitleTranslationKeyFormat
* @property bool $showSlugField
* @property string $slugTranslationMethod
* @property string $slugTranslationKeyFormat
* @property string $propagationMethod
* @property ActiveQueryInterface $variantFieldLayout
* @property int|null $variantFieldLayoutId
Expand Down
20 changes: 20 additions & 0 deletions src/services/ProductTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,11 @@ public function handleChangedProductType(ConfigEvent $event): void
$productTypeRecord->productTitleFormat = $productTitleFormat;
$productTypeRecord->hasProductTitleField = $hasProductTitleField;

// Slug fields
$productTypeRecord->showSlugField = $data['showSlugField'] ?? true;
$productTypeRecord->slugTranslationMethod = $data['slugTranslationMethod'] ?? 'site';
$productTypeRecord->slugTranslationKeyFormat = $data['slugTranslationKeyFormat'] ?? '';

if ($productTypeRecord->maxVariants != $data['maxVariants']) {
$shouldResaveProducts = true;
}
Expand Down Expand Up @@ -961,6 +966,21 @@ private function _createProductTypeQuery(): Query
$query->addSelect('productTypes.propagationMethod');
}

/** @since 5.3 */
if ($db->columnExists(Table::PRODUCTTYPES, 'showSlugField')) {
$query->addSelect('productTypes.showSlugField');
}

/** @since 5.3 */
if ($db->columnExists(Table::PRODUCTTYPES, 'slugTranslationMethod')) {
$query->addSelect('productTypes.slugTranslationMethod');
}

/** @since 5.3 */
if ($db->columnExists(Table::PRODUCTTYPES, 'slugTranslationKeyFormat')) {
$query->addSelect('productTypes.slugTranslationKeyFormat');
}

return $query;
}

Expand Down
62 changes: 61 additions & 1 deletion src/templates/settings/producttypes/_edit.twig
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,66 @@
</div>
{% endmacro %}

{% from _self import variantTitleFormatField, productTitleFormatField, uriFormatText %}
{% macro productSlugField(productType, disabled) %}
{% import '_includes/forms' as forms %}

{{ forms.lightswitchField({
label: "Show the Slug field"|t('app'),
name: 'showSlugField',
toggle: 'slug-container',
reverseToggle: '#slugFormat-container, #field-layout .fld-slug-field-icon',
on: productType.showSlugField,
disabled: disabled,
}) }}

{% if craft.app.getIsMultiSite() %}
<div id="slug-container"{% if not productType.showSlugField %} class="hidden"{% endif %}>
{{ forms.selectField({
label: '{name} Translation Method'|t('app', {
name: 'Slug'|t('app'),
}),
instructions: 'How should {name} values be translated?'|t('app', {
name: 'Slug'|t('app'),
}),
id: 'slug-translation-method',
name: 'slugTranslationMethod',
options: [
{ value: 'none', label: 'Not translatable'|t('app') },
{ value: 'site', label: 'Translate for each site'|t('app') },
{ value: 'siteGroup', label: 'Translate for each site group'|t('app') },
{ value: 'language', label: 'Translate for each language'|t('app') },
{ value: 'custom', label: 'Custom…'|t('app') },
]|filter,
value: productType.slugTranslationMethod,
toggle: true,
targetPrefix: 'slug-translation-method-',
disabled: disabled,
}) }}

<div id="slug-translation-method-custom" {% if productType.slugTranslationMethod != 'custom' %}class="hidden"{% endif %}>
{{ forms.textField({
label: '{name} Translation Key Format'|t('app', {
name: 'Slug'|t('app'),
}),
instructions: 'Template that defines the {name} field’s custom "translation key" format. Values will be copied to all sites that produce the same key.'|t('app', {
name: 'Slug'|t('app'),
}),
id: 'slug-translation-key-format',
name: 'slugTranslationKeyFormat',
value: productType.slugTranslationKeyFormat,
errors: productType.getErrors('slugTranslationKeyFormat'),
data: {
'error-key': 'slugTranslationKeyFormat',
},
disabled: disabled,
}) }}
</div>
</div>
{% endif %}

{% endmacro %}

{% from _self import variantTitleFormatField, productTitleFormatField, uriFormatText, productSlugField %}


{{ forms.textField({
Expand Down Expand Up @@ -264,6 +323,7 @@
}) }}

{{ productTitleFormatField(productType, readOnly) }}
{{ productSlugField(productType, readOnly) }}

{{ forms.textField({
label: "Automatic SKU Format"|t('commerce'),
Expand Down
Loading