Reputation: 63
I'm trying to modify the homeslider module in prestashop so it can show the latest products .
I created this method which return an array of the last new products :
protected function getNewProducts($nbr){
$newProducts = false;
if (Configuration::get('PS_NB_DAYS_NEW_PRODUCT'))
$newProducts = Product::getNewProducts((int) $this->context->language->id, 0, $nbr);
if (!$newProducts)
return;
return $newProducts;
}
and I want to extract from this array of product the informations i need such as it's name, description ...etc
so I changed the installSamples method :
protected function installSamples()
{
$languages = Language::getLanguages(false);
for ($i = 1; $i <= 3; ++$i)
{
$slide = new HomeSlide();
$slide->position = $i;
$slide->active = 1;
$product_set = $this->getNewProducts(3); // get the list of the last 3 products
foreach ($languages as $language)
{
$tmp=array_values($product_set);
$slide->title[$language['id_lang']] = $tmp[0].name;
$slide->description[$language['id_lang']] = '<h2>LOLILOL</h2>
<p>Test1</p>
<p><button class="btn btn-default" type="button">Shop now !</button></p>';
$slide->legend[$language['id_lang']] = 'sample-'.$i;
$slide->url[$language['id_lang']] = 'http://www.prestashop.com/?utm_source=back-office&utm_medium=v16_homeslider'
.'&utm_campaign=back-office-'.Tools::strtoupper($this->context->language->iso_code)
.'&utm_content='.(defined('_PS_HOST_MODE_') ? 'ondemand' : 'download');
$slide->image[$language['id_lang']] = 'sample-'.$i.'.jpg';
}
$slide->add();
}
}
I tried to change the Slide Title, but Instead of getting the name of the product, I get : ArrayName
Same thing for description, instead of getting the Description of the product, I get : ArrayDescription
Thanks !
Upvotes: 0
Views: 5654
Reputation: 167
To get all fields from a product use:
$product = new Product($id_product);
var_dump ($product->name);
/*
Resulut is an array with available languages:
["name"]=>
array(3) {
[1]=> string(23) "PHP book"
[2]=> string(23) "PHP Buch"
[3]=> string(23) "PHP livre"
}
*/
This provides all information for all languages. In this case name,description,... is an array.
To get a specific language you need the language ID:
$langID = $this->context->language->id;
$product = new Product($id_product, false, $langID);
var_dump ($product->name);
/*
Now the output is a string with the product name by the current language.
["name"]=> string(23) "PHP book"
*/
Upvotes: 2
Reputation: 16
prestashop save text information on the basis on language id, so name and description field return as array.
You can get the name of product by passing language id in array and you will get name and description.
You will get language id by following code:
$lang_id = $this->context->language->id;
Upvotes: 0