
Carpe_diem
Members-
Posts
72 -
Joined
-
Last visited
Recent Profile Visitors
The recent visitors block is disabled and is not being shown to other users.
Carpe_diem's Achievements
-
Carpe_diem started following Prevent quantity go above stock quantity , SEO Friendly URL not working when switching Language , Dropdown for quantity on cart page and 4 others
-
Hi guys, I am currently working on a custom module and I am having a problem with the SEO friendly URLs. If I go directly to my page recepies mysite.com/si/recepti/sladice/jabolcni-strudel it works fine, but when I change the language with the prestashop default language switcher to lets say italian it displays the italian version of the recipe but my url is not mysite.com/it/recepti/desset/strudel-di-mele but it is mysite.com/it/module/recipes/recipe?category_rewrite=dessert&rewrite=strudel-di-mele Can someone please help me solve this. I am attaching my code: // Here is my routes method in main module file: public function hookModuleRoutes($params) { return [ 'recipes-recipe' => [ 'controller' => 'recipe', 'rule' => 'recepti/{category_rewrite}/{rewrite}', 'keywords' => [ 'category_rewrite' => [ 'regexp' => '[_a-zA-Z0-9-\pL]*', 'param' => 'category_rewrite' ], 'rewrite' => [ 'regexp' => '[_a-zA-Z0-9-\pL]*', 'param' => 'rewrite' ], ], 'params' => [ 'fc' => 'module', 'module' => 'recipes', 'controller' => 'recipe', ], ] } // Here is my frontcontroller code: public $category_rewrite; public $rewrite; public function init() { parent::init(); $this->rewrite = Tools::getValue('rewrite'); $this->category_rewrite = Tools::getValue('category_rewrite'); } public function initContent() { parent::initContent(); $id_lang = $this->context->language->id; $data = Db::getInstance()->getRow(" SELECT n.*, nl.* FROM "._DB_PREFIX_."recipe n INNER JOIN "._DB_PREFIX_."recipe_lang nl ON n.id_recipe = nl.id_recipe AND nl.id_lang = ".(int)$id_lang." WHERE nl.slug = '".pSQL($this->rewrite)."' AND n.active = 1" ); if (!$data) { $original_post = Db::getInstance()->getRow(" SELECT n.id_recipe FROM "._DB_PREFIX_."recipe n INNER JOIN "._DB_PREFIX_."recipe_lang nl ON n.id_recipe = nl.recipe WHERE nl.slug = '".pSQL($this->rewrite)."' AND n.active = 1" ); if ($original_post) { // Get the post in the current language $data = Db::getInstance()->getRow(" SELECT n.*, nl.*,nl.slug as post_slug, kl.slug as category_slug FROM "._DB_PREFIX_."recipe n INNER JOIN "._DB_PREFIX_."recipe_lang nl ON n.id_recipe = nl.id_recipe INNER JOIN "._DB_PREFIX_."recipe_category_lang kl ON n.id_cat = kl.id_cat WHERE n.id_recipe = ".(int)$original_post['id_recipe']." AND nl.id_lang = ".(int)$id_lang." AND kl.id_lang = ".(int)$id_lang." AND n.active = 1" ); if ($data) { Tools::redirect($this->context->link->getModuleLink( 'recipes', 'recipe', ['category_rewrite' => $data['category_slug'], 'rewrite' => $data['post_slug'] ], true, $id_lang )); } } } $alternate_links = []; $languages = Language::getLanguages(true); foreach ($languages as $language) { $post_in_lang = Db::getInstance()->getRow(" SELECT nl.slug as post_slug, kl.slug as category_slug FROM "._DB_PREFIX_."recipe n INNER JOIN "._DB_PREFIX_."recipe_lang nl ON n.id_recipe = nl.id_recipe INNER JOIN "._DB_PREFIX_."recipe_category_lang kl ON kl.id_cat = n.id_cat WHERE nl.id_recipe = ".(int)$data['id_recipe']." AND nl.id_lang = ".(int)$language['id_lang']." AND kl.id_lang = ".(int)$language['id_lang'] ); if ($post_in_lang) { $alternate_links[$language['id_lang']] = [ 'href' => $this->context->link->getModuleLink( 'recipes', 'recipe', ['category_rewrite' => $post_in_lang['category_slug'], 'rewrite' => $post_in_lang['slug']], true, $language['id_lang'] ), 'iso_code' => $language['iso_code'], 'lang_id' => $language['id_lang'], 'name' => $language['name'] ]; } } $this->registerLanguageUrls($alternate_links); } protected function registerLanguageUrls($alternate_links) { $languages = Language::getLanguages(true); $links = []; foreach ($languages as $lang) { if (isset($alternate_links[$lang['id_lang']])) { $links[$lang['id_lang']] = $alternate_links[$lang['id_lang']]['href']; } } $this->context->link->lang_links = $links; }
-
Dropdown for quantity on cart page
Carpe_diem replied to Carpe_diem's topic in Addons, modules and themes developers
thank you it works -
Dropdown for quantity on cart page
Carpe_diem replied to Carpe_diem's topic in Addons, modules and themes developers
I will give it a try, thank you. -
Hi guys, In a project I need to change the input field with a dropdown field on the cart page. My goal is to create a dropdown that updates the quantity depending on the selected value of the dropdown. If a user has 5 items of product A in his cart. He decides he wants 7 so he picks seven in the dropdown and the cart should update to 7. But in the end he wants only 2 items and it needs to change the quantity in the cart to 2. Here is my try. It increases the quantity without a problem, but I can't figure out how to fix decreasing, everything I try it increases by the number you want to decrease. To make it clear, if the quantity is 5 and I change it to 2 it increases the quantity to 8. Here is my whole code: document.addEventListener("DOMContentLoaded", function () { const quantitySelects = document.querySelectorAll(".js-cart-line-product-quantity-select"); quantitySelects.forEach(function (select) { let isUpdating = false; select.addEventListener("change", async function (event) { event.preventDefault(); if (isUpdating) return; const selectedQuantity = parseInt(this.value, 10); const currentQuantity = parseInt(this.dataset.currentQty, 10); // Current quantity in the dataset const productId = this.dataset.productId; const maxQty = parseInt(this.dataset.maxQty, 10); // Max available quantity if (selectedQuantity > maxQty) { alert(`Maximum available quantity is ${maxQty}`); this.value = this.dataset.currentQty; // Revert to previous value return; } const quantityInput = document.querySelector(`input.js-cart-line-product-quantity[data-product-id="${productId}"]`); if (!quantityInput) return; const cartToken = prestashop.static_token; this.disabled = true; isUpdating = true; try { const baseUrl = prestashop.urls.pages.cart || "/cart"; const updateUrl = `${baseUrl}?update=1&id_product=${productId}&token=${cartToken}`; let quantityDifference = selectedQuantity - currentQuantity; const formData = new FormData(); formData.append("ajax", "1"); formData.append("action", "update"); formData.append("id_product", productId); formData.append("id_customization", "0"); formData.append("qty", quantityDifference); // Send the difference (negative for decrease, positive for increase) formData.append("token", cartToken); const response = await fetch(updateUrl, { method: "POST", body: formData, headers: { "X-Requested-With": "XMLHttpRequest", }, }); if (!response.ok) throw new Error("Network response was not ok"); const data = await response.json(); if (data.hasError) { alert(data.errors.join(", ")); this.value = this.dataset.currentQty; } else { quantityInput.value = selectedQuantity; this.dataset.currentQty = selectedQuantity; window.location.reload(); } } catch (error) { console.error("Error:", error); alert("An error occurred while updating the quantity"); this.value = this.dataset.currentQty; } finally { this.disabled = false; isUpdating = false; } }); }); }); Here is my cart-detailed-product-line.tpl <div class="cart-line-product-quantity-wrapper"> <select class="js-cart-line-product-quantity-select form-control" data-product-id="{$product.id_product}" data-current-qty="{$product.quantity}" data-max-qty="{$product.quantity_available}" name="quantity_{$product.id_product}"> {for $qty=$product.minimal_quantity to $product.quantity_available} <option value="{$qty}" {if $qty == $product.quantity}selected="selected" {/if}> {$qty} </option> {/for} </select> <input class="js-cart-line-product-quantity form-control" type="hidden" data-down-url="{$product.down_quantity_url}" data-up-url="{$product.up_quantity_url}" data-update-url="{$product.update_quantity_url}" data-product-id="{$product.id_product}" value="{$product.quantity}" name="product_quantity_{$product.id_product}" min="{$product.minimal_quantity}" /> </div> Can someone please help me out
-
Hi guys, I am currently working on my first project on Prestashop 8. I have installed my custom module that was created for version 1.7. I can insert rows without a problem, but I can't edit rows because the categories. Here is my code that works in version 1.7 but not in version 8. $categories = Category::getCategories($this->context->language->id, true, false, $this->context->shop->id); $filteredCategories = array_filter($categories, function ($category) { return $category['id_category'] != 2; // Exclude the "home" category }); $fields_form = array( array( 'type' => 'categories', 'label' => $this->l('Kategorija'), 'name' => 'id_category', 'tree' => array( 'id' => 'category_tree', 'use_search' => false, 'use_checkbox' => true, 'selected_categories' => !empty($data['id_category']) ? explode(',', $data['id_category']) : [], 'categories' => $filteredCategories, ), 'hidden' => true, ), ), Could someone please help me solve this so it works in version 8.
-
Hi, I am currently working on a project and my client wants that current total discount is displayed on the cart page above the current total price. As I use $cart with print_r or var_dump there is no discount available like $cart.totals.discount. Could someone please give me some help or clue how to set this up
-
Hi guys, I am trying to create a custom product list with a custom .tpl. I need help to display product variants. I add this <div class="product-actions"> {block name='product_buy'} <form action="{$urls.pages.cart}" method="post" id="add-to-cart-or-refresh"> <input type="hidden" name="token" value="{$static_token}"> <input type="hidden" name="id_product" value="{$product.id}" id="product_page_product_id"> <input type="hidden" name="id_customization" value="{$product.id_customization}" id="product_customization_id"> {block name='product_discounts'} {include file='catalog/_partials/product-discounts.tpl'} {/block} {block name='product_variants'} {include file='catalog/_partials/product-variants.tpl'} {/block} {block name='product_add_to_cart'} {include file='catalog/_partials/product-add-to-cart.tpl'} {/block} </form> {/block} </div> but nothing is displayed except for quantity and add to cart. Could someone please help me
-
Hi guys, I am currently working on a custom module for Prestashop 1.7. My goal is to display a product list where the each product has an add to cart button, the products combinations as select list and a textarea that they can add if the want send additional information. So far I can retrieve products (name, description, price, ...) with this: $id_category = 42; $products = Product::getProducts(Context::getContext()->language->id, 0, null, 'id_product', 'ASC', $id_category); I don't have a clue how can I add the products combinations as a select option and add a custom textarea so for customers to send additional information regarding the product. Could someone please help me or guide me to get this working.
-
Hi guys, I have just discovered a problem in my Prestashop 1.7.8.7 on cart page. The problem I am facing is that customers can add quantites above the available stock. Let say Product A has a stock of 5 and the customer can add 10. Prestashop shows a notification alert that a product is not available the quantity that they have entered. It gets even more messy and not understandable when there are mulitple products in the cart with the quantities above the stock and it does not say which product need to be decreased. I tried to fix it with jQuery code that I have added below: $(document).on("input change", ".js-cart-line-product-quantity", function () { updateButtonState($(this)); }); $(window).on("resize", function () { updateButtonState($(".js-cart-line-product-quantity")); location.reload(); }); function updateButtonState($quantityInput) { $quantityInput.each(function () { var maxStockQty = parseFloat($(this).attr("max")); var minStockQty = parseFloat($(this).attr("min")); var currentQty = parseFloat($(this).val()); var $relatedButton = $(this).closest(".js-cart-line").find(".bootstrap-touchspin-up"); var $touchspinContainer = $relatedButton.closest(".bootstrap-touchspin"); // Assuming .bootstrap-touchspin is the container element var $messageContainer = $(this).next(".message-container"); // Use next to select the element after the input $touchspinContainer.find(".custom-span").remove(); if (currentQty >= maxStockQty) { $relatedButton.prop("disabled", true); $touchspinContainer.append("<span class='custom-span'>Quantity exceeds available stock.</span>"); } else if (currentQty <= minStockQty) { $relatedButton.prop("disabled", true); $touchspinContainer.append("<span class='custom-span'>Quantity below minimum allowed.</span>"); } else { $relatedButton.prop("disabled", false); $messageContainer.text(""); } }); } updateButtonState($(".js-cart-line-product-quantity")); it kinda works. It breaks when I quickly change the quantity to a lower number and then back to a higher and it goes above the max number. The AJAX that checks the quantity and calculates the final price and that breaks my code and it doesn't work. How do I solve this problem so customers can't set quantity above the stock available for selected product.
-
Hi guys, I have a question that is back office related in Prestashop 1.7. Is it possible to add a column in the back office order to display how much discount the product had when it was purchased. Now it only shows the reduced price. To be more clear I have added an image to clarify want I am asking for.