Jump to content

Display Product list (with attributes and stock) on a custom page


cyanoxide

Recommended Posts

I've got a custom controller set up that extends from the front controller, it's fairly basic and currently contains the following:

class StockController extends FrontController{
 
    public function init(){
      parent::init();

    }

    public function initContent(){
		$this->display_column_left = false;
		$this->display_column_right = false;
      	parent::initContent();
      	$this->setTemplate(_PS_THEME_DIR_.'stockcontroller.tpl');    
      
    }
}

This is all working correctly as it stands but I would like to use  Product::getproducts(); so I can display a full list of products available and their stock levels. 

 

After looking at all the various posts on here regarding the topic I was assuming I could add the following into init or init content but it just gives me a fatal error message.

$products = Product::getProducts($this->context->language->id, 0, 100);
$products_all = Product::getProductsProperties($this->context->language->id, $products);
$this->context->smarty->assign(array(
        'products' => $products_all
));

What am I missing?

Link to comment
Share on other sites

Checking the PHP error log gives me the following:

 

[17-Feb-2017 11:05:58 Europe/London] PHP Warning:  Missing argument 5 for ProductCore::getProducts(), called in /var/www/webroot/ROOT/controllers/front/stocklistController.php on line 22 and defined in /var/www/webroot/ROOT/classes/Product.php on line 1190

 

Edited by cyanoxide (see edit history)
Link to comment
Share on other sites

Hello,

You missed the required order_way parameter.

Cf. public static function getProducts($id_lang, $start, $limit, $order_by, $order_way, $id_category = false,$only_active = false, Context $context = null)

Thank you for this observation, I've added in parameters for both $order_by and $order_way and I'm no longer seeing the "fatal error" message I was receiving but I'm still not seeing the product array on my display page. Using {$products|@print_r} in the .tpl file all I'm seeing is "Array ( ) 1" so I'm assuming something is still missing.

Link to comment
Share on other sites

  • 2 weeks later...

Maybe that your id_lang (required parameter for getProducts function) is not defined.

$id_lang = $this->context->language->id;
$products = Product::getProducts($id_lang, 0, 100 ... ...);

var_dump($id_lang) // Returns ?

If false, before your init function, insert :

public function __construct()
{
    parent::__construct();

    $this->context = Context::getContext();
}
Link to comment
Share on other sites

Okay, I've tried doing that and var_dump($id_lang) displays "null" on my page so I've added the code and the "null" changed to "int(1)" Testing the product list at this point throught the tpl still just displays "Array () 1". 

 

Here is the code I've currently got based on your suggestions.

<?php

class MotorsportController extends FrontController{

 	public function __construct(){
    	parent::__construct();
		$this->context = Context::getContext();
	}
	public function init(){
      	parent::init();
    }
    public function initContent(){
		$this->display_column_left = false;
		$this->display_column_right = false;
      	parent::initContent();
      	$this->setTemplate(_PS_THEME_DIR_.'motorsport.tpl');   
      	$id_lang = $this->context->language->id;
     	$products = Product::getProducts($id_lang, 0, 1000, 'asc', 'asc');
      
       var_dump($id_lang);  
      
		$products_all = Product::getProductsProperties($id_lang, $products);


     	 $this->context->smarty->assign(array(
        	'products' => $products_all
		));
      
    }
}


And I'm trying to call it on the tpl file with (I've tried it without the "@" also with no change.

{$products|@print_r}
Link to comment
Share on other sites

Now that your var $id_lang is ok, test your var $products.

Warning, one of your parameter is not correct.

$order_by should be 'id_product' or 'price' or 'date_add' or 'date_upd' BUT NOT 'asc'

var_dump($id_lang); // Should return 1 or 2 and so.

$products = Product::getProducts($id_lang, 0, 1000, 'id_product', 'asc', false, false, false, true, null);
// Required parameters $id_lang, $start (here 0), $limit (here 1000), $order_by (here 'id_product'), $order_way ('here asc')

var_dump($products); // Should return an array of products
Edited by Yopixel (see edit history)
Link to comment
Share on other sites

Okay thank you, progress has been made.

 

I'm now seeing the array of of product properties. The bad news is that this array doesn't contain the attributes or the quantity values for them. I've now got access to the default attribute id and quantity and the quantity of all attributes combined but not the other two attributes.

Link to comment
Share on other sites

I stripped out the personal information and set the range to just the first product in the list.

 

