Automatically add a voucher code if the URL conains a KeyWord in Prestashop - 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.

Related

Prestashop 1.7 How to Display data from database to tpl

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');
}

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.

How to implement backoffice controller

after lot of google searches and going through prestashop's official documentation over and over again, I still couldn't find an example of backoffice controller. I even looked into the modules folder of prestashop's installation, but couldn't find any.
I need to implement 3 different back-office pages, each served by its own controller and view.
Can anyone provide me any hint, or even one working example...Just hello world is more than enough.
Thanks in advance...
notice:i write this article for prestashop1.5 and i don't check it for prestashop 1.6.perhaps it is works for 1.6 too.
You should know every thing in this way have special structure.
step 1: Create a folder in your module folder call that 'controllers' (notice:this name is static)
step 2: Create a folder in controller folder call that 'admin' (notice:this name is static too)
step 3: Create a php class file call that AdminMyclassnameController (notice:in this name Admin at first and controller at last is the key word and thay are static but Myclassname is dynamic .attention to first words all of should be uppercase A for Admin, M for Mclass,... )
step 4: Then you should write your class in AdminMyclassnameController and this class should extent with AdminController or AdminControllerCore.
for know how does it class work you can search about helper forums in internet.
step 5: When you create the class you want a tab to show that controller notice:when act to step 1,2,3,4 this controller take an automatic URL for access to this URL you should create a tab.in yourmodule.php in install() you should add this codes
$tab = new Tab();
$tab->class_name = 'AdminTest';
$tab->module = 'test';
$tab->id_parent = 9;
$tab->position = 11;
then you can see the tab in admin office that redirect to your controller.
i am tired to continue ... but if you want more send a message and i continue this article
this is use full site really clear:
http://doc.prestashop.com/display/PS15/Using+helpers+to+overload+a+back-office+template
http://presthemes.com/prestashop-news/modules-classes-and-controller-override-by-julien-breux-4.html
http://doc.prestashop.com/display/PS15/Diving+into+PrestaShop+Core+development
http://www.prestashop.com/forums/topic/270177-solved-making-own-objectmodel-class-doesnt-work/
http://doc.prestashop.com/display/PS15/New+Developers+Features+In+PrestaShop+1.5
http://blog.belvg.com/how-to-implement-a-controller.html
best regards

Adding quick link on admin home page (Prestashop 1.5.4)

Good day! Tell me, how can I add a quick link on the home page of the administrator to configure my module?
Follow the following steps:
1) In admin section to go Administration at top menu and then click on Quick Access.
2) IN next page click on Add new and you will see a form
3) Now open admin panel in another tab and to the module page or section, of which you want to place a link in quick access.
4) Copy that complete link in note pad and remove the token section of the link. It is required to remove the token section according to Prestashop.
5) Now come back to the add new form for quick access, give your link a name and then copy that modified link to the Url field.
6) Save it and you will have that link in quick access.
The above is method is used to add it at admin. Now if you want to add it pro-grammatically you can follow the following steps.
1) In your module in the install function, user a code like below
Db::getInstance()->insert('quick_access', array('new_window' => 0, 'link' => 'link_to_your_module_page'));
//an entry is made in quick_access table, get the quick_access id to insert lang data
$id = Db::getInstance()->Insert_ID(); //this will give you last inserted ID from quick_access table which is your current quick_access id.
//now make insertions in quick_access_lang table for multi language data.
//get all your site languages, and place a foreach loop and in that loop insert
//data into the quick_access_lang table using below code
Db::getInstance()->insert('quick_access_lang', array('id_quick_access' => $id, 'id_lang' => 'lang_id', 'name' => 'name of your link'));
//Now for uninstalling module, you want to delete the link, so you need to store the quick access link id in configuration table so you can use it later.
Configuration::updateValue('MY_QUICK_ACCESS_LINK_ID', $id);
2) Now in your uninstall function in your module class, place the below code
$id = Configuration::get('MY_QUICK_ACCESS_LINK_ID'); //get id of your quick access link
Db::getInstance()->delete('quick_access', 'where id_quick_access = '.$id);
Db::getInstance()->delete('quick_access_lang', 'where id_quick_access = '.$id);
//now delete the id from config table
Configuration::deleteByName('MY_QUICK_ACCESS_LINK_ID');
Note : The above code is not tested, it may / may not need some adjustments.
Thank you
There is a hook in Prestashop DisplayAdminHomeQuickLinks it will help you to add quick link on prestashop admin panel. I have use this in my theme. http://goo.gl/0S3mn And it will help you to solve the quick link.
In Prestashop 1.6.1 (maybe earlier too) in Admin view, at the top of the page, Quick Access has the option to "Add current page to QuickAccess". So just navigate to the Configuration page you need and use it.

