I want to add a new column in the order table grid in PrestaShop 1.7.7.0.
Currently, I've only managed to add a new column that gets its values based on the SQL query (code below). However, I need to redo it so that it gets $ id_order from the current row of the table and processes it according to some function. Before it was done like this:
AdminOrdersController.php
$this->fields_list = array(
'id_order' => array(
'title' => $this->trans('ID', array(), 'Admin.Global'),
'align' => 'text-center',
'class' => 'fixed-width-xs',
),
(...)
'ms_pelne' => array(
'title' => $this->trans('Pełne', array(), 'Admin.Global'),
'align' => 'text-center',
'callback' => 'msSprawdzZamowienie',
'orderby' => false,
'search' => false,
'remove_onclick' => true,
),
));
public function msSprawdzZamowienie($id_order, $tr)
{
(...)
}
Currently, unfortunately it does not work ... My present code is:
<?php
use PrestaShop\PrestaShop\Core\Grid\Column\Type\DataColumn;
use PrestaShop\PrestaShop\Core\Grid\Filter\Filter;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use PrestaShop\PrestaShop\Core\Grid\Column\Type\Common\BadgeColumn;
use PrestaShop\PrestaShop\Core\Grid\Column\ColumnCollection;
if (!defined('_PS_VERSION_')) {
exit;
}
class ms_MontowniaStron extends Module
{
// const CLASS_NAME = 'ms_mstron2';
public function __construct()
{
$this->name = 'ms_montowniastron';
$this->tab = 'front_office_features';
$this->version = '1.0.0';
$this->author = 'ms';
$this->need_instance = 0;
$this->ps_versions_compliancy = [
'min' => '1.7',
'max' => _PS_VERSION_
];
$this->bootstrap = false;
parent::__construct();
$this->displayName = $this->l('MS Pełne Zamówienie');
$this->description = $this->l('Moduł pozwalający na wyświetlanie informacji o pełnym zamówieniu.');
$this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
}
/**
* Install module and register hooks to allow grid modification.
*
* @see https://devdocs.prestashop.com/1.7/modules/concepts/hooks/use-hooks-on-modern-pages/
*
* @return bool
*/
public function install()
{
if (!parent::install()
//Installation des hooks
|| !$this->registerHook([
'actionOrderGridDefinitionModifier',
'actionOrderGridQueryBuilderModifier',
'actionOrderGridDataModifier',
'actionOrderGridPresenterModifier'
])
) {
return false;
}
return true;
}
public function uninstall() {
return parent::uninstall();
}
/**
* Hooks allows to modify Customer grid definition.
* This hook is a right place to add/remove columns or actions (bulk, grid).
*
* @param array $params
*/
public function hookActionOrderGridDefinitionModifier(array $params)
{
/** @var GridDefinitionInterface $definition */
$definition = $params['definition'];
/** @var ColumnCollection */
$columns = $definition->getColumns();
// dodajemy nową
$definition
->getColumns()
->addAfter(
'id_order',
(new BadgeColumn('ms_full_order'))
->setName($this->l('Pełne'))
->setOptions([
'field' => 'ms_full_order',
'badge_type' => 'success',
'empty_value' => '--',
])
);
}
public function hookActionOrderGridQueryBuilderModifier(array $params)
{
$searchQueryBuilder = $params['search_query_builder'];
$searchQueryBuilder->addSelect('(
SELECT
id_order
FROM
ms_orders AS msg
LIMIT 1
) as ms_full_order');
}
}
?>
Yes, below my code.
Upload to: override/controllers/admin/AdminOrdersController.php
Key of code is function "msSprawdzZamowienie()".
<?php
class AdminOrdersController extends AdminOrdersControllerCore
{
public $toolbar_title;
protected $statuses_array = array();
public function __construct()
{
$this->bootstrap = true;
$this->table = 'order';
$this->className = 'Order';
$this->lang = false;
$this->addRowAction('view');
$this->explicitSelect = true;
$this->allow_export = true;
$this->deleted = false;
parent::__construct();
$this->_select = '
a.id_currency,
a.id_order AS id_pdf,
a.id_order AS ms_pelne,
CONCAT(LEFT(c.`firstname`, 1), \'. \', c.`lastname`) AS `customer`,
osl.`name` AS `osname`,
os.`color`,
IF((SELECT so.id_order FROM `' . _DB_PREFIX_ . 'orders` so WHERE so.id_customer = a.id_customer AND so.id_order < a.id_order LIMIT 1) > 0, 0, 1) as new,
country_lang.name as cname,
IF(a.valid, 1, 0) badge_success';
$this->_join = '
LEFT JOIN `' . _DB_PREFIX_ . 'customer` c ON (c.`id_customer` = a.`id_customer`)
INNER JOIN `' . _DB_PREFIX_ . 'address` address ON address.id_address = a.id_address_delivery
INNER JOIN `' . _DB_PREFIX_ . 'country` country ON address.id_country = country.id_country
INNER JOIN `' . _DB_PREFIX_ . 'country_lang` country_lang ON (country.`id_country` = country_lang.`id_country` AND country_lang.`id_lang` = ' . (int) $this->context->language->id . ')
LEFT JOIN `' . _DB_PREFIX_ . 'order_state` os ON (os.`id_order_state` = a.`current_state`)
LEFT JOIN `' . _DB_PREFIX_ . 'order_state_lang` osl ON (os.`id_order_state` = osl.`id_order_state` AND osl.`id_lang` = ' . (int) $this->context->language->id . ')';
$this->_orderBy = 'id_order';
$this->_orderWay = 'DESC';
$this->_use_found_rows = true;
$statuses = OrderState::getOrderStates((int) $this->context->language->id);
foreach ($statuses as $status) {
$this->statuses_array[$status['id_order_state']] = $status['name'];
}
$this->fields_list = array(
'id_order' => array(
'title' => $this->trans('ID', array(), 'Admin.Global'),
'align' => 'text-center',
'class' => 'fixed-width-xs',
),
'reference' => array(
'title' => $this->trans('Reference', array(), 'Admin.Global'),
),
'ms_pelne' => array(
'title' => $this->trans('Pełne', array(), 'Admin.Global'),
'align' => 'text-center',
'callback' => 'msSprawdzZamowienie',
'orderby' => false,
'search' => false,
'remove_onclick' => false,
),
'new' => array(
'title' => $this->trans('New client', array(), 'Admin.Orderscustomers.Feature'),
'align' => 'text-center',
'type' => 'bool',
'tmpTableFilter' => true,
'orderby' => false,
),
'customer' => array(
'title' => $this->trans('Customer', array(), 'Admin.Global'),
'havingFilter' => true,
),
);
if (Configuration::get('PS_B2B_ENABLE')) {
$this->fields_list = array_merge($this->fields_list, array(
'company' => array(
'title' => $this->trans('Company', array(), 'Admin.Global'),
'filter_key' => 'c!company',
),
));
}
$this->fields_list = array_merge($this->fields_list, array(
'total_paid_tax_incl' => array(
'title' => $this->trans('Total', array(), 'Admin.Global'),
'align' => 'text-right',
'type' => 'price',
'currency' => true,
'callback' => 'setOrderCurrency',
'badge_success' => true,
),
'payment' => array(
'title' => $this->trans('Payment', array(), 'Admin.Global'),
),
'osname' => array(
'title' => $this->trans('Status', array(), 'Admin.Global'),
'type' => 'select',
'color' => 'color',
'list' => $this->statuses_array,
'filter_key' => 'os!id_order_state',
'filter_type' => 'int',
'order_key' => 'osname',
),
'date_add' => array(
'title' => $this->trans('Date', array(), 'Admin.Global'),
'align' => 'text-right',
'type' => 'datetime',
'filter_key' => 'a!date_add',
),
'id_pdf' => array(
'title' => $this->trans('PDF', array(), 'Admin.Global'),
'align' => 'text-center',
'callback' => 'printPDFIcons',
'orderby' => false,
'search' => false,
'remove_onclick' => true,
),
));
if (Country::isCurrentlyUsed('country', true)) {
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT DISTINCT c.id_country, cl.`name`
FROM `' . _DB_PREFIX_ . 'orders` o
' . Shop::addSqlAssociation('orders', 'o') . '
INNER JOIN `' . _DB_PREFIX_ . 'address` a ON a.id_address = o.id_address_delivery
INNER JOIN `' . _DB_PREFIX_ . 'country` c ON a.id_country = c.id_country
INNER JOIN `' . _DB_PREFIX_ . 'country_lang` cl ON (c.`id_country` = cl.`id_country` AND cl.`id_lang` = ' . (int) $this->context->language->id . ')
ORDER BY cl.name ASC');
$country_array = array();
foreach ($result as $row) {
$country_array[$row['id_country']] = $row['name'];
}
$part1 = array_slice($this->fields_list, 0, 3);
$part2 = array_slice($this->fields_list, 3);
$part1['cname'] = array(
'title' => $this->trans('Delivery', array(), 'Admin.Global'),
'type' => 'select',
'list' => $country_array,
'filter_key' => 'country!id_country',
'filter_type' => 'int',
'order_key' => 'cname',
);
$this->fields_list = array_merge($part1, $part2);
}
$this->shopLinkType = 'shop';
$this->shopShareDatas = Shop::SHARE_ORDER;
if (Tools::isSubmit('id_order')) {
// Save context (in order to apply cart rule)
$order = new Order((int) Tools::getValue('id_order'));
$this->context->cart = new Cart($order->id_cart);
$this->context->customer = new Customer($order->id_customer);
}
$this->bulk_actions = array(
'updateOrderStatus' => array('text' => $this->trans('Change Order Status', array(), 'Admin.Orderscustomers.Feature'), 'icon' => 'icon-refresh'),
);
}
public function msSprawdzZamowienie($id_order, $tr)
{
static $valid_order_state = array();
$order = new Order($id_order);
if (!Validate::isLoadedObject($order)) {
return '';
}
$products = $order->getProducts();
$html = '';
$products_available = array();
foreach( $products AS $product )
{
$available = ( $product[ 'product_quantity' ] <= $product[ 'quantity' ] && $product[ 'quantity' ] > 0 ) ? '1' : '0';
array_push( $products_available, $available );
}
$this->context->smarty->assign(array(
'order' => $order,
'tr' => $tr,
));
if( in_array( '0', $products_available ) ){
$html = '<span style="background:red;color:#fff;padding:2px 5px;border-radius:3px;">NIE</span>';
} else {
$html = '<span style="background:green;color:#fff;padding:2px 5px;border-radius:3px;">TAK</span>';
}
return $html;
}
}