I asked this same question on the official forums but received no response. Not sure if anyone here is experienced with PrestaShop but here is my issue.
I need to add an extra field in the manufacturer edit/add tab, I was able to do this by overriding renderForm in AdminManufacturersController.php like this:
public function renderForm()
{
global $shopOptions;
$this->fields_form_override = array(
array(
'type' => 'checkbox',
'label' => 'Shop',
'name' => 'shop_select',
'desc' => 'Choose The Shops This Manufacturer Applies To',
'values' => array(
'query' => $shopOptions, >> comes from array filled by db query in __construct
'id' => 'id',
'name' => 'name'
),
),
);
return parent::renderForm();
}
This works and I am now trying to find the update and create functions for a manufacturer. When editing the product classes, you can easily spot set functions like setQuantity in StockAvailable.php.
I have ssh access to the server so I was able to dig deeper with grep, to no avail. It seems like it uses some sort of function to auto insert into the database whilst some classes use a plain old execute with a normal query.
Any ideas on where this could be found?
On Prestashop 1.6.x you do not need to amend any function for it to have CRUD functionalities. You just need to add it in :
RenderForm (like you already did)
Add the variable in the manufacturer class (Manufacturer.php) like public $shop_select;
Add it in the public static $definition array in the manufacturer class
Add the column in manufacturer or manufacturer_lang table depending on whether your field is a lang field.
Cheers :)
Related
I'm developing a prestashop module and I'm trying to show a category tree in my backoffice configuration page.
I'm trying to follow this instructions below but I don't know exactly where to add this code.
It should be inside main module's php? or inside a separate .php file and call it from the main one (don't know how to do it either).
As much time I'm spending trying to figure out, how to implement the code in the link above, the more I think I'm losing my time.
I see that "use" files, and this JS, " /admin-dev/themes/new-theme/js/components/form/choice-tree.js " are not in any prestashop folders.
Well, you should invest some time and learn Symfony since this is what you need to build backend modules for Prestashop 1.7.
As a pointer, you need to create a form class extending the CommonAbstractType, add a build form method. e.g. :
public function buildForm(FormBuilderInterface $builder, array $options)
{
$this->context = Context::getContext();
$parents = [
['id_category' => 2, 'name' => 'Home', 'children' => $this->getSubCategories(1, true, 2)]
];
$builder->add('category', CategoryChoiceTreeType::class, [
'choices_tree' => $parents,
'choice_value' => 'id_category',
'choice_children' => 'children',
'choice_label' => 'name',
'disabled_values' => $disabledCategories,
'label' => 'Choose a category'
])
then add methods for retrieving the data to populate the form fields.
Then use this class in your controller and display the form:
$form = $this->createForm(YourFormForm::class);
Also add a processForm to process data.
As mentioned, this is not a copy/paste situation you need to understand the Symfony workflow.
The only way that I found to "paint" the categorytree in my configuration page is adding this code to the inputs form array:
Can anyone tell me how to retrieve users selection data to my database?
It does not work as any other form field.
array(
'type' => 'categories',
'label' => $this->l('Destination Category'),
'desc' => $this->l('Select ONE Category'),
'name' => 'CATEGORY_CATEGORY_TO',
'tree' => [
// 'selected_categories' => [],
'disabled_categories' => null,
'use_search' => false,
'use_checkbox' => false,
'id' => 'id_category_tree',
],
'required' => true
),
Well, it is SOLVED!!!! Finally it was very simple, but you must get the correct info for you particular case.
#Robertino's answer might be the best implementation, I don't know, but it became impossible to solve for me,
I uses this code below, and called $categoryTree from the form input. This input must be type=> categories_select
Thanks for your time, and for the help of another post from this forum.
$root = Category::getRootCategory();
//Generating the tree
$tree = new HelperTreeCategories('categories_1'); //The string in param is the ID used by the generated tree
$tree->setUseCheckBox(false)
->setAttribute('is_category_filter', $root->id)
->setRootCategory($root->id)
->setSelectedCategories(array((int)Configuration::get('CATEGORY_1'))) //if you wanted to be pre-carged
->setInputName('CATEGORY_1'); //Set the name of input. The option "name" of $fields_form doesn't seem to work with "categories_select" type
$categoryTree = $tree->render();
And the Form:
array(
'type' => 'categories_select',
'label' => $this->l('Category'),
'desc' => $this->l('Select Category '),
'name' => 'CATEGORY_1', //No ho podem treure si no, no passa la variable al configuration
'category_tree' => $categoryTree, //This is the category_tree called in form.tpl
'required' => true
I am working on Magento 1.8.1 with a theme incorporated. Multiple extensions involved as well and most are completely customized.
I have been trying to get the company name to appear on the Sales Order Grid for some time now and have had some luck.
First off, I have copied /core/Mage/Adminhtml/Block/Sales/Order/Grid.php to /local/Mage/Adminhtml/Block/Sales/Order/Grid.php.
Then I updated the code in _prepareCollection() to this:
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass());
$collection->getSelect()->join(
array('addressTable' => 'sales_flat_order_address'),'main_table.entity_id = addressTable.parent_id AND addressTable.address_type = "billing"',array('billing_company'=>'company'));
$this->setCollection($collection);
return parent::_prepareCollection();
}
While adding the following to _prepareColumns:
$this->addColumn('company', array(
'header' => Mage::helper('sales')->__('Bill to Company'),
'index' => 'billing_company',
));
Everything is working like it should until I decided it would be really nice to have the Ship to Company as well since most of our customers buy for other companies.
To accomplish this I added the column with no problem just like I did previously:
$this->addColumn('company', array(
'header' => Mage::helper('sales')->__('Ship to Company'),
'index' => 'shipping_company',
));
The column added with no problem except it was not in the place I put it (before shipping_name), This was just adding the column no data yet.
Now to add the data, I would add another collection under _prepareCollection since the shipping information is on a different row compared to billing in the table, like this:
$collection->getSelect()->join(
array('addressTable' => 'sales_flat_order_address'),'main_table.entity_id = addressTable.parent_id AND addressTable.address_type = "shipping"',array('shipping_company'=>'company'));
But when I try this I get an error:
You cannot define a correlation name 'addressTable' more than once
I get quite a bit of conflict between the two columns as well. For instance, when I do not have the Ship to Company commented under _prepareColumns it shows the Ship to Company column where the Bill to Company column should be. There is also no data in the column. As soon as I comment Ship to Company Bill to Company appears and has the correct data.
Basically, I just need to get the Ship to Company Column to show up as well as the Bill to Company Column. Preferably next to the appropriate Name.
I have already added the Company Column to the Customer Grid and Create New Order Customer Grid with no problem as well.
Okay so my edit above was not exactly correct. The columns show up and populate just fine but after trying to search the field I was receiving an error. So after more research, I found this solution: Using column aliases in Sales Order Grid field
The answer to this question works perfect just had to change a few minor things. I attached my code below just in case someone else is looking for this solution.
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass());
$collection->getSelect()->join(array('address_billing' => $collection->getTable("sales/order_address")),'main_table.entity_id = address_billing.parent_id AND address_billing.address_type = "billing"',array('address_billing.company as billing_company'));
$collection->getSelect()->join(array('address_shipping' => $collection->getTable("sales/order_address")),'main_table.entity_id = address_shipping.parent_id AND address_shipping.address_type = "shipping"',array('address_shipping.company as shipping_company'));
$this->setCollection($collection);
return parent::_prepareCollection();
}
Under _prepareColumns() change to this:
$this->addColumn('company', array(
'header' => Mage::helper('sales')->__('Bill to Company'),
'index' => 'billing_company',
'filter_index' => 'address_billing.company',
));
$this->addColumnAfter('shipping_company', array(
'header' => Mage::helper('sales')->__('Ship to Company'),
'index' => 'shipping_company',
'filter_index' => 'address_shipping.company',
),
'billing_name'
);
Remember I had to use the addColumnAfter function to get my shipping company column placed in the right spot.
With this fix everything is finally working the way I want it to!
Happy coding!
modify code is
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass());
$collection->getSelect()->join(array('billing'=>'sales_flat_order_address'),'main_table.entity_id=billing.parent_id and billing.address_type="billing"',array('*'));
$collection->getSelect()->join(array('shipping'=>'sales_flat_order_address'),'main_table.entity_id=shipping.parent_id and shipping.address_type="shipping"',array('shipping.company as shipping_company'));
$this->setCollection($collection);
parent::_prepareCollection();
return Mage_Adminhtml_Block_Widget_Grid::_prepareCollection();
}
And add columns in _prepareColumns()
$this->addColumn('company', array(
'header' => Mage::helper('sales')->__(' Billing company'),
'index' => 'company',
'filter_index' => 'billing.company',
));
$this->addColumn('shipping_company', array(
'header' => Mage::helper('sales')->__(' Shipping company'),
'index' => 'shipping_company',
'filter_index' => 'shipping_company',
));
I have added a new field to my database. I also have managed to add necessary codes and functions in catalog section. This new field is related to the customer. The data related to this new field gets added successfully to the database.
This new field belongs to Customer table.
Now, I want to know, when viewing customer's details in the admin section, how this new field should be retrieved from database? I mean which file should be edited for this purpose?
getCustomer($customer_id) and getCustomers($data = array()) are the functions used to get customer data.
Since they are SELECT * querys your field is being processed automatically.
Afterwards you need to go in the Controller section in the controller\sale folder and there you have customer.php, custom_field.php and and edit the ones that you need. For example:
$this->data['customers'][] = array(
'customer_id' => $result['customer_id'],
'name' => $result['name'],
'email' => $result['email'],
'customer_group' => $result['customer_group'],
'status' => ($result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled')),
'approved' => ($result['approved'] ? $this->language->get('text_yes') : $this->language->get('text_no')),
'ip' => $result['ip'],
'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added'])),
'selected' => isset($this->request->post['selected']) && in_array($result['customer_id'], $this->request->post['selected']),
'action' => $action
);
add your field in this array (this is from customer.php).
And finally edit the .tpl files that are called from the view\template\sale folder so they appear where they want them to.
Hope i was clear enough.
I have a model called Forwards this model uses a table from my Database. However it is required for some of the views connected to this model to use my costum datasource (This is a datasource i have created which links it to an API).
Now my question how can i add a datasource to a Model that already has a datasource.
And how can i tell the Paginator that it has to use this datasource when it is paginating (so it doesnt confilct)
Update
I have been reading abit it seems the only way to use two datasource in a model is to Hack abit, so i was wondering is it possible to use two models from one controller while still paginating?
i want to keep these settings
public $paginate = array(
'fields' => array(
'Offer.id',
'Offer.name',
'OfferUrl.preview_url',
'Stat.ltr',
'Stat.cpc',
'Category.name',
'Country.name')
, 'conditions' => array('Stat.date' => array(
'conditional' => 'BETWEEN'
, 'values' => array()
),
),
'group' => array('Stat.offer_id'),
'Method' => 'getStats',
'totals' => true,
'limit' => 20
);
I set these within my controller
You can change the data source by calling Model::setDatasSource().
You can change the model the paginator uses by:
$this->Paginator->paginate($ModelInstance);
Or
$this->Paginator->paginate('ModelName');
You might want to read that page.
Don't use Controller::paginate any more but instead $this->Paginator->settings. For settings per model use
$this->Paginator->settings['ModelName'] = array(/*...*/);
I'm trying to add the following functionality, however I'm not sure where to start. Any advice, examples, or direction would be greatly appreciated.
I want to add button to the cgridview of the main model in this context. Each of the records available in the cgridview for this model, have a unique attribute called lot for example R3XSEF9
There is another secondary table/model in my database that has records with this same lot attribute. However, this table only has certain records out of all the possible records, sometimes duplicates, and has a set of different attributes.
What I'd like to do is, using the lot attribute, for example lot R3XSEF9 from my cgridview, search the secondary table to see if there is one ore more corresponding rows which contains that same lot R3XSEF9.
If so, I would like the button to be displayed in my CButtonColumn and link to the views for those corresponding models of the secondary table. If not, I would like no button to appear.
Thanks for any help. If any clarification is required, I would be happy to do so.
First of all you need to link tables using "relations" function in model class. If you use FOREIGN KEY constraint in DB relations already filled.
SQL statement:
CREATE TABLE Model1
(
...
FOREIGN KEY(lot) REFERENCES MainModel(lot) ON UPDATE CASCADE ON DELETE RESTRICT,
...
)
Model class:
class MainModel extends CActiveRecord
{
...
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'lots' => array(self::HAS_MANY, 'Model2', 'lot'),
);
}
Then you can use custom button column in your grid (view file) like this:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id' => 'main-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
...
array(
'class' => 'CButtonColumn',
'template' => '{lots}',
'header' => 'Lots',
'buttons' => array(
'lots' => array(
'label' => 'Lots',
'imageUrl' => Yii::app()->request->baseUrl.'/img/....png',
'url' => 'Yii::app()->createUrl("controller1/lotlistbymainid", array("id" => $data->id))',
'visible' => 'count($data->lots) > 0',
),
),
),
Explanation of button parameters to be passed thru "buttons" array you can find here. Especialy this part:
buttons property
public array $buttons;
the configuration for additional buttons. Each array element specifies a single button which has the following format:
'buttonID' => array(
'label'=>'...', // text label of the button
'url'=>'...', // a PHP expression for generating the URL of the button
'imageUrl'=>'...', // image URL of the button. If not set or false, a text link is used
'options'=>array(...), // HTML options for the button tag
'click'=>'...', // a JS function to be invoked when the button is clicked
'visible'=>'...', // a PHP expression for determining whether the button is visible
)