Jump to content

Order products by custom field in table product


Recommended Posts

Hello,

I added a custom field in the database (table = product),

I´m trying to sort products by this field but prestashop sorts by the order which products were inserted.

The data in the database are correct.

I don´t know why it doesn´t work

 

Someone can help me?

Link to comment
Share on other sites

I override Product.php adding this

<?php
Class Product extends ProductCore
{
    public $custom;

    public function __construct($id_product = null, $full = false, $id_lang = null, $id_shop = null, Context $context = null)
    {
        self::$definition['fields']['custom'] = array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'size' => 255);
        parent::__construct($id_product, $full, $id_lang, $id_shop, $context);
    }

}

?>

also CategoryController.php

<?php
Class CategoryController extends CategoryControllerCore
{
    public function productSort()
    {
        // $this->orderBy = Tools::getProductsOrder('by', Tools::getValue('orderby'));
        // $this->orderWay = Tools::getProductsOrder('way', Tools::getValue('orderway'));
        // 'orderbydefault' => Tools::getProductsOrder('by'),
        // 'orderwayposition' => Tools::getProductsOrder('way'), // Deprecated: orderwayposition
        // 'orderwaydefault' => Tools::getProductsOrder('way'),

        $stock_management = Configuration::get('PS_STOCK_MANAGEMENT') ? true : false; // no display quantity order if stock management disabled
        $order_by_values = array(0 => 'name', 1 => 'price', 2 => 'date_add', 3 => 'date_upd', 4 => 'position', 5 => 'manufacturer_name', 6 => 'quantity', 7 => 'reference', 8 => 'custom');
        $order_way_values = array(0 => 'asc', 1 => 'desc');
        $this->orderBy = Tools::strtolower(Tools::getValue('orderby', $order_by_values[(int)Configuration::get('PS_PRODUCTS_ORDER_BY')]));
        $this->orderWay = Tools::strtolower(Tools::getValue('orderway', $order_way_values[(int)Configuration::get('PS_PRODUCTS_ORDER_WAY')]));
        if (!in_array($this->orderBy, $order_by_values))
            $this->orderBy = $order_by_values[0];
        if (!in_array($this->orderWay, $order_way_values))
            $this->orderWay = $order_way_values[0];

        $this->context->smarty->assign(array(
            'orderby' => $this->orderBy,
            'orderway' => $this->orderWay,
            'orderbydefault' => $order_by_values[(int)Configuration::get('PS_PRODUCTS_ORDER_BY')],
            'orderwayposition' => $order_way_values[(int)Configuration::get('PS_PRODUCTS_ORDER_WAY')], // Deprecated: orderwayposition
            'orderwaydefault' => $order_way_values[(int)Configuration::get('PS_PRODUCTS_ORDER_WAY')],
            'stock_management' => (int)$stock_management));
    }
}

?>

product-sort.tpl adding this

<option value="custom:asc"{if $orderby eq 'custom' AND $orderway eq 'asc'} selected="selected"{/if}>{l s='According Herakles: Lowest first'}</option>
            <option value="custom:desc"{if $orderby eq 'custom' AND $orderway eq 'desc'} selected="selected"{/if}>{l s='According Herakles: Highest first'}</option>

In Category.php I add

if ($order_by == 'id_product' || $order_by == 'date_add' || $order_by == 'date_upd' || $order_by == 'custom')
            $order_by_prefix = 'p';

In informations.tpl I add

  <div class="form-group">
        <label class="control-label col-lg-3" for="custom">
            <span class="label-tooltip" data-toggle="tooltip"
                title="{l s='Order the product over other products.'}">
                {$bullet_common_field} {l s='Custom'}
            </span>
        </label>
        <div class="col-lg-3">
            <input size="255" maxlength="255" type="text" id="custom" name="custom" value="{$product->custom|escape:'html':'UTF-8'}" />
        </div>
    </div>

In AdminImportController.php inside function __construct()

