For this tutorial we will use the [eccube directory]/admin/product page as an example. Our objective is to display the # of stocks available in the product master page as shown below.
We will focus on these 4 files for this tutorial:
This file contains the basic setters and getters for the table columns.
The model initializes the functions to be used for displaying and editing the values of the Product table.
The controller retrieves the values needed from the Model and renders it to the view (twig).
The view is the main frontend interface displayed where the users of the site can interact with.
Since we would like to display the value of the Stocks column from the database, we'll have to make sure that the setter and getter are initialized. Stocks is a built in feature in Ec-cube's Product Management so by default, it should be initialized. Look for the codes below and check if they exist in the ProductClass.php file.
/** * @var string */private $stock;/** * Set stock * * @param string $stock * @return ProductClass */ public function setStock($stock) { $this->stock = $stock; return $this; } /** * Get stock * * @return string */ public function getStock() { return $this->stock; }Now that we already confirmed that the setter and getter for $stock are existing, we'll need to find/create the function in Product.php that returns the value of the $stock. We'll name the functions as
Again, by default these functions should be existing in your Ec-cube source files; if not then just copy the codes below.
private $stocks = array();public function _calc() { ... ... // stock $this->stocks[] = $ProductClass->getStock(); ... ...}/** * Get Stock min * * @return integer */public function getStockMin() { $this->_calc(); return min($this->stocks);}/** * Get Stock max * * @return integer */public function getStockMax(){ $this->_calc(); return max($this->stocks);}Check which twig/view is being rendered by finding the ->render() function. In this case it should show you the codes below.
In this case, we can see that the view rendered in this page is Product/index.twig .
return $app->render('Product/index.twig', array( 'searchForm' => $searchForm->createView(), 'pagination' => $pagination, 'disps' => $disps, 'pageMaxis' => $pageMaxis, 'page_no' => $page_no, 'page_status' => $page_status, 'page_count' => $page_count, 'active' => $active,));On step 2 we find/created a function named getStockMax() and getStockMin(). To call and display its values we'll have to use:
Use these 2 codes in the location where you want the stock value to show.
<span id="result_list__code--{{ Product.id }}"> {{ Product.code_min }} {% if Product.code_min != Product.code_max %} ~ {{ Product.code_max }} <br/> {% endif %} <span>Stocks: {{ Product.stock_max }}</span> </span>