Prestashop 1.7 How to Display data from database to tpl - sql

i'm new on Prestashop, i have a little problem on prestashop 1.7, i just bought a module for my orders, on the page of details when i launch smarty i can see the array $order_reference with some information, but when i go to the page of my history i can't access to this information, i don't know how to add this information to access it with my page history, do i have to create a function on history controller or class with dbclass or do u have any others solutions to add my details information of my order ?
The problem is my information is in a another table who call eo_orderreference and i can't access to it with my orders object. I think i need to write a function but as i see on internet it is wrong to write it on a tpl file, i have to do it on my class but i don't know how.
This is the information i want to add
SELECT 'order_reference' FROM eo_orderreference
Sorry i can't upload image because it contains sensible informations.
Thank for your time.

Smarty templates are meant to display data,
you should perform your query in the PHP file that recall your TPL and then use the Prestashop
assign()
method to return data to your TPL as smarty variables that you can show at front office.
See here for more details.

you can create the sql query within a function either within a module or a controller.
For example:
public function getAccessories($id_product)
{
// Your code
}
into a hook or initcontent, you can call the function on the query sql and assign it a template:
public function hookDiplayAccessoryExtraProduct($params)
{
$accessories = $this->getAccessories((int)$params['id_product']);
$this->context->smarty->assign(array(
'accessories_custom' => $accessories,
)
);
return $this->display(__FILE__, 'views/templates/front/accessory.tpl');
}

Related

Which hook I should use to get info about cart after payment in Prestashop 1.6

