TinoArts Posted July 1, 2020 Share Posted July 1, 2020 (edited) Is there a way, or perhaps a module, that adds a "Master price" field to product's BO page? This price should be superior to every other price, including specific prices, discounts, etc. Thank you. Edited July 1, 2020 by TinoArts additional info (see edit history) Link to comment Share on other sites More sharing options...
TinoArts Posted July 6, 2020 Author Share Posted July 6, 2020 I've come up with an ugly, but working solution. Resolved. Link to comment Share on other sites More sharing options...
Guest Posted July 6, 2020 Share Posted July 6, 2020 So share your solution with us. Maybe it will help others. Link to comment Share on other sites More sharing options...
TinoArts Posted July 6, 2020 Author Share Posted July 6, 2020 (edited) No problem, but as I said, this is an ugly solution, no use of controllers, only plain PHP with some ajax workarounds. I've added an <input> field for my 'fixed_price' to pricing.html.twig template. <input type="text" id="fixed_price" name="fixed_price" class="form-control" value="0"> I created a new table 'ps_product_fixed_price' to store my fixed prices. But this can be done by adding a column to 'ps_product' table as well. CREATE TABLE ps_product_fixed_price (id_product INT UNSIGNED NOT NULL, fixed_price DECIMAL, PRIMARY KEY (id_product)) Then, I've added a simple script to retrieve the fixed price and update my custom field value with a simple ajax call (the script should be loaded in Product BO page). $(function() { /* Retrieve fixed price with AJAX */ let id_product = {{ id_product }}; $.ajax({ url: '/themes/my_theme/inc/get-fixed-price.php', data: { id_product: id_product }, type: 'GET', success: function(result) { $('#fixed_price').val(result); } }); }); The content of "get-fixed-price.php" file: <?php include(dirname(__FILE__).'/../../../config/config.inc.php'); include(dirname(__FILE__).'/../../../init.php'); $id_product = $_GET['id_product']; $fixed_price = Db::getInstance()->getValue("SELECT `fixed_price` FROM `ps_product_fixed_price` WHERE `id_product` = " . $id_product); if($fixed_price) { echo $fixed_price; } else { echo 0; } Another script is used to save the inserted value to database with another ajax call. The script is triggered on Presta BO 'Save' product button. It pauses the PS default save events, perform the fixed price update and then finish the default PS events (the script should be loaded in Product BO page). let fixed_price_saved = 0; let default_save_performed = 0; $('body').on("click", '#submit', function(e) { // If default save already performed, change fixed_price_saved back to 0 if(default_save_performed) { fixed_price_saved = 0; default_save_performed = 0; } // Block default save event if fixed price is not saved yet if(!fixed_price_saved) { e.preventDefault(); let id_product = {{ id_product }}; let fixed_price = $('#fixed_price').val(); $.ajax({ url: '/themes/my_theme/inc/set-fixed-price.php', data: { id_product: id_product, fixed_price: fixed_price }, type: 'POST', success: function(result) { console.log(result); } }); // Allow default save event and click the save button again fixed_price_saved = 1; $(this).click(); } else { default_save_performed = 1; } }); The set-fixed-price.php file content is here: <?php include(dirname(__FILE__).'/../../../config/config.inc.php'); include(dirname(__FILE__).'/../../../init.php'); $id_product = $_POST['id_product']; $fixed_price = $_POST['fixed_price']; Db::getInstance()->execute("INSERT INTO `ps_product_fixed_price` (`id_product`, `fixed_price`) VALUES(" . $id_product . "," . $fixed_price . ") ON DUPLICATE KEY UPDATE `fixed_price` = " . $fixed_price); echo "Fixed price successfully stored"; Now I had a working way of loading and storing my custom field value in my custom table/column. Last thing I had to do was to override the default PS price calculation. I did it by adding this piece of code to Product.php. I've added it to the getPriceStatic() method, just before the return statement. // Fixed price $tax_manager = TaxManagerFactory::getManager($address, Product::getIdTaxRulesGroupByIdProduct((int) $id_product, $context)); $product_tax_calculator = $tax_manager->getTaxCalculator(); $fixed_price = Db::getInstance()->getValue("SELECT `fixed_price` FROM `ps_product_fixed_price` WHERE `id_product` = " . $id_product); if($fixed_price) { $fixed_price = $product_tax_calculator->addTaxes($fixed_price); return $fixed_price; } As I said, this is an ugly workaround made by a person that does not work with PS at all. I'm aware it should be done by modifying controllers and classes and I will certainly rebuild it in the near future, but for the moment it is working as expected and I'm happy with it. Edited July 6, 2020 by TinoArts (see edit history) Link to comment Share on other sites More sharing options...
Guest Posted July 6, 2020 Share Posted July 6, 2020 Yes, I understood the solution and by modifying, resp. solving with modules and hooks would make the solution easier. However, watch out for the float in the database. Use decimal instead. You may have a problem with numbers and price conversion. Link to comment Share on other sites More sharing options...
TinoArts Posted July 6, 2020 Author Share Posted July 6, 2020 Noted, thanks. I've edited the query above. Link to comment Share on other sites More sharing options...
Guest Posted July 6, 2020 Share Posted July 6, 2020 It will definitely be better to create a new column in the ps_product table. Then it will be easier to add a fixed price field to the product administration. Next, you would just create your own variable and load your price into it. Link to comment Share on other sites More sharing options...
TinoArts Posted July 6, 2020 Author Share Posted July 6, 2020 My biggest struggle was to actually display the column (field) in the Pricing tab using correct PS approaches. That is why I ended up with this mess. Link to comment Share on other sites More sharing options...
Guest Posted July 6, 2020 Share Posted July 6, 2020 I don't have time now, but you can call me on WhatsApp tomorrow. Are you from Slovakia? Link to comment Share on other sites More sharing options...
TinoArts Posted July 6, 2020 Author Share Posted July 6, 2020 I am, yep. Nut sure about my schedule tomorrow though. Link to comment Share on other sites More sharing options...
Guest Posted July 6, 2020 Share Posted July 6, 2020 Prestashop version ? Link to comment Share on other sites More sharing options...
TinoArts Posted July 6, 2020 Author Share Posted July 6, 2020 1.7.5.0 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now