GetResource, dynamic parent.

my problem is the following. I'm currently making a blog-page with get-page, get-resources, form-it, and wayfinder. This question requires a decent amount of knowledge about Modx and snippits. I've got the page numberin and all working and i've got a template page with all my calls in it (called weblogTemplate). This template has the following wayfinder call in it :
[[!getResources? &parents=`5` &limit=`5` &tpl=`blogPost`]]
[[!getPage?
&elementClass=`modSnippet`
&element=`getResources`
&parents=`4`
&depth=`2`
&limit=`5`
&pageNavOuterTpl=`[[+first]][[+prev]][[+pages]][[+next]][[+last]]`
&pageVarKey=`page`
&pageFirstTpl=`<li class="controlFirst"><a[[+classes]][[+title]] href="[[+href]]">Eerste pagina</a></li>`
&pageLastTpl=`<li class="controlLast"><a[[+classes]][[+title]] href="[[+href]]">Laatste pagina</a></li>`
&pagePrevTpl=`<li class="controlPrev"><a[[+classes]][[+title]] href="[[+href]]"><<</a></li>`
&pageNextTpl=`<li class="controlNext"><a[[+classes]][[+title]] href="[[+href]]">>></a></li>`
&includeTVs=`1`
&includeContent=`1`
&tpl=`blogListPost`
]]
as you can see the parent is set here to id number 5. This is fine for the homepage but any child page connected in the blog page also uses the same template and so would also have the same menu as the parent. You could use a fix to simply create 1 template for a page and keep using a different getResource call but keep in mind that it is a blog im making, new pages keep getting added. The user can't (, and wouldn't even understand to) make a template and edit any code. A solution i thought of would be to make the parent id dynamic, so it adjust to whatever page it is currently on. So for example if it was on the page with id number 12 it would make the parent call set to 12 and so show all the content under id number 12. If anyone has any ideas / thoughts / solutions i would be very grateful to hear them.
(a link about wayfinder that i used.)
The best solution would be to use two templates - one for main and one for the blog pages and use in blog templates:
&parents=`[[*id]]`
The problem with the user solveds by setting default_template in the system settings.
This worked for me:
[[!getPage?
&elementClass=`modSnippet`
&element=`getResources`
&parents=`[[*id]]`
&depth=`0`
&limit=`10`
&pageNavOuterTpl=`[[+first]][[+prev]][[+pages]][[+next]][[+last]]`
&pageVarKey=`page`
&pageFirstTpl=`<li class="controlFirst"><a[[+classes]][[+title]] href="[[+href]]">Eerste pagina</a></li>`
&pageLastTpl=`<li class="controlLast"><a[[+classes]][[+title]] href="[[+href]]">Laatste pagina</a></li>`
&pagePrevTpl=`<li class="controlPrev"><a[[+classes]][[+title]] href="[[+href]]"><<</a></li>`
&pageNextTpl=`<li class="controlNext"><a[[+classes]][[+title]] href="[[+href]]">>></a></li>`
&includeTVs=`1`
&includeContent=`1`
&tpl=`blogListPost`
]]
Thanks to Vasis for the provided help.