John Davidson

php - Automatically generate product sort number

0 comments
Message:


Thanks for looking at my question.


I'm not a dev so please be patience


I have source this code on github but it's not longer maintened...
Testing this ocmod on my testing installation in Opencart 3.0.3.6 and 3.0.3.8


This xml file will automatically add a product sort order number in the DB if the product have 0 and it will do it from 5000 in reverse 4999...1....(-50000 - infinity )


I need to do it the opposite and the correct way (for me) from 1 to N


Thanks for your collaboration,


<file path="admin/controller/catalog/product.php">
<operation>
<search><![CDATA[ $data['sort_order'] = $this->request->post['sort_order']; ]]></search>
<add position="after"><![CDATA[ } elseif ((!empty($product_info)) && ($product_info['sort_order'] == 0)) {
$data['sort_order'] = ( 5000 - $this->request->get['product_id'] );]]></add>
</operation>
<!-- Automatic Product Sort Number -->
<operation>
<search><![CDATA[ $data['sort_order'] = 1; ]]></search>
<add position=" replace"><![CDATA[ $data['sort_order'] = 0;]]></add>
</operation>
</file>

File admin/controller/catalog/product.php


File admin/model/catalog/product.php



My Answer:
To achieve the desired result of automatically generating product sort numbers from 1 to N, you can modify the code as follows:

xml


request->post['sort_order']; ]]>
$data['sort_order'] = $this->model_catalog_product->getMaxSortOrder() + 1;]]>




In this modified code snippet, we are calling a method `getMaxSortOrder()` from the model to get the maximum sort order currently in the database and then adding 1 to it to generate the next sort order number. This will ensure that the product sort numbers are generated sequentially from 1 to N.

You will also need to add the `getMaxSortOrder()` method in the `admin/model/catalog/product.php` file to retrieve the maximum sort order value from the database.

Please make sure to test this code thoroughly on your testing installation before implementing it on a live site. Let me know if you need further assistance.

Rate this post

3 of 5 based on 1128 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us