add a random suffix to url_key(slug) and name of copied product
This commit is contained in:
parent
d6941b7015
commit
43f158d4c1
|
|
@ -603,6 +603,8 @@ class ProductRepository extends Repository
|
|||
|
||||
$attributesToSkip = config('products.skipAttributesOnCopy') ?? [];
|
||||
|
||||
$randomSuffix = substr(md5(microtime()), 0, 6);
|
||||
|
||||
foreach ($originalProduct->attribute_values as $oldValue) {
|
||||
if (in_array($oldValue->attribute->code, $attributesToSkip)) {
|
||||
continue;
|
||||
|
|
@ -611,13 +613,23 @@ class ProductRepository extends Repository
|
|||
$newValue = $oldValue->replicate();
|
||||
|
||||
if ($oldValue->attribute_id === $attributeIds['name']) {
|
||||
$newValue->text_value = __('admin::app.copy-of') . ' ' . $originalProduct->name;
|
||||
$newProductFlat->name = __('admin::app.copy-of') . ' ' . $originalProduct->name;
|
||||
$copiedName = sprintf('%s %s (%s)',
|
||||
trans('admin::app.copy-of'),
|
||||
$originalProduct->name,
|
||||
$randomSuffix
|
||||
);
|
||||
$newValue->text_value = $copiedName;
|
||||
$newProductFlat->name = $copiedName;
|
||||
}
|
||||
|
||||
if ($oldValue->attribute_id === $attributeIds['url_key']) {
|
||||
$newValue->text_value = __('admin::app.copy-of-slug') . '-' . $originalProduct->url_key;
|
||||
$newProductFlat->url_key = __('admin::app.copy-of-slug') . '-' . $originalProduct->url_key;
|
||||
$copiedSlug = sprintf('%s-%s-%s',
|
||||
trans('admin::app.copy-of'),
|
||||
$originalProduct->url_key,
|
||||
$randomSuffix
|
||||
);
|
||||
$newValue->text_value = __('admin::app.copy-of-slug') . '-' . $originalProduct->url_key . '-' . $randomSuffix;
|
||||
$newProductFlat->url_key = __('admin::app.copy-of-slug') . '-' . $originalProduct->url_key . '-' . $randomSuffix;
|
||||
}
|
||||
|
||||
if ($oldValue->attribute_id === $attributeIds['sku']) {
|
||||
|
|
|
|||
|
|
@ -37,18 +37,18 @@ class ProductCopyCest
|
|||
'attribute_family_id' => $original->attribute_family_id,
|
||||
]);
|
||||
|
||||
$I->seeRecord(ProductAttributeValue::class, [
|
||||
$attr = $I->grabRecord(ProductAttributeValue::class, [
|
||||
'attribute_id' => 2,
|
||||
'product_id' => $copiedProduct->id,
|
||||
'text_value' => 'Copy of ' . $originalName,
|
||||
]);
|
||||
$I->assertStringStartsWith('Copy of ' . $originalName, $attr->text_value);
|
||||
|
||||
// url_key
|
||||
$I->seeRecord(ProductAttributeValue::class, [
|
||||
$attr = $I->grabRecord(ProductAttributeValue::class, [
|
||||
'attribute_id' => 3,
|
||||
'product_id' => $copiedProduct->id,
|
||||
'text_value' => 'copy-of-' . $original->url_key,
|
||||
]);
|
||||
$I->assertStringStartsWith('copy-of-' . $original->url_key, $attr->text_value);
|
||||
|
||||
// sku
|
||||
$I->seeRecord(ProductAttributeValue::class, [
|
||||
|
|
@ -74,10 +74,10 @@ class ProductCopyCest
|
|||
'qty' => 10,
|
||||
]);
|
||||
|
||||
$I->seeRecord(ProductFlat::class, [
|
||||
$flat = $I->grabRecord(ProductFlat::class, [
|
||||
'product_id' => $copiedProduct->id,
|
||||
'name' => 'Copy of ' . $originalName,
|
||||
]);
|
||||
$I->assertStringStartsWith('Copy of ' . $originalName, $flat->name);
|
||||
|
||||
$I->assertCount($count + 1, Product::all());
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue