Ecco il codice corretto e completo ::
class methods extends Module {
function __construct()
{
$this->tab = 'export';
$this->version = '1.0';
parent::__construct();
$this->page = basename(__FILE__, '.php');
$this->description = $this->l('Export Google Shoping xml format for products');
$this->need_instance = 0;
$this->ps_versions_compliancy = '1.6';
$this->uri = ToolsCore::getCurrentUrlProtocolPrefix() .$this->context->shop->domain_ssl.$this->context->shop->physical_uri;
$this->array = array();
}
//Ritorna i parent di una categoria
//@param int $id_category_default
//@return string $defcat_name
public function recurse($id_category){
$a = CategoryCore::getChildren($id_category,1);
if(empty($a))
return;
foreach ($a as $key => $value) {
$this->array[] = $value['id_category'];
$this->recurse($value['id_category'], $this->array);
}
return $this->array;
}
//Restituisce le categorie da stampare di un prodotto
public function getCategory($id_product, $id_lang) {
$product = new Product($id_product);
$category_no_child = array();
$method = new methods(); //Se non faccio così e uso il $this mi ritorna l'errore "Using $this when not in object context"
$categorys = $product->getCategories();
foreach($categorys as $key => $id_category){
if (empty(CategoryCore::getChildren($id_category, $id_lang)))
array_push($category_no_child, $id_category);
}
foreach($categorys as $key => $id_category) {
$count = 0;
$all_child = $method->recurse($id_category); //Qua dovrei usare $this
if (!empty ($all_child)) {
foreach ($all_child as $item => $child) {
if (in_array($child, $categorys, true)) {
$count++;
}
}
if ($count == 0) {
$category_no_child[] = $id_category;
}
}
}
return $category_no_child;
}