Search products function when adding new fields into Product.

Help for integrating the Laravel package
Forum rules
Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
Sabir_Ali
Posts: 34
Joined: 12 Feb 2024, 06:43

Search products function when adding new fields into Product.

Post by Sabir_Ali » 17 Feb 2024, 14:23

PHP: 8.1.2-1ubuntu2.14
Laravel Framework: 10.43.0
System: Ubuntu Gnu Linux 22.04

This is my first experience in working with Aimeos. My task is to add two new fields into the Product model (Item) and modifying Admin Panel to add these new fields as optional (one bool and one string) data (the goal is to add our own API library in the text step, which will use these fields to switch on/off using that API).

What is done:
1. Template (admin panel product base data) modified and added into packages/myproject/templates/admin/jqadm/product/item.php
2. Laravel migration is used but also setup is added into packages/myproject/setup/default/schema/product.php
3. Product Item customization file added into packages/myproject/src/MShop/Product/Item/MyProjectStandard.php with new attributes getters, setters, froArray() and toArray() methods. It works fine.
4. Product custom manager added into packages/myproject/src/MShop/Product/Manager/MyProject.php
. where saving method is implemented and works fine.
5. Product decorator added into packages/myproject/src/MShop/Product/Manager/Decorator/MyProject.php.

What is the problem.
I have an issue in opening list of products in Admin Panel. I can see the basic data of the products list (so I can click on the name and enter). But no images and to additional information in the list. And when I enter into the product customization pages they are almost empty. But on the Base tab of the product customization I see the new fields data that I saved.