case $this->entities[$this->l('Products')]:
				self::$validators['image'] = array(
					'AdminImportController',
					'split'
				);

				$this->available_fields = array(
					'no' => array('label' => $this->l('Ignore this column')),
					'id' => array('label' => $this->l('ID')),
					'active' => array('label' => $this->l('Active (0/1)')),
					'name' => array('label' => $this->l('Name')),
					'category' => array('label' => $this->l('Categories (x,y,z...)')),
					'price_tex' => array('label' => $this->l('Price tax excluded')),
					'price_tin' => array('label' => $this->l('Price tax included')),
					'id_tax_rules_group' => array('label' => $this->l('Tax rules ID')),
					'wholesale_price' => array('label' => $this->l('Wholesale price')),
					'on_sale' => array('label' => $this->l('On sale (0/1)')),
					'reduction_price' => array('label' => $this->l('Discount amount')),
					'reduction_percent' => array('label' => $this->l('Discount percent')),
					'reduction_from' => array('label' => $this->l('Discount from (yyyy-mm-dd)')),
					'reduction_to' => array('label' => $this->l('Discount to (yyyy-mm-dd)')),
					'reference' => array('label' => $this->l('Reference #')),
					'supplier_reference' => array('label' => $this->l('Supplier reference #')),
					'supplier' => array('label' => $this->l('Supplier')),
					'manufacturer' => array('label' => $this->l('Manufacturer')),
					'ean13' => array('label' => $this->l('EAN13')),
					'upc' => array('label' => $this->l('UPC')),
					'ecotax' => array('label' => $this->l('Ecotax')),
					'width' => array('label' => $this->l('Width')),
					'height' => array('label' => $this->l('Height')),
					'depth' => array('label' => $this->l('Depth')),
					'weight' => array('label' => $this->l('Weight')),
					'quantity' => array('label' => $this->l('Quantity')),
					'minimal_quantity' => array('label' => $this->l('Minimal quantity')),
					'visibility' => array('label' => $this->l('Visibility')),
					'additional_shipping_cost' => array('label' => $this->l('Additional shipping cost')),
					'unity' => array('label' => $this->l('Unit for the unit price')),
					'unit_price' => array('label' => $this->l('Unit price')),
					'description_short' => array('label' => $this->l('Short description')),
					'description' => array('label' => $this->l('Description')),
					'tags' => array('label' => $this->l('Tags (x,y,z...)')),
					'meta_title' => array('label' => $this->l('Meta title')),
					'meta_keywords' => array('label' => $this->l('Meta keywords')),
					'meta_description' => array('label' => $this->l('Meta description')),
					'link_rewrite' => array('label' => $this->l('URL rewritten')),
					'available_now' => array('label' => $this->l('Text when in stock')),
					'available_later' => array('label' => $this->l('Text when backorder allowed')),
					'available_for_order' => array('label' => $this->l('Available for order (0 = No, 1 = Yes)')),
					'available_date' => array('label' => $this->l('Product availability date')),
					'date_add' => array('label' => $this->l('Product creation date')),
					'show_price' => array('label' => $this->l('Show price (0 = No, 1 = Yes)')),
					'image' => array('label' => $this->l('Image URLs (x,y,z...)')),
					'delete_existing_images' => array(
						'label' => $this->l('Delete existing images (0 = No, 1 = Yes)')
					),
					'features' => array('label' => $this->l('Feature (Name:Value:Position:Customized)')),
					'online_only' => array('label' => $this->l('Available online only (0 = No, 1 = Yes)')),
					'condition' => array('label' => $this->l('Condition')),
					'customizable' => array('label' => $this->l('Customizable (0 = No, 1 = Yes)')),
					'uploadable_files' => array('label' => $this->l('Uploadable files (0 = No, 1 = Yes)')),
					'text_fields' => array('label' => $this->l('Text fields (0 = No, 1 = Yes)')),
					'out_of_stock' => array('label' => $this->l('Action when out of stock')),
					'shop' => array(
						'label' => $this->l('ID / Name of shop'),
						'help' => $this->l('Ignore this field if you don\'t use the Multistore tool. If you leave this field empty, the default shop will be used.'),
					),
					'advanced_stock_management' => array(
						'label' => $this->l('Advanced Stock Management'),
						'help' => $this->l('Enable Advanced Stock Management on product (0 = No, 1 = Yes).')
					),
					'depends_on_stock' => array(
						'label' => $this->l('Depends on stock'),
						'help' => $this->l('0 = Use quantity set in product, 1 = Use quantity from warehouse.')
					),
					'warehouse' => array(
						'label' => $this->l('Warehouse'),
						'help' => $this->l('ID of the warehouse to set as storage.')
					),
					'custom' => array('label' => $this->l('Custom')),
				);

 and in functionImport() I added

	if (isset($product->custom))
		$product->custom= strval($product->custom);
Link to comment
Share on other sites

  • 1 month later...

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...