array(1) { [0]=> array(80) { ["id_product"]=> string(2) "54" ["id_supplier"]=> string(1) "2" ["id_manufacturer"]=> string(1) "0" ["id_category_default"]=> string(2) "15" ["id_shop_default"]=> string(1) "1" ["id_tax_rules_group"]=> string(2) "65" ["on_sale"]=> string(1) "0" ["online_only"]=> string(1) "0" ["ean13"]=> string(13) "5060460900017" ["upc"]=> string(0) "" ["ecotax"]=> string(8) "0.000000" ["quantity"]=> int(22) ["minimal_quantity"]=> string(1) "1" ["price"]=> float(195) ["wholesale_price"]=> string(8) "0.000000" ["unity"]=> string(0) "" ["unit_price_ratio"]=> string(8) "0.000000" ["additional_shipping_cost"]=> string(4) "0.00" ["reference"]=> string(7) "ref1" ["supplier_reference"]=> string(0) "" ["location"]=> string(0) "" ["width"]=> string(8) "0.000000" ["height"]=> string(8) "0.000000" ["depth"]=> string(8) "0.000000" ["weight"]=> string(8) "1.000000" ["out_of_stock"]=> string(1) "2" ["quantity_discount"]=> string(1) "0" ["customizable"]=> string(1) "0" ["uploadable_files"]=> string(1) "0" ["text_fields"]=> string(1) "0" ["active"]=> string(1) "1" ["redirect_type"]=> string(3) "404" ["id_product_redirected"]=> string(1) "0" ["available_for_order"]=> string(1) "1" ["available_date"]=> string(10) "0000-00-00" ["condition"]=> string(3) "new" ["show_price"]=> string(1) "1" ["indexed"]=> string(1) "1" ["visibility"]=> string(4) "both" ["cache_is_pack"]=> string(1) "0" ["cache_has_attachments"]=> string(1) "0" ["is_virtual"]=> string(1) "0" ["cache_default_attribute"]=> string(3) "280" ["date_add"]=> string(19) "2015-10-23 13:58:44" ["date_upd"]=> string(19) "2017-03-07 09:49:54" ["advanced_stock_management"]=> string(1) "0" ["pack_stock_type"]=> string(1) "3" ["id_shop"]=> string(1) "1" ["id_lang"]=> string(1) "1" ["description"]=> string(1132) "Lorem ipsem dolor set amet." ["description_short"]=> string(189) "Lorem ipsem dolor set amet." ["link_rewrite"]=> string(9) "product1" ["meta_description"]=> string(0) "" ["meta_keywords"]=> string(0) "" ["meta_title"]=> string(26) "Product Metaname" ["name"]=> string(9) "Product Name" ["available_now"]=> string(0) "" ["available_later"]=> string(27) "AVAILABILITY: ON BACK ORDER" ["manufacturer_name"]=> NULL ["supplier_name"]=> string(7) "Supplier" ["rate"]=> float(20) ["tax_name"]=> string(10) "VAT UK 20%" ["id_product_attribute"]=> string(3) "280" ["allow_oosp"]=> int(0) ["category"]=> string(12) "category" ["link"]=> string(52) "https://www.example.com/category/product.html" ["attribute_price"]=> float(0) ["price_tax_exc"]=> float(162.5) ["price_without_reduction"]=> float(195) ["reduction"]=> float(0) ["specific_prices"]=> array(19) { ["id_specific_price"]=> string(4) "3154" ["id_specific_price_rule"]=> string(1) "0" ["id_cart"]=> string(1) "0" ["id_product"]=> string(2) "54" ["id_shop"]=> string(1) "0" ["id_shop_group"]=> string(1) "0" ["id_currency"]=> string(1) "8" ["id_country"]=> string(1) "0" ["id_group"]=> string(1) "0" ["id_customer"]=> string(1) "0" ["id_product_attribute"]=> string(1) "0" ["price"]=> string(10) "162.500000" ["from_quantity"]=> string(1) "1" ["reduction"]=> string(8) "0.000000" ["reduction_tax"]=> string(1) "1" ["reduction_type"]=> string(6) "amount" ["from"]=> string(19) "0000-00-00 00:00:00" ["to"]=> string(19) "0000-00-00 00:00:00" ["score"]=> string(2) "40" } ["quantity_all_versions"]=> int(36) ["id_image"]=> string(10) "en-default"  ["attachments"]=> array(0) { } ["virtual"]=> int(0) ["pack"]=> int(0) ["packItems"]=> array(0) { } ["nopackprice"]=> int(0) ["customization_required"]=> bool(false) } }

Link to comment
Share on other sites

Hey again, I've tried adding that in but it doesn't seem to be having any effect. I can grab the attribute id for each product (The original one I was testing uses an attr id of 280) using 

$attr_id = $product_all[0][id_product_attribute];

or the same without the "[0]" in a foreach loop which I currently have but passing those variables over to getAttributeCombinationsById just gives me blank arrays. I also tried passing "280" directly into the method but that also didn't work so I must be missing something here.

 

This is what I've got now.

      	$id_lang = $this->context->language->id;
     	$products = Product::getProducts($id_lang, 0, 5, 'id_product', 'asc');
		$products_all = Product::getProductsProperties($id_lang, $products);
      foreach ($products_all as $product_all) {
        $attr_id = $product_all[id_product_attribute];
      	$product_attr = Product::getAttributeCombinationsById($attr_id, $id_lang);
        var_dump($product_attr);
      }
      	

Thank you for all your help with this so far. It's proving to be a lot more difficult than I first expected but it is giving me some excellent insight on how prestashop works.

Link to comment
Share on other sites

  • 5 months later...

Hey again, I've tried adding that in but it doesn't seem to be having any effect. I can grab the attribute id for each product (The original one I was testing uses an attr id of 280) using 

$attr_id = $product_all[0][id_product_attribute];

or the same without the "[0]" in a foreach loop which I currently have but passing those variables over to getAttributeCombinationsById just gives me blank arrays. I also tried passing "280" directly into the method but that also didn't work so I must be missing something here.

 

This is what I've got now.

      	$id_lang = $this->context->language->id;
     	$products = Product::getProducts($id_lang, 0, 5, 'id_product', 'asc');
		$products_all = Product::getProductsProperties($id_lang, $products);
      foreach ($products_all as $product_all) {
        $attr_id = $product_all[id_product_attribute];
      	$product_attr = Product::getAttributeCombinationsById($attr_id, $id_lang);
        var_dump($product_attr);
      }
      	

Thank you for all your help with this so far. It's proving to be a lot more difficult than I first expected but it is giving me some excellent insight on how prestashop works.

 

 

You want to achieve something like this?  :)

https://www.tinybibiya.com/stocklist

Link to comment
Share on other sites

  • 7 months 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
×
×
  • Create New...