– Add featured enable/disabled button on product list page admin :
 
Log file
 
Step 1 
 
src/PrestaShopBundle/Resources/views/Admin/Product
 
File : list.html.twig
 
  <td>
              {% if product.is_featured|default(0) == 0 %}
                <a href=”#” onclick=”unitProductAction3(this, ‘enabled’); return false;”>
                  <i class=”material-icons action-disabled”>clear</i>
                </a>
              {% else %}
                <a href=”#” onclick=”unitProductAction3(this, ‘disabled’); return false;”>
                  <i class=”material-icons action-enabled “>check</i>
                </a>
              {% endif %}
            </td>
For add in head:
src/PrestaShopBundle/Resources/views/Admin/Product
 
catalog.html.twig
 
 
                 <th>
                    {{ “Featured “|trans({}, ‘Admin.Global’) }}                   
                  </th> 
Step 2
 
:src/PrestaShopBundle/Controller/Admin
 
File : ProductController.php
 
find : n unitAction
 
add code
case ‘enabled’: 
                    $hookDispatcher->dispatchMultiple([‘actionAdminActivateBefore’, ‘actionAdminProductsControllerActivateBefore’], $hookEventParameters);
                    // Hooks: managed in ProductUpdater
                    $productUpdater->featureProductIdList([$id]);
                    $this->addFlash(‘success’, $translator->trans(‘Product successfully activated as feature product.’, array(), ‘Admin.Catalog.Notification’));
                    $logger->info(‘Product activated as featured: ‘ . $id);
                    $hookDispatcher->dispatchMultiple([‘actionAdminActivateAfter’, ‘actionAdminProductsControllerActivateAfter’], $hookEventParameters);
                    break; 
                case ‘disabled’: 
                    $hookDispatcher->dispatchMultiple([‘actionAdminDeactivateBefore’, ‘actionAdminProductsControllerDeactivateBefore’], $hookEventParameters);
                    // Hooks: managed in ProductUpdater
                    $productUpdater->featureProductIdList([$id], false);
                    $this->addFlash(‘success’, $translator->trans(‘Product successfully deactivated as feature product.’, array(), ‘Admin.Catalog.Notification’));
                    $logger->info(‘Product deactivated as featured: ‘ . $id);
                    $hookDispatcher->dispatchMultiple([‘actionAdminDeactivateAfter’, ‘actionAdminProductsControllerDeactivateAfter’], $hookEventParameters);
                    break;  
 
Step 4 :
src/Adapter/Product
 
File : AdminProductDataUpdater.php
 
add code for enabled/disabled
 
public function featureProductIdList(array $productListId, $activate = true)
    {
 
         
        if (count($productListId) < 1) {
            throw new \Exception(‘AdminProductDataUpdater->activateProductIdList() should always receive at least one ID. Zero given.’, 5003);
        }
 
        $failedIdList = array();
        foreach ($productListId as $productId) {
            $product = new \ProductCore($productId);
            if (!\ValidateCore::isLoadedObject($product)) {
                $failedIdList[] = $productId;
                continue;
            }
            $product->is_featured = ($activate?1:0);
            $product->update();
            $this->hookDispatcher->dispatchForParameters(‘actionProductActivation’, array(‘id_product’ => (int)$product->id, ‘product’ => $product, ‘activated’ => $activate));
        }
 
        if (count($failedIdList) > 0) {
            throw new UpdateProductException(‘Cannot change activation state on many requested products’, 5004);
        }
        return true;
    }
 
Step 4 :
 
src/PrestaShopBundle/Resources/config/admin/
 
File : routing_product.yml                    
 
Add “disabled|enabled” in unitaction look like below
 
admin_product_unit_action:
    path:     /unit/{action}/{id}
    methods:  [POST|GET]
    defaults:
        _controller: PrestaShopBundle:Admin/Product:unit
    requirements:
        action: delete|duplicate|activate|deactivate|disabled|enabled
        id: \d+

 

backend/themes/default/js/bundle/product

add in catalog.js

function unitProductAction3(element, action) {
//alert(element);return false;

//alert(element);return false;
var form = $(‘form#product_catalog_list’);

// save action URL for redirection and update to post to bulk action instead
// using form action URL allow to get route attributes and stay on the same page & ordering.
var urlHandler = $(element).closest(‘[data-uniturl]’);
var redirectUrlHandler = $(element).closest(‘[redirecturl]’);
var redirectionInput = $(‘<input>’)
.attr(‘type’, ‘hidden’)
.attr(‘name’, ‘redirect_url’).val(redirectUrlHandler.attr(‘redirecturl’));

form.append($(redirectionInput));
var url = urlHandler.attr(‘data-uniturl’).replace(/duplicate/, action);

form.attr(‘action’, url);
// alert(url);return false;
form.submit();
}

 

Fetch data  from database

src/Adapter/Product

File:  AdminProductDataProvider.php

Function : getCatalogProductList

You have to add field.

is_featured’ => array(‘table’ => ‘sa’, ‘field’ => ‘is_featured’, ‘filtering’ => self::FILTERING_EQUAL_NUMERIC),

 

 

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 3.00 out of 5)
Loading...