Jump to content

How to get product informations by product name


Gramzivi
 Share

Recommended Posts

As far as I know there is no this kind of method in prestashop but You can use this function 

 public function getProductIdByProductName($search, $id_shop = 1, $id_lang = 1)
    {
        return Db::getInstance()->getValue('
        SELECT id_product
        FROM '._DB_PREFIX_.'product_lang
        WHERE name LIKE "%'.(string)$search.'%"
        AND id_shop = '.$id_shop.' AND id_lang = '.$id_lang.'
        ');
    }

and after that in the same module or so use retrieved ID in example like this 

$product = new Product($this->getProductIdByProductName('Your product name'));

Function is returning plain int with id of product that You can use to create new Product object.

 

If You want to have strict match of name delete those % % in LIKE statement and If You using multilanguage and multishop You have to pass context values to get id of shop and language.

Edited by hakeryk2 (see edit history)

Share this post


Link to post
Share on other sites

  • 3 years later...
  • 1 month later...

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
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More