Jump to content

PS 1.7 ajax search add image


Recommended Posts

i'm tring to make the ajax search block working on ps 1.7 with no luck. Default installation has a bug on variable on product object (the category are not show on default ajax suggestion in ps 1.7.2 ..... after month of "stable" release).

 

 

This is the default ps_blockserach module variable accessible on frontend in the ajax serach autocomplete for the product object

 

  1. active:"1"
  2. add_to_cart_url:"https://store.sixrace.it/it/carrello?add=1&id_product=184&id_product_attribute=0"
  3. canonical_url:"https://store.sixrace.it/it/cerchioni/184-cerchi-oz-gass-rs-a-in-alluminio-per-panigale-v4-ozgaspaniv4.html"
  4. description_short:"<p>Cerchi OZ GASS RS-A in alluminio per Ducati Panigale V4 monobraccio</p>"
  5. discount_amount:"362,16 €"
  6. discount_percentage:"-15%"
  7. discount_percentage_absolute:"15%"
  8. discount_to_display:"362,16 €"
  9. discount_type:"percentage"
  10. has_discount:true
  11. id_product:"184"
  12. label:undefined
  13. labels:{tax_short: "(Tasse incl.)", tax_long: "Tasse incluse"}
  14. link:"https://store.sixrace.it/it/cerchioni/184-cerchi-oz-gass-rs-a-in-alluminio-per-panigale-v4-ozgaspaniv4.html"
  15. link_rewrite:"cerchi-oz-gass-rs-a-in-alluminio-per-panigale-v4-ozgaspaniv4"
  16. main_variants:[]
  17. manufacturer_name:"OZ"
  18. name:"Cerchi OZ GASS RS-A in alluminio per Panigale V4"
  19. price:"2.052,22 €"
  20. price_amount:2052.22
  21. rate:22
  22. reference:"OZGASPANIV4"
  23. regular_price:"2.414,38 €"
  24. regular_price_amount:2414.38
  25. tax_name:"IVA IT 22%"
  26. unit_price:""
  27. url:"https://store.sixrace.it/it/cerchioni/184-cerchi-oz-gass-rs-a-in-alluminio-per-panigale-v4-ozgaspaniv4.html"
  28. value:undefined

 

in /classes/Search.php we can find a lot of database query and one with ajax

 

if ($ajax) {
            $sql = 'SELECT DISTINCT p.id_product, pl.name pname, cl.name cname,
						cl.link_rewrite crewrite, pl.link_rewrite prewrite '.$score.', pi.id_image pimg
					FROM '._DB_PREFIX_.'product p
					INNER JOIN `'._DB_PREFIX_.'product_lang` pl ON (
						p.`id_product` = pl.`id_product`
						AND pl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('pl').'
					)
					'.Shop::addSqlAssociation('product', 'p').'
					INNER JOIN `'._DB_PREFIX_.'category_lang` cl ON (
						product_shop.`id_category_default` = cl.`id_category`
						AND cl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('cl').'
					)
                                                 INNER JOIN `'._DB_PREFIX_.'image` pi ON (
                        p.`id_product` = pi.`id_product`
                        AND position = 1
                    )
					WHERE p.`id_product` '.$product_pool.'
					ORDER BY position DESC LIMIT 10';
            return $db->executeS($sql, true, false);
        }

 

But this not help me on find WHY category are not passed to the product object and also a way to query the default image link

This is the ps_searchbar.js module file with some modification, the "category_name" is a standard code of the 1.7.2 "stable" release that result in an non exist variable ..

 

/* global $ */
$(document).ready(function () {
    var $searchWidget = $('#search_widget');
    var $searchBox    = $searchWidget.find('input[type=text]');
    var searchURL     = $searchWidget.attr('data-search-controller-url');

    $.widget('prestashop.psBlockSearchAutocomplete', $.ui.autocomplete, {
        _renderItem: function (ul, product) {
                console.log(product);
                return $("<LI>")
/*manufacturer_name
price*/
                    .append($("<A>")
                    .append($("<SPAN>").html(product.image).addClass("image"))
                    .append($("<SPAN>").html(product.category_name).addClass("category"))
                    .append($("<SPAN>").html(product.manufacturer_name))
                    .append($("<SPAN>").html(' > ').addClass("separator"))
                    .append($("<SPAN>").html(product.name).addClass("product"))
                    .append($("<SPAN>").html(' - '))
                    .append($("<SPAN>").html(product.reference))
                    .append($("<SPAN>").html(' ('))
                    .append($("<SPAN>").html(product.price))
                    .append($("<SPAN>").html(')'))
                   ).appendTo(ul)
            ;
        }
    });

    $searchBox.psBlockSearchAutocomplete({
        source: function (query, response) {
            $.post(searchURL, {
                s: query.term,
                resultsPerPage: 10
            }, null, 'json')
            .then(function (resp) {
                response(resp.products);
            })
            .fail(response);
        },
        select: function (event, ui) {
            var url = ui.item.url;
            window.location.href = url;
        },
    });
});

 

I cant find how to add image to the $product object, any help?

Link to comment
Share on other sites

  • 2 weeks later...
  • 1 month later...

Hi,

I have encountered a similar issue before.

Have you tried adding 'category_name' as a value to $allowed_properties array in prepareProductArrayForAjaxReturn method of the ProductListFrontController?

 

/**
 * Cleans the products array with only whitelisted properties.
 *
 * @return array
 */
protected function prepareProductArrayForAjaxReturn(array $products)
{
    $allowed_properties = array('id_product', 'price', 'reference', 'active', 'description', 'category_name', 'description_short', 'link',
        'link_rewrite', 'name', 'manufacturer_name', 'position', 'url', 'canonical_url', 'add_to_cart_url',
        'has_discount', 'discount_type', 'discount_percentage', 'discount_percentage_absolute', 'discount_amount',
        'price_amount', 'regular_price_amount', 'regular_price', 'discount_to_display', 'labels', 'main_variants',
        'unit_price', 'tax_name', 'rate'
    );
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...