I'm developing a custom Prestashop Module. The requirement is simple: Add a predefined block of javascript code to specific sections of the shopping process. Those are:
Home page
Product page
Product added to cart
Purchase completed
The code will be specific to each page.
I already read the basics of module development, but I can't find documentation for this specific functionality.
I already have a working module that is installable and configurable from the back office admin. I'm assuming I need to extend the footer and check the page currently being served, but I have no idea how to do this.
It's more simple than it appears :), you have to check in which page you are, in your hookDisplayHeader method add some if:
public function hookDisplayHeader($params){
/* some code */
// check if we are in homepage
if($this->context->controller->php_self == 'index'){
$this->context->controller->addJS('path-to-index-js');
}
// check if we are in product page
if($this->context->controller->php_self == 'product'){
$this->context->controller->addJS('path-to-product-js');
}
// and so on for all other pages
/* ... */
/* some code */
}
Also there is a global variable in js
page_name
Every page in PrestaShop has a unique value of page_name smarty variable.
This variable's value can be fetched through the following code in any controller:
$this->context->smarty->tpl_vars['page_name']->value
You can add a hook (i.e. hookHeader) and then add a condition in that hook for every page where you want to add the script.
Related
I am new to cs-cart. Not really familiar with it. My latest project involves working with Cs-Cart. I have been able to work out how to output products and to work with categories.
Been trying this for a few weeks now. Been on few websites and stackoverflow questions. Nothing quite like what I want. The only S.O. question that came close has no answer.
I will like to have a little pop up when an item a specific item is added to the cart. In the popup div or box, I will like to suggest other items with. If possible with an add to cart button next to each suggestion.
I have searched everywhere but not much. I don't mind creating my own plugin for this but I don't know how to listen to those add to cart events and creating popups in cs-cart.
Any help is appreciated ..
If you add a product to cart in CS-Cart, you have a pop-up box. By default, the add to cart function is done via AJAX request.
If you would like to modify this notification's content, you have to check the /design/themes/YOUR_THEME_NAME/views/checkout/components/product_notification.tpl template file. (replace the YOUR_THEME_NAME)
However I suggest you, not to write directly into a core file (both php and tpl files). You can find the official developer documentation here.
If you would like to create a controller extension for the add to cart function, you can create a controller eg. /app/addons/my_changes/controllers/frontend/checkout.post.php. Write this code into this file:
<?php
if ($mode == "add") {
//Do something here
}
If the My changes add-on is active, this code will run after the product is added to the cart, but before the tpl file is displayed.
You can create the app/addons/my_changes/controllers/frontend/checkout.post.php file with the following content:
<?php
use Tygh\Registry;
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if ($mode == 'add') {
$added_products = Registry::get('view')->getTemplateVars('added_products');
if (!empty($added_products)) {
foreach ($added_products as $data) {
if (!empty($data['product_id']) && $data['product_id'] == THE_ID_OF_REQUIRED_PRODUCT) {
$msg = Registry::get('view')->fetch('addons/my_changes/custom_product_notification.tpl');
fn_set_notification('I', __('custom_product_notification_title'), $msg, 'I');
}
}
}
}
return;
}
then
create the "custom_product_notification_title" language variable on the Administration -> Language -> Manage translation page
create the "design/themes/YOUR_THEME/templates/addons/my_changes/custom_product_notification.tpl" template with the required content
activate the "My changes" add-on
clear the cache by adding the "?cc&ctpl" to the URL in the admin panel
I am developing a plugin for wordpress that loads javascript in a page.
But i want this plugin to load only on selected pages. Not all pages.
Can someone suggest how to do that?
Here is the code.
add_action('wp_enqueue_scripts', 'soundninja_enqueue');
function soundninja_enqueue($hook)
{
wp_enqueue_script('soundninja', // id
'http://soundninja.github.io/SNtest/build/Soundninja.min.js', // path
array('jquery'), // dependencies
0, // appends ?ver=$wordpress_version
true // in_footer
);
}
Another possible workaround would be to keep the plugin deactivated and activate it only for the required pages. This would prevent the additional overhead involved in loading the plugin on pages where the plugin is not required. And most importantly you do not have to tweak the code of the existing plugin.
Here is the post which can give you more idea http://shibashake.com/wordpress-theme/how-to-selectively-load-plugins-for-specific-pages
Depending on where you will call your function soundninja_enqueue($hook) you can easily add an if statement asking if the current page/post id is in an allowed list of ids.
But first you need to get the current page/post id, for this you have a couple of options, depending if are calling the function inside or outside of The loop.
see here on how to get the current page id in wordpress
<?php
$allowedIds = array(12,13,14);
if (in_array($currentID,$allowedIds)) soundninja_enqueue($hook);
Another option is to pass the the current page/post id as a parameter to the function and do the same if test inside the function, your choice.
I am creating a custom members area for my client's employees. Basically, what I've done so far is I created a new role=consultants and I gave that role a read only access.
Then I uploaded the Peter's Login Redirect Plugin so that the consultants (employees) land in a page called CONSULTANTS PORTAL. From there, they will be able to access their individual page which it will load as long as the name of the page matches the username given to them. That way they can only see their own page.
To see this process, you can visit this link in the wordpress.org forums EASY CLIENT PORTAL
So I've managed a lot of it, except...I am supposed to duplicate the page.php and then add the script that will make the individual page show up. But, the Genesis Framework is pretty complicated. The page.php has an empty skeleton and the actual meat of the page is in a li/structure root folder (That's what I think anyway) .
As it is right now, I have the following in my default template page consultants-portal.php
<?php
/**
* Template Name: Consultants Portal
*/
global $current_user;
get_currentuserinfo();
$page = get_page_by_title($current_user->user_login);
_e($page->post_content);
genesis();
?>
This code gets me this. You can see the content (my page) loading before the page loads. Which tells me there is something else I need to add to this so that the content loads in the actual white area of the page.
The instructions in the link I mentioned says to add the dynamic script right above the is_page or have_posts, but I as I said, Genesis doesn't have this in page.php. instead it is all broken in pieces and spread through the root.
Sorry if I made this too long to read, I wanted you to have all the info I have.
has anyone done this before?
Try out the following code:
<?php
/**
* Template Name: Consultants Portal
*/
// remove Genesis default loop
remove_action( ‘genesis_loop’, ‘genesis_do_loop’ );
// add a custom loop
add_action( ‘genesis_loop’, ‘my_custom_loop’ );
function my_custom_loop () {
// add your queries or custom structure here
global $current_user;
get_currentuserinfo();
$page = get_page_by_title($current_user->user_login);
_e($page->post_content);
}
genesis(); ?>
Instead of writing the code directly, write it inside the loop function as above.
I am developing a module in prestashop. The module has been hooked into left column and right column and its working fine with that. Now I want to show the module output in product footer page. So for that can someone tell me how to hook my module to the product details page? Any help and suggestions will be really appreciable.
My code for leftColumn and rightColumn is like this
function hookLeftColumn()
{
$defaultLanguage = (int)(Configuration::get('PS_LANG_DEFAULT'));
global $cookie, $smarty;
$value=array();
$result="SELECT status,app_id from "._DB_PREFIX_."storeblocks";
$value=Db::getInstance()->ExecuteS($result);
$smarty->assign('array',$value);
$smarty->assign('default',$defaultLanguage);
return $this->display(__FILE__, 'stores.tpl');
}
function hookRightColumn()
{
return $this->hookLeftColumn();
}
For product page, there are several hooks available in PS.
You can use displayLeftColumnProduct hook, which hooks modules just below the image of the product. You can also use displayRightColumnProduct which is for the right side section.
Another set of hooks is displayProductTab and displayProductTabContent , which are used for the tabs on the product page.
If these hooks don't help you, simply, then there are several other ways you can get your results. You can hook your module to any of that hooks which is more suitable for your needs, and then using css position and lft , top etc to move that hook to the required place.
If that is not a choice, then you will need to create your own hook and then using that hook on the product page. Please read this for creating your own hook
http://www.programmingtunes.com/creating-new-prestashop-hook/
Also for a complete list of the hooks in PS, please read this article in PS docs
http://doc.prestashop.com/display/PS15/Hooks+in+PrestaShop+1.5
Let me know if you still need any other details.
Thank you
notice:this answer is about prestashop 1.5 and also you can use it for prestashop 1.6 too.
if your means about product details page is the more info tab as i show in bellow image i can help you
step1.you should instal two hook in install function in your module
public function install() {
return parent::install() &&
$this->registerHook('displayProductTab')&&
$this->registerHook('displayProductTabContent')
}
step2:you should use them and return the some html code
i want add a new tab i use.
notice:id is dynamic you change it but class is static should be use as the selected.
also href is linked to the content id(you see that in step 3)
public function hookDisplayProductTab($params) {
return '<li>
<a id="myid" class="selected"
href="#linked">mytab</a>
</li>'
}
step3:if want when you click on mytab this show the it's content you add an other function.
notice:the href="#linked" at previous function is linked to the id="linked".
public function hookDisplayProductTabContent($params){
return <div id="linked" class="rte">
<div class="product-overview-full">
my contents
</div>
</div>
}
advanced:write your html or smarty in your .tpl and return theme weth this code
return $this->display(__FILE__, 'mytpl.tpl') ;
best regards.
i made a couple of php pages and integrated them into wordpress.
The first page is fine, but the second one show "page not found" on the title when it is loaded.
You can find the first page here:
http://www.stefanovirgulti.it/spese.php
then click on "Aggiungi Negozio" to go to second page.
code of first page:
(suppressed wordpress template code)
//if ( is_user_logged_in() ){
if ( true ){
$index=linkBuilder("Aggiungi Negozio",$_SERVER['PHP_SELF']."?p=1");
$appPath="./moneym/";
//$page=$_GET["p"];
switch ($_GET["p"])
{
case 1:
$page="negozi.php";
break;
default:
echo "this is the first page<br>";
echo $index;
break;
}
if ($page != "") include $appPath.$page;
}
else {
echo "This is a private page.<br>";
}
function linkBuilder($name,$path){
return sprintf("%s ",$path,$name);
}
(suppressed wordpress template code)
The code of the second page contains only an echo.
How do i fix this?
PS: the second page works, but if you check the title page, it says "page not found" and i can't change that, this is my problem.
How did you create these pages? Without looking at your header.php file I'll assume your using some sort of default code to get the page title. To create new pages in wordpress you need to create them in the backend admin panel. if your just loading in files, the wordpress enviroment will see this as a page that doesn't exist.
Solved!
I found the "page custom template" functionality. I just made a template with all my code, then used it as template for a wp static page.
I have done the custom template starting from page.php of my current template, took off the code that handles the content, and replace it with my php/sql stuff.
I have made a new page inside wp and used this custom template.
In this way i have a page that does what i want, but act as a real wp page, i can even add it to menus and apply whatever plugin to it. I left the title funcionality so i can change the title of my cutom page from wp admin.
THe reason for this is that one of your included phps includes a check for wordpress environment and displays that title when the condition is satisfied.
The solution is to use php to output "" tags before the included file is loaded.