Subject:
Add remaining stock in product page.
When stock is available, replace “in stock” with the remaining stock on product page. In BO, we can enable or disable this function. If this function is disabled, keep the default display of Magento.
Note: There is 2 steps to finish this exercise. 1. Add a new boolean setting in System->Configuration to control if we should open or close this attribute. 2. Looking for the template which shows the stock, add a new controll according to the settings in BO.
========================================================================================
First, we will finishe the setting in BO。
1) Read the article of Alanstorm - Custom Magento System Configuration and Assume that we have create the new module "Hellointershop".
2) Adding a new tab 2.1 Add system.xml file
- Location: app/code/local/Kingsun/Hellointershop/etc/system.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <config>
- <tabs>
- <intershopconfig translate="label" module="hellointershop">
- <label>Intershop Extensions</label>
- <sort_order>1000</sort_order>
- </intershopconfig>
- </tabs>
- <sections>
- <intershop_options translate="label" module="hellointershop">
- <label>Intershop Options</label>
- <tab>intershopconfig</tab>
- <sort_order>1000</sort_order>
- <show_in_default>1</show_in_default>
- <show_in_website>1</show_in_website>
- <show_in_store>1</show_in_store>
- <groups>
- <intershop_group translate="label" module="hellointershop">
- <label>My Intershop Options</label>
- <frontend_type>text</frontend_type>
- <sort_order>1000</sort_order>
- <show_in_default>1</show_in_default>
- <show_in_website>1</show_in_website>
- <show_in_store>1</show_in_store>
- <fields>
- <intershop_input translate="label">
- <label>My Input Field: </label>
- <comment>My Comment</comment>
- <frontend_type>text</frontend_type>
- <sort_order>20</sort_order>
- <show_in_default>1</show_in_default>
- <show_in_website>1</show_in_website>
- <show_in_store>1</show_in_store>
- </intershop_input>
- <intershop_select translate="label">
- <label>My Dropdown: </label>
- <comment>Source model provider Magento's default Yes/No values</comment>
- <frontend_type>select</frontend_type>
- <sort_order>90</sort_order>
- <show_in_default>1</show_in_default>
- <show_in_website>1</show_in_website>
- <show_in_store>1</show_in_store>
- <source_model>adminhtml/system_config_source_yesno</source_model>
- </intershop_select>
- <intershop_select_myself translate="label">
- <label>My Dropdown (defined by myself): </label>
- <comment>Source model defined by myself</comment>
- <frontend_type>select</frontend_type>
- <frontend_class>free-method</frontend_class>
- <sort_order>100</sort_order>
- <show_in_default>1</show_in_default>
- <show_in_website>1</show_in_website>
- <show_in_store>1</show_in_store>
- <source_model>hellointershop/words</source_model>
- </intershop_select_myself>
- </fields>
- </intershop_group>
- </groups>
- </intershop_options>
- </sections>
- </config>
2.2 Modify the config.xml file.
- Location: app/code/local/Kingsun/Hellointershop/etc/config.xml
- <config>
- <modules>
- <Kingsun_Hellointershop>
- <version>0.1.0</version>
- </Kingsun_Hellointershop>
- </modules>
- <frontend>
- <routers>
- <hellointershop>
- <use>standard</use>
- <args>
- <module>Kingsun_Hellointershop</module>
- <frontName>hellointershop</frontName>
- </args>
- </hellointershop>
- </routers>
- </frontend>
- <global>
- <models>
- <hellointershop>
- <class>Kingsun_Hellointershop_Model</class>
- </hellointershop>
- </models>
- <helpers>
- <hellointershop>
- <class>Kingsun_Hellointershop_Helper</class>
- </hellointershop>
- </helpers>
- </global>
- <adminhtml>
- <acl>
- <resources>
- <!-- <all>
- <title>Allow Everything</title>
- </all>-->
- <admin>
- <children>
- <system>
- <children>
- <config>
- <children>
- <intershop_options>
- <title>Intershop - All</title>
- </intershop_options>
- </children>
- </config>
- </children>
- </system>
- </children>
- </admin>
- </resources>
- </acl>
- </adminhtml>
- </config>
2.3) Add Data.php file
- Location : app/code/local/Kingsun/Hellointershop/Helper/Data.php
- <?php
- class Kingsun_Hellointershop_Helper_Data extends Mage_Core_Helper_Abstract
- {
- }
2.4) Add a Words.php file.
- Location : app/code/local/Kingsun/Hellointershop/Model/Words.php
- <?php
- class Kingsun_Hellointershop_Model_Words
- {
- public function toOptionArray()
- {
- return array(
- array('value'=>1, 'label'=>Mage::helper('helloworld')->__('Hello')),
- array('value'=>2, 'label'=>Mage::helper('helloworld')->__('Goodbye')),
- array('value'=>3, 'label'=>Mage::helper('helloworld')->__('Yes')),
- array('value'=>4, 'label'=>Mage::helper('helloworld')->__('No')),
- );
- }
- }
3. Open the debug in BO, we set "Template path hints" and "Add block name to hints" to true. Refrsh the page of FO. We can see the template which displayes the stock is app/design/frontend/base/default/template/catalog/product/view/type/default.phtml. Copy the file to your folder. In the new file, we use
- Mage::getStoreConfig('intershop_options/intershop_group/intershop_select')
to get the choice you made in BO. We use
- Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty()
to get the stock of the product. The total code will look like
- <?php if(Mage::getStoreConfig('intershop_options/intershop_group/intershop_select')){?>
- <p class="availability in-stock"><?php echo $this->__('Availability:') ?> <span><?php echo Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty(); ?></span></p>
- <?php }else{?>
- <p class="availability in-stock"><?php echo $this->__('Availability:') ?> <span><?php echo $this->__('In stock') ?></span></p>
- <?php }?>