I guess, I have to implement the Search method in a my own Decorator (but in fact there is no concrete information about it in the documentation, so I'm just guessing). I think the list must be opened by Standard Aimeos product manager to avoid code duplication. But I cannot understand how to do it correct. The configuration file mshop.php is often looks like this:

Code: Select all

return [
    'product' => [
        'manager' => [
            'name' => 'MyProject',
            'decorators' => [
                'local' => ['MyProject']
            ],
            'item' => [
                'name' => ['MyProjectStandard']
            ]
        ]
    ]
];
I tried to create my own Decorator for search function in packages/myproject/src/MShop/Product/Manager/Decorator/MyProjectSearch.php and modified the mshop.php file like 'decorators' => ['local' => ['MyProject', 'MySearch']. When I tried to use Search function in my own manager or decorator I was getting the error like:
"Class "\Aimeos\MShop\Product\Manager\Product\Standard" not found.
As you can see, there are two words Product in the path. Which is it not real. And I can't understand why it gives my this path. But anyway I'm not sure is it the right way.

The question is:
What is the right approach to use my own manager for Saving data but keep the Search method by Standard Manager (to see all the information in the list)? Or it is not possible and I have to implement all the methods to make the search function work? And if so, how to do it correct?

User avatar
aimeos
Administrator
Posts: 8380
Joined: 01 Jan 1970, 00:00

Re: Search products function when adding new fields into Product.

Post by aimeos » 19 Feb 2024, 07:48

Sabir_Ali wrote: 17 Feb 2024, 14:23 2. Laravel migration is used but also setup is added into packages/myproject/setup/default/schema/product.php
You don't need a Laravel migration, extending the schema for the Aimeos setup task is enough.
Sabir_Ali wrote: 17 Feb 2024, 14:23 3. Product Item customization file added into packages/myproject/src/MShop/Product/Item/MyProjectStandard.php with new attributes getters, setters, froArray() and toArray() methods. It works fine.
4. Product custom manager added into packages/myproject/src/MShop/Product/Manager/MyProject.php
. where saving method is implemented and works fine.
5. Product decorator added into packages/myproject/src/MShop/Product/Manager/Decorator/MyProject.php.
You don't need to do both, either create a decorator OR extend the product item and manager!
We recommend using a decorator only because it's much easier.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Sabir_Ali
Posts: 34
Joined: 12 Feb 2024, 06:43

Re: Search products function when adding new fields into Product.

Post by Sabir_Ali » 19 Feb 2024, 08:38

Thank you for your reply!

OK. I try to simplify the code using Decorator for now. I removed the mention of the Item and Manager from the msghop.php file (so as I suppose, custom Item and manager will not work). Now the list works as before (original state). I get all data of the products. And now I can't get and save the new attributes data. What I must to do now to make new attributes work?

There is the following text in the Extend Manager documentation:

Item properties#

Afterwards, you can access the values of your new columns in the item using:

Code: Select all

$item->get( 'mycolumn', '<default value>' );
$item->mycolumn;
And so on...

Should I do it somewhere? If YES - where? I don't have any additional logic classes yet. I just need to have ability to get and save the new attributes data.

User avatar
aimeos
Administrator
Posts: 8380
Joined: 01 Jan 1970, 00:00

Re: Search products function when adding new fields into Product.

Post by aimeos » 19 Feb 2024, 09:13

Please post the lines you've added to your overwritten template to display your new item properties.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Sabir_Ali
Posts: 34
Joined: 12 Feb 2024, 06:43

Re: Search products function when adding new fields into Product.

Post by Sabir_Ali » 19 Feb 2024, 09:21

Sure! There they are (so I'm not sure everything is correct :) )

Code: Select all

<div class="form-group row optional">
    <label class="col-sm-4 form-control-label"><?= $enc->html( $this->translate( 'admin', 'Through Processing API' ) ) ?></label>
    <div class="col-sm-8">
        <input class="form-check-input" type="checkbox" tabindex="1"
               name="<?= $enc->attr( $this->formparam( array( 'item', 'product.is_api_purchase' ) ) ) ?>"
               value="1" <?= $this->get( 'itemData/product.is_api_purchase' ) ? 'checked="checked"' : '' ?>
               v-bind:readonly="!can('change')">
    </div>
</div>
<div class="form-group row optional">
    <label class="col-sm-4 form-control-label help"><?= $enc->html( $this->translate( 'admin', 'Service Code' ) ) ?></label>
    <div class="col-sm-8">
        <input class="form-control item-service-code" type="text" tabindex="1"
               name="<?= $enc->attr( $this->formparam( array( 'item', 'product.service_code' ) ) ) ?>"
               placeholder="<?= $enc->attr( $this->translate( 'admin', 'Enter service code (optional)' ) ) ?>"
               value="<?= $enc->attr( $this->get( 'itemData/product.service_code' ) ) ?>"
               v-bind:readonly="!can('change')">
    </div>
</div>

User avatar
aimeos
Administrator
Posts: 8380
Joined: 01 Jan 1970, 00:00

Re: Search products function when adding new fields into Product.

Post by aimeos » 19 Feb 2024, 09:22

Remove the "product." prefix from your added code because when adding item properties via decorators, they have no prefix.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Sabir_Ali
Posts: 34
Joined: 12 Feb 2024, 06:43

Re: Search products function when adding new fields into Product.

Post by Sabir_Ali » 19 Feb 2024, 09:31

Wow! After removal the prefix, saving of both attributes works only by using Decorator! But in the template view saved data still doesn't appears.

User avatar
aimeos
Administrator
Posts: 8380
Joined: 01 Jan 1970, 00:00

Re: Search products function when adding new fields into Product.

Post by aimeos » 19 Feb 2024, 09:43

Did you change this line too:

Code: Select all

value="<?= $enc->attr( $this->get( 'itemData/product.service_code' ) ) ?>"
To:

Code: Select all

value="<?= $enc->attr( $this->get( 'itemData/service_code' ) ) ?>"
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Sabir_Ali
Posts: 34
Joined: 12 Feb 2024, 06:43

Re: Search products function when adding new fields into Product.

Post by Sabir_Ali » 19 Feb 2024, 10:07

I apologize for my inattention. :oops: Now it works fine. Thanks a lot!

I would like to take this opportunity to ask. When I will add the logic to these attributes, where would you recommend to place it? Will I need to use more advanced approach, for example my own manager? Or using the Laravel best practises will enough?

User avatar
aimeos
Administrator
Posts: 8380
Joined: 01 Jan 1970, 00:00

Re: Search products function when adding new fields into Product.

Post by aimeos » 21 Feb 2024, 10:52

Aimeos managers/items work differently than Laravel models. You can add macros for implementing own logic in items:
https://aimeos.org/docs/latest/models/e ... properties
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply