I want to make Cart order list empty after a new product has been added to the cart.
In fact only on product every time can be in cart.
Tanx
2 ways to add your custom logic :
create your own module and hook it on "actionCartSave"
override "add" and "update" methods in "Cart" class (/override/classes/Cartp.php)
Edit : The 2nd way os wrong because infinite update loop.
Here is a module that do it :
class OneProductCart extends Module {
public function __construct() {
$this->name = 'oneproductcart';
$this->tab = 'front_office_features';
$this->version = '1.0';
$this->author = 'SJousse';
$this->need_instance = 0;
parent::__construct();
$this->displayName = $this->l('One Product Cart');
$this->description = $this->l('Keep only last product in cart.');
}
public function install() {
return (parent::install() && $this->registerHook('actionCartSave'));
}
public function hookActionCartSave($params) {
$cart = $params['cart'];
$last = $cart->getLastProduct();
$prods = $cart->getProducts();
foreach ($prods as $prod)
if ($prod['id_product'] != $last['id_product'])
$cart->deleteProduct($prod['id_product']);
}
}
For people using Prestashop v 1.4.9 and have created a module:
call global $smarty, $cart;
then run the function $cart->delete();
function hookHome($params)
{
global $smarty, $cart;
/** some code here **/
$cart->delete();
}
Related
I was able to create a menu tab in the back office but when I click on it, I get
Page not found.
The controller is missing or invalid.
Here's the code for my controller -
<?php
class AdminModuleNameConvert extends ModuleAdminController {
public function __construct() {
$this->bootstrap = true;
parent::__construct();
}
}
Using the solution provided by ethercreation, I get the controller to load, but it shows me
Try width :
In your module : modulenameconverter
class modulenameconverter extends Module
{
public function __construct(Context $context = null)
{
$this->name = 'modulenameconverter';
$this->version = '1';
$this->bootstrap = true;
$this->author = 'Stackoverflow';
$this->displayName = $this->l('modulenameconverter');
$this->description = $this->l('Module name converter');
parent::__construct();
}
public function install()
{
$tab = new Tab();
$tab->class_name = 'Adminmodulenameconverter';
$tab->module = 'modulenameconverter';
$tab->name[1] = 'modulenameconverter';
$tab->id_parent = 2;
$tab->active = 1;
if (!$tab->save()) {
return false;
}
return parent::install();
}
public function uninstall()
{
$id_tab = (int)Tab::getIdFromClassName('Adminmodulenameconverter');
$tab = new Tab($id_tab);
if (Validate::isLoadedObject($tab)) {
if (!$tab->delete()) {
return false;
}
} else {
return false;
}
return parent::uninstall();
}
}
In module/controllers/admin/AdminModulenameconverterController.php
class AdminNameconverterController extends ModuleAdminController
{
public function __construct()
{
parent::__construct();
$this->bootstrap = true;
$this->id_lang = $this->context->language->id;
$this->default_form_language = $this->context->language->id;
}
public function initContent()
{
parent::initContent();
}
}
I was having the exact same issue and it appears that I was not configuring the tab parameters correctly... I was trying to pass an array for the "$this->module" parameter so Prestashop could not find the module because in the database, the module linked to the tab was equal to ""...
So my best advice in this case is to always check your database fields to see if they are correctly filled.
To conclude : It's always the silliest issues that makes the biggest headaches... -_-'
I have a problem with Prestashop 1.7.x.
I create a basic module and I registered for the first time to DisplayLeftColumn then to displayHome and then back to DisplayLeftColumn.
I can install the module with no errors, and the position it's set ok in the backoffice, but not show anything.
Thanks in advance!
if(!defined('_PS_VERSION_'))
exit;
class homephoto extends Module{
public function __construct()
{
$this->name = 'homephoto';
$this->tab = 'front_office_features';
$this->version = '1.0';
$this->author = 'PlusPlusDesign';
$this->ps_version_compilancy = array('min'=>'1.5', 'max' => _PS_VERSION_);
$this->need_instance = 0;
$this->bootstrap = true;
$this->displayName = $this->l('home photo');
$this->description = $this->l('This is for the mainpage');
parent::__construct();
}
public function install(){
if(!parent::install() or !$this->registerHook('displayLeftColumn'))
return false;
return true;
}
public function displayLeftColumn($params){
return 'Hello World';
}
}
Change your hook function:
public function hookDisplayLeftColumn($params){
return 'Hello World';
}
Here is the list of available hooks, with information to know where he is called, and what he does : https://devdocs.prestashop.com/1.7/modules/concepts/hooks/list-of-hooks/
Then in the module add "hook" in front to be able to call it as a function and so it can be called.
Ex : In your code : "displayLeftColumn" and in your module function : hookDisplayLeftColumn
Regards
You have to add hook to your function to be called. See the example below :
public function hookDisplayLeftColumn($params){
return 'Hello World';
}
How to add custom input field in Product image form using hooks.I am trying to add a new check box in product image form but i do not know how to create it by using modules as well as i am not able to override core product page template.I am createing directory structure inside themes/classic/module/module_name/.....
If some can write the main module php file of Prestashop 1.7 I am very thankful to you.
<?php
if (!defined('_PS_VERSION_')) {
exit;
}
class youmodule extends Module
{
protected $config_form = false;
public function __construct()
{
$this->name = 'youmodule';
$this->tab = 'administration';
$this->version = '1.0.0';
$this->need_instance = 0;
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('youmodule');
$this->description = $this->l('youmodule');
$this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
}
public function install()
{
return parent::install() &&
$this->registerHook('header') &&
$this->registerHook('displayAdminProductsExtra');
}
public function uninstall()
{
return parent::uninstall();
}
public function hookDisplayAdminProductsExtra($params)
{
$id_product = Tools::getValue('id_product');
//YOURCODE
$this->smarty->assign(array(
'yourvariable' => $yourvariabl
));
return $this->display(__FILE__, '/views/templates/admin/product.tpl');
}
}
I m trying to load product class in a custom made module so that I run the following:
$customized_datas = Product::getAllCustomizedDatas((int)$order->id_cart);
My module structure is as follows:
class autoWebspace extends Module {
public function __construct() {
$this->name = 'autowebspace';
$this->tab = 'front_office_features';
$this->version = '1.0';
$this->author = 'dimitris';
$this->need_instance = 0;
parent::__construct();
$this->displayName = $this->l('webspace creation in plesk');
$this->description = $this->l('Creates webspace in plesk after purchase');
}
public function install() {
return (parent::install() && $this->registerHook('actionValidateOrder') && $this->registerHook('displayHeader'));
}
public function hookActionValidateOrder($params) {
$customized_datas = Product::getAllCustomizedDatas((int)$order->id_cart);
}
how can I do that?
Like that:
public function hookActionValidateOrder($params) {
$customized_datas = Product::getAllCustomizedDatas((int)$params['order']->id_cart);
}
Or you can use $params['cart']->id instead.
I am new to prestashop. I wanted learn how the code will flow .
ex. how product are inserting in database ?
how product data is fetching and displaying in store page ?
please help me .
you should open PrestaShop Product class.
you can get all product data using this:
$product = new Product($id_product);
if add a new product:
$product = new Product();
write all parameters like:
$product->name = 'test';
$product->reference = '35GH';
$product->save();
If u print out product array you will see all parameters. GOod luck, have fun. And even lot of information in stackoverflow how to php add product and etc.
If you want to work on products like you want to add products,delete products,want some customization then first you have to create a new module. For that you have to create a new folder in that location with you module name
C:\wamp\www\prestashop\modules\your_module_name
Then after you have to create a file named your_module_name.php in that folder
C:\wamp\www\prestashop\modules\your_module_name\your_module_name.php
After then you have to code in that file like:
your_module_name.php
class your_module_name extends Module
{
public function __construct()
{
$this->name = 'your_module_name';
$this->tab = 'front_office_features';
$this->version = '1.0.0';
$this->author = 'abc';
$this->need_instance = 0;
//$this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('Your Module Name');
$this->description = $this->l('This is the module for facilitate user to purchase particular product.');
$this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
if (!Configuration::get('your_module_name')) {
$this->warning = $this->l('No name provided');
}
}
public function install()
{
if (Shop::isFeatureActive()) {
Shop::setContext(Shop::CONTEXT_ALL);
}
if (!parent::install())
{
return false;
}
$defaultsettings = $this->getDefaultSettings();
$defaultsettings = serialize($defaultsettings);
Configuration::updateValue('your_module_name', $defaultsettings);
return true;
}
public function uninstall()
{
if (!parent::uninstall()) {
return false;
}
return true;
}
public function getContent()
{
$output = null;
$product = new Product($id_product);
$product->name = 'test';
$product->reference = '35GH';
$product->save();
}
Now in that getContent method you can perform operations on products.I hope it would help you.