Jump to content

Need to Bulk Remove Custom Values for a Specific Attribute in PrestaShop


juanccub

Recommended Posts

Hello everyone!

I am writing to you because I have a problem with my PrestaShop for which I can't find a solution. I have several hundred products on my site, and many of them have a "custom value" assigned to a specific attribute "bracelet material."

I would like to remove all the custom values of this attribute, but doing it product by product in the backoffice would take me months.

Is there a way to automate this, to remove all the custom values of this attribute? Perhaps with a module or an SQL query?

Thank you in advance for your help!

Link to comment
Share on other sites

Hi.

There is no need for any special features and I am surprised that Prestashop does not already have such a feature built in.

image.png.8045d79f99e2c8271391831ee44d9ffb.png

 

Just edit the file:
./controllers/admin/AdminFeaturesController.php

1. find the renderView() function and edit it like this:

public function renderView()
    {
        if ($id = (int) Tools::getValue('id_feature')) {
            $this->setTypeValue();
            $this->list_id = 'feature_value';
            $this->lang = true;

            // Action for list
            $this->addRowAction('edit');
            $this->addRowAction('delete');

            if (!Validate::isLoadedObject($obj = new Feature((int) $id))) {
                $this->errors[] = $this->trans('An error occurred while updating the status for an object.', [], 'Admin.Notifications.Error') . ' <b>' . $this->table . '</b> ' . $this->trans('(cannot load object)', [], 'Admin.Notifications.Error');

                return;
            }

            $this->feature_name = $obj->name;
            $this->toolbar_title = $this->feature_name[$this->context->employee->id_lang];
            $this->fields_list = [
                'id_feature_value' => [
                    'title' => $this->trans('ID', [], 'Admin.Global'),
                    'align' => 'center',
                    'class' => 'fixed-width-xs',
                ],
                'value' => [
                    'title' => $this->trans('Value', [], 'Admin.Global'),
                ],
                /* ADDED PS8MODULES */
                'custom' => [
                    'title' => $this->trans('Custom', [], 'Admin.Global'),
                    'align' => 'center',
                    'class' => 'fixed-width-xs',
                    'type' => 'bool',
                    'filter_key' => 'custom'
                ],
            ];

            $this->_where = sprintf('AND `id_feature` = %d', (int) $id);
            self::$currentIndex = self::$currentIndex . '&id_feature=' . (int) $id . '&viewfeature';
            $this->processFilter();

            return parent::renderList();
        }
    }

 

2. find the function getList(.....) and edit it like this:

public function getList($id_lang, $order_by = null, $order_way = null, $start = 0, $limit = null, $id_lang_shop = false)
    {
        
        if ($this->table == 'feature_value') {
            $this->_where .= ' AND (a.custom = 0 OR a.custom IS NULL OR a.custom = 1)';
        }

        parent::getList($id_lang, $order_by, $order_way, $start, $limit, $id_lang_shop);

        if ($this->table == 'feature') {
            $nb_items = count($this->_list);
            for ($i = 0; $i < $nb_items; ++$i) {
                $item = &$this->_list[$i];

                $query = new DbQuery();
                $query->select('COUNT(fv.id_feature_value) as count_values');
                $query->from('feature_value', 'fv');
                $query->where('fv.id_feature =' . (int) $item['id_feature']);
                $res = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query);
                $item['value'] = (int) $res;
                unset($query);
            }
        }
        
    }

 

3. save the edited file

4. clear Cache

  • Like 1
Link to comment
Share on other sites

On 9/21/2024 at 8:51 AM, ps8modules said:

Thank you very much, it works perfectly!

 

 

 

Hi.

There is no need for any special features and I am surprised that Prestashop does not already have such a feature built in.

image.png.8045d79f99e2c8271391831ee44d9ffb.png

 

Just edit the file:
./controllers/admin/AdminFeaturesController.php

1. find the renderView() function and edit it like this:

public function renderView()
    {
        if ($id = (int) Tools::getValue('id_feature')) {
            $this->setTypeValue();
            $this->list_id = 'feature_value';
            $this->lang = true;

            // Action for list
            $this->addRowAction('edit');
            $this->addRowAction('delete');

            if (!Validate::isLoadedObject($obj = new Feature((int) $id))) {
                $this->errors[] = $this->trans('An error occurred while updating the status for an object.', [], 'Admin.Notifications.Error') . ' <b>' . $this->table . '</b> ' . $this->trans('(cannot load object)', [], 'Admin.Notifications.Error');

                return;
            }

            $this->feature_name = $obj->name;
            $this->toolbar_title = $this->feature_name[$this->context->employee->id_lang];
            $this->fields_list = [
                'id_feature_value' => [
                    'title' => $this->trans('ID', [], 'Admin.Global'),
                    'align' => 'center',
                    'class' => 'fixed-width-xs',
                ],
                'value' => [
                    'title' => $this->trans('Value', [], 'Admin.Global'),
                ],
                /* ADDED PS8MODULES */
                'custom' => [
                    'title' => $this->trans('Custom', [], 'Admin.Global'),
                    'align' => 'center',
                    'class' => 'fixed-width-xs',
                    'type' => 'bool',
                    'filter_key' => 'custom'
                ],
            ];

            $this->_where = sprintf('AND `id_feature` = %d', (int) $id);
            self::$currentIndex = self::$currentIndex . '&id_feature=' . (int) $id . '&viewfeature';
            $this->processFilter();

            return parent::renderList();
        }
    }

 

2. find the function getList(.....) and edit it like this:

public function getList($id_lang, $order_by = null, $order_way = null, $start = 0, $limit = null, $id_lang_shop = false)
    {
        
        if ($this->table == 'feature_value') {
            $this->_where .= ' AND (a.custom = 0 OR a.custom IS NULL OR a.custom = 1)';
        }

        parent::getList($id_lang, $order_by, $order_way, $start, $limit, $id_lang_shop);

        if ($this->table == 'feature') {
            $nb_items = count($this->_list);
            for ($i = 0; $i < $nb_items; ++$i) {
                $item = &$this->_list[$i];

                $query = new DbQuery();
                $query->select('COUNT(fv.id_feature_value) as count_values');
                $query->from('feature_value', 'fv');
                $query->where('fv.id_feature =' . (int) $item['id_feature']);
                $res = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query);
                $item['value'] = (int) $res;
                unset($query);
            }
        }
        
    }

 

3. save the edited file

4. clear Cache

 

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...