I create a module. When user bought some product I want to display a special page for him. To do this I need a information about cart after payment. Which hook I should use to this ?
Thanks for help
You can use actionOrderStatusUpdate.
public function hookActionOrderStatusUpdate($params)
{
// You can use $params['newOrderStatus'] or $params['id_order'], i. e.:
$order = new Order((int)$params['id_order']);
if (Validate::isLoadedObject($order) && $order->valid)
{
// The order is paid, you code goes here...
}
}
if you want to redirect the customer to a specific page instead of the standard OrderConfirmation, you can create a module and register/use the hooks displayOrderConfirmation or displayPaymentReturn in which you have the order object as first argument where you can check if the customer bought specific products. You also can override the OrderConfirmationController to modify the standard behaviour (but it's not the best practice).
Good luck

Prestashop 1.6 Create Module to Display Carrier Filter

My Prestashop-based site is currently having an override for AdminOrdersController.php, I have placed it in override folder.
From the link provided below, it is perfectly working fine to add a Carrier filter which is not available in Prestashop 1.6 now. I have tried the solution and it is working perfectly.
Reference: Adding carrier filter in Orders page.
Unfortunately, for production site, I have no access to core files and unable to implement as such. Thus, I will need to create a custom module. Do take note that I already have an override in place for AdminOrdersController.php. I would like to tap on this override and insert the filter.
I have managed to create a module and tried placing an override (with the code provided in the URL) in mymodule/override/controller/admin/AdminOrdersController.php with the carrier filter feature.
There has been no changes/effect, I am baffled. Do I need to generate or copy any .tpl file?
Any guidance is greatly appreciated.
Thank you.
While the answer in the linked question works fine the same thing can be achieved with a module alone (no overrides needed).
Admin controllers have a hook for list fields modifications. There are two with the same name however they have different data in their params array.
actionControllernameListingFieldsModifier executes before a filter is applied to list.
actionControllernameListingFieldsModifier executes before data is pulled from DB and list is rendered.
So you can add fields to existing controller list definition like this in your module file:
public function hookActionAdminOrdersListingFieldsModifier($params) {
if (isset($params['select'])) {
$params['select'] .= ', cr.name';
$params['join'] .= ' LEFT JOIN `'._DB_PREFIX_.'carrier` cr ON (cr.`id_carrier` = a.`id_carrier`)';
}
$params['fields']['carrier'] = array(
'title' => $this->l('Carrier'),
'align' => 'text-center',
'filter_key' => 'cr!name'
);
}
Because array data is being passed into $params array by reference you can modify them in your hook and changes persist back to controller. This will append carrier column at the end of list.
It is prestashop best practice to try and solve problems through module hooks and only if there is really no way to do it with hooks, then do it with overrides.
Did you delete /cache/class_index.php ? You have to if you want your override to take effect.
If it still does not work, maybe you can process with the hook called in the AdminOrderControllers method with your new module.

Automatically add a voucher code if the URL conains a KeyWord in Prestashop

The main goal that I want to achieve is to add a voucher code if the user has clicked on a specific external link that point to my shop.
So the (javascript?) script will analyze the url and if it contains any selected keyword will add the voucher.
Anyone knows how to do that?
Thanks
I found a pretty simple solution.
I made a module.
You can find it here: GITHUB
Was pretty easy:
Generated a basic module HERE
Selected Header Hook for my new module
Modified the header hook function in the modulename.php file in root with this one:
public function hookHeader()
{
$this->context->controller->addJS($this->_path.'/views/js/front.js');
$this->context->controller->addCSS($this->_path.'/views/css/front.css');
if (Tools::getValue('voucher')){
$cartVoucher = Tools::getValue('voucher');
$idDiscount = Discount::getIdByName($cartVoucher);
Context::getContext()->cart->addDiscount($idDiscount);
}
}
Hope will help someone.
Thanks to all.

silverstripe 3 - How to add access control to generated data objects?

Good afternoon,
Please let me know if this question is not clear enough, I'll try my best to make as straight-forward as possible.
How can I add access control to objects that are generated by an end-user using my data object?
Example: I have a class that extends a DataObject. Someone logs in the back-end; fills out the form that's generated by the CMS for the data object. A record is then created in the database by the CMS.
I would like to add an access control to that newly created record in the database.
For a code scenario you can take a look at one of my posts: Silverstripe 3 - Unable to implement controller access security from CMS
The only other way I can think of asking this question is: How to Dynamically (or programmatically) create permissions for records that are created by a DataObject extension via the CMS?
Thanks for your assistance.
Update - Sample Code
///>snippet, note it also has a Manager class that extends ModelAdmin which manages this!
class component extends DataObject implements PermissionProvider{
public static $db = array(
'Title' => 'Varchar',
'Description' => 'Text',
'Status' => "Enum('Hidden, Published', 'Hidden')",
'Weight' => 'Int'
);
///All the regular permission checks (overrides), for the interface goes here, etc...
///That is: canView, canDelete, canEdit, canCreate, providePermissions
}
Now, from the back-end an end-user can add components using the Manager Interface that's generated by extending ModelAdmin. How can I add individual permissions to those added components by the end-user?
Thanks.
Update 2
Example: Add Process Data Object that extends ModelAdmin will give you this in the back end
Then, when you click on the generated 'Add Process' button, you'll get this:
Finally, someone fills out the form and clicks on the 'Create' button, which saves the data in the database. That looks like this:
Now, on that record thats created in MySQL I'd like to add granular permissions to that record. Meaning, for every record created I want to be able to Deny/Allow access to it via a Group/Individual, etc.
Is that even possible with the SilverStripe framework? Thanks.
Implement the functions canView, canEdit, canDelete, and/or canCreate on your DataObject.
Each function will return true or false depending on the conditions you set - any conditions, not just what is defined in the CMS.
See the example code on the tutorial site.

Get data from product and shop functionality

I´m starting with prestashop 1.5 and i´m creating a module where will appears: Two Featured Products and Four products on offer.
I have already create the module (.php .tpl) basic files and i can get the products id, but i don´t know how to get the data of that products (title, image, description and price).
This is my code:
module.php
public function obtenerDestacados(){
$sql = 'SELECT id_product FROM '._DB_PREFIX_.'product';
$results = Db::getInstance()->ExecuteS($sql);
return $results;
}
module.tpl
{foreach iniciallou::obtenerDestacados() as $prod}
{$prod['id_product']}
<br />
{/foreach}
How i can do something like the homeFeaturedProducts module from 0?.
I would advise you not to create custom functions, if in case you are making something very specific, since there is a lot of SQL JOINS going on in prestashop tables.
You've suggested homefeatured module, why not to grab products exactly like the module?
I currently have the hf module customized, so I can not tell you the original code, albeit take look at prestashop class files, and use created methods for grabbing products, like mostly used Category::getProducts, or Product::getProducts (parameters look up in the methods definitions in class files)