You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Aimeos Laravel:商品添加表单新增“Old SKU”字段求助

Adding "Old SKU" Field to Aimeos Product Form (Laravel)

Hey there! Since you've already tackled the template override and database extension for your "Old SKU" field, let's walk through the remaining key steps to get it fully working with Aimeos' data handling system.

1. Configure ORM & Schema Mapping

Aimeos needs to recognize your new database field to read/write data to it. Add this configuration to your extension's ./ext/myextension/config/mshop.php file:

return [
    'product' => [
        'manager' => [
            'lists' => [
                'columns' => [
                    // Keep existing columns, add this:
                    'old_sku' => [
                        'code' => 'product.old_sku',
                        'internalcode' => 'mpro_old_sku', // Match your DB column name
                        'label' => 'Old SKU',
                        'type' => 'string',
                        'internaltype' => 'varchar',
                        'default' => '',
                        'public' => true,
                    ],
                ],
            ],
        ],
        'schema' => [
            'table' => 'mshop_product',
            'columns' => [
                // Keep existing columns, add this:
                'mpro_old_sku' => [
                    'name' => 'mpro_old_sku',
                    'type' => 'string',
                    'length' => 255,
                    'default' => '',
                    'nullable' => true,
                ],
            ],
        ],
    ],
];

2. Verify Template Field Binding

Double-check that your template's "Old SKU" input field is correctly mapped to the field key you configured. In your ./ext/myextension/admin/jqadm/templates/product/form/base.blade.php, the field should look like this (match the structure of the existing SKU field):

<div class="form-group row">
    <label class="col-sm-2 col-form-label" for="product-old-sku">
        {{ trans('admin', 'Old SKU') }}
    </label>
    <div class="col-sm-10">
        <input class="form-control" type="text" id="product-old-sku" 
            name="product[old_sku]" value="{{ $item->get('old_sku', '') }}" />
    </div>
</div>
  • The name="product[old_sku]" matches the old_sku key in your configuration.
  • $item->get('old_sku', '') pulls the saved value from the product item (with a default empty string if it's not set).

3. Optional: Add Custom Getter/Setter Methods (For Cleaner Code)

If you want to use explicit methods instead of get('old_sku') in your code, create a custom Product Item class in your extension at ./ext/myextension/src/MShop/Product/Item/Standard.php:

namespace Aimeos\MShop\Product\Item;

class Standard extends \Aimeos\MShop\Product\Item\StandardBase
{
    /**
     * Get the old SKU value
     *
     * @return string Old SKU
     */
    public function getOldSku() : string
    {
        return $this->get('old_sku');
    }

    /**
     * Set the old SKU value
     *
     * @param string $oldSku Old SKU
     * @return \Aimeos\MShop\Product\Item\Iface Product item for chaining
     */
    public function setOldSku(string $oldSku) : \Aimeos\MShop\Product\Item\Iface
    {
        return $this->set('old_sku', $oldSku);
    }
}

Then update your config/mshop.php to use this custom item class:

'product' => [
    'manager' => [
        'item' => 'Aimeos\MShop\Product\Item\Standard',
        // ... other existing config
    ],
],

4. Clear Caches & Test

Aimeos caches configuration and templates aggressively, so run these commands to make sure your changes take effect:

php artisan cache:clear
php artisan aimeos:cache

Now head to your Aimeos admin, edit a product, enter an Old SKU value, save it, and verify:

  • The value is stored in your mshop_product table's mpro_old_sku column
  • The value loads back into the form when you edit the product again

5. Using Old SKU in Frontend (Optional)

To display the Old SKU on your frontend product pages, you can fetch it from the product item like this (in your Laravel blade templates):

@if($product->get('old_sku'))
    <p>Old SKU: {{ $product->get('old_sku') }}</p>
@endif

Or if you added the custom getter:

@if($product->getOldSku())
    <p>Old SKU: {{ $product->getOldSku() }}</p>
@endif

内容的提问来源于stack exchange,提问作者Mohal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:16:47