Magento My custom module block not show - magento

I'm using Magento 1.9.2.2 and I'm trying to create a module which show a product list. Bellow my current code which doesn't render the block. I hope someone can put me in the right direction how to fix this.
app/etc/modules/Envato_All.xml
<?xml version="1.0"?>
<config>
<modules>
<Envato_Recentproducts>
<active>true</active>
<codePool>local</codePool>
</Envato_Recentproducts>
</modules>
</config>
app/code/local/Envato/Recentproducts/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Envato_Recentproducts>
<version>1.0</version>
</Envato_Recentproducts>
</modules>
<global>
<blocks>
<recentproducts>
<class>Envato_Recentproducts_Block</class>
</recentproducts>
</blocks>
<models>
<recentproducts>
<class>Envato_Recentproducts_Model</class>
</recentproducts>
</models>
</global>
</config>
app/code/local/Envato/Recentproducts/Model/Recentproducts.php
<?php
class Envato_Recentproducts_Model_Recentproducts extends Mage_Core_Model_Abstract
{
public function getRecentProduct()
{
$products = Mage::getModel("catalog/product")
->getCollection()
->addAttributeToSelect('*')
->setOrder('entity_id', 'DESC')
->setPageSize(5);
return $products;
}
}
app/code/local/Envato/Recentproducts/Block/Recentproducts.php
<?php
class Envato_Recentproducts_Block_Recentproducts extends Mage_Core_Block_Template
{
public function getRecentProducts()
{
// call model to fetch data
$arr_products = array();
$products = Mage::getModel("recentproducts/recentproducts")­->getRecentProducts();
foreach ($products as $product) {
$arr_products[] = array(
'id' => $product-­>getId(),
'name' => $product­->getName(),
'url' => $product­->getProductUrl(),
);
}
return $arr_products;
}
}
app/design/frontend/default/default/template/recentproducts/recentproducts.phtml
<?php
$products = $this­->getRecentProducts();
?>
<div id="product_list">
<h1>Recent Products</h1>
<?php if (is_array($products) && count($products)) { ?>
<?php foreach($products as $product) { ?>
<div>
<?php echo $product['name'] ?>
</div>
<?php } ?>
<?php } ?>
</div>
Used block
{{block type="recentproducts/recentproducts" name="recentproducts_recentproducts" template="recentproducts/recentproducts.phtml"}}

Have you tried to add "recentproducts/recentproducts" and set to allow under configuration->permission->Blocks?
don't forget to flush your cache after adding this..
Let me know

Related

Magento - Payment Method for each Product

There is the possibility to choose the form of payment for each product?
For example: In my store one product is selling for "cash in game", so by the time the customer is buying this product magento enable only the payment option "in game".
As for the other products that are bought for real money, it disables the payment option "in game" and enable payment for card credit (for exemplo).
I found some modules, but tested and did not work.
Thank's
yes you can do it by this module. for example we want to cash on delivery for some products ->
First create a product attribute (name : 'magepal_payment_filter_by_product', type : yes/no) to identify these products.
E.g base of Magento v1.7 you could
Auto enable payment module programmatically [see][1]
Enable all applicable payment module and filter which module to show where
In /app/code/local/MagePal/PaymentFilterByProduct/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<MagePal_PaymentFilterByProduct>
<version>1.0.1</version>
</MagePal_PaymentFilterByProduct>
</modules>
<global>
<helpers>
<paymentfilterbyproduct>
<class>MagePal_PaymentFilterByProduct_Helper</class>
</paymentfilterbyproduct>
<payment>
<rewrite>
<data>MagePal_PaymentFilterByProduct_Helper_Payment_Data</data>
</rewrite>
</payment>
</helpers>
</global>
</config>
In /app/code/local/MagePal/PaymentFilterByProduct/Helper/Payment/Data.php
<?php
class MagePal_PaymentFilterByProduct_Helper_Payment_Data extends Mage_Payment_Helper_Data
{
public function getStoreMethods($store = null, $quote = null)
{
$methods = parent::getStoreMethods($store, $quote);
if(!Mage::getStoreConfig('paymentfilterbyproduct/general_option/paymentfilterbyproduct_enable', Mage::app()->getStore()) || !$quote){
return $methods;
}
//get a list of product in cart
$cart = Mage::getSingleton('checkout/session');
$specialProductInCart = array();
foreach ($cart->getQuote()->getAllItems() as $item) {
$specialProductInCart[] = $item->getMagepalPaymentFilterByProduct();
}
// if special product in cart
// need to if check $item->getMagepalPaymentFilterByProduct() return 'yes/no' or '0/1)
if(in_array('yes', $specialProductInCart)){
$filter_csv = Mage::getStoreConfig('paymentfilterbyproduct/filter_option/paymentfilter_special_products', Mage::app()->getStore());
}
else{
$filter_csv = Mage::getStoreConfig('paymentfilterbyproduct/filter_option/paymentfilter_all_product', Mage::app()->getStore());
}
$filter_array = explode(",", $filter_csv);
foreach ($methods as $k => $method){
if(!in_array($method->getCode(), $filter_array))
unset($methods[$k]);
}//methods
return $methods;
}
}
In /app/code/local/MagePal/PaymentFilterByProduct/etc/system.xml
<?xml version="1.0"?>
<config>
<tabs>
<magepal translate="label" module="paymentfilterbyproduct">
<label>MagePal</label>
<sort_order>900</sort_order>
</magepal>
</tabs>
<sections>
<paymentfilterbyproduct translate="label" module="paymentfilterbyproduct">
<label>Payment Method Filter by Product</label>
<tab>magepal</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>
<general_option translate="label">
<label>General Options</label>
<frontend_type>text</frontend_type>
<sort_order>1</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<fields>
<paymentfilter_enable translate="label">
<label>Enable Payment Filter</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_yesno</source_model>
<sort_order>50</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</paymentfilter_enable>
</fields>
</general_option>
<filter_option translate="label">
<label>Payment Method Filter Configuration</label>
<frontend_type>text</frontend_type>
<sort_order>2</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<comment>Please enable all applicable payment methods in system payment config</comment>
<fields>
<paymentfilter_all_products translate="label">
<label>Select Default Payment option for All Products</label>
<frontend_type>multiselect</frontend_type>
<source_model>MagePal_PaymentFilterByProduct_ActivePaymentMethod</source_model>
<sort_order>30</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</paymentfilter_admin>
<paymentfilter_special_products translate="label">
<label>Select Payments for Cart with Special Products</label>
<frontend_type>multiselect</frontend_type>
<source_model>MagePal_PaymentFilterByProduct_ActivePaymentMethod</source_model>
<sort_order>40</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</paymentfilter_store>
</fields>
</filter_option>
</groups>
</paymentfilterbyproduct>
</sections>
</config>
In /app/code/local/MagePal/PaymentFilterByProduct/Helper/Data.php
<?php
class MagePal_PaymentFilterByProduct_Helper_Data extends Mage_Core_Block_Template
{
}
In /app/code/local/MagePal/PaymentFilterByProduct/ActivePaymentMethod.php
<?php
class MagePal_PaymentFilterByProduct_ActivePaymentMethod
{
//get all active (enable) payment method
public function toOptionArray()
{
$payments = Mage::getSingleton('payment/config')->getActiveMethods();
foreach ($payments as $paymentCode=>$paymentModel) {
if($paymentModel->canUseCheckout() == 1){
$paymentTitle = Mage::getStoreConfig('payment/'.$paymentCode.'/title');
$methods[$paymentCode] = array(
'label' => $paymentTitle,
'value' => $paymentCode,
);
}
}
return $methods;
}
}
In /app/etc/modules/MagePal_PaymentFilterByProduct.xml
<?xml version="1.0"?>
<config>
<modules>
<MagePal_PaymentFilterByProduct>
<active>true</active>
<codePool>local</codePool>
</MagePal_PaymentFilterByProduct>
</modules>
</config>

Get specific category level

How can I get a specific category level from Magento, my category setup looks like this now.
root_catalog
|-Shop
|-Shoes
|-T-shirts
|-Brands
|-Nike
|-Womens
|-Mens
|-Adidas
|-Asics
<?php if( $category = Mage::getModel('catalog/category')->load( $categories[1]) ): ?>
<?php echo $category->getName(); ?>
<?php endif ?>
When calling $category->getName(); I would like to only display the Brand Name, is that possible?
You can get category level from
$category = Mage::getModel('catalog/category')->load( $categories[1]) )->getLevel()
and then check with your brand name category level, if match then display name.
e.g. suppose brand category level is 3
<?php if( $category = Mage::getModel('catalog/category')->load( $categories[1]) ): ?>
<?php if($category->getLevel() == 3)
echo $category->getName(); ?>
<?php endif ?>
<?php endif ?>
ANKIT's answer is good, but it could be improved by actually query-ing the specific levels instead of loading the whole collection and doing a conditional. Take for example if you want to get all categories in a specific level:
<ul>
<?php $categories = Mage::getModel('catalog/category')
->getCollection()
// magic is prepared here..
->addAttributeToSelect('*')
// then the magic happens here:
->addAttributeToFilter('level', array('eq'=>2))
->load();
foreach($categories as $category):
?>
<li>$category->getName()</li>
<?php endforeach; ?>
</ul>
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');
$categories = $product->getCategoryIds(); /*will return category ids array*/
foreach($categories as $category){
$cat = $objectManager->create('Magento\Catalog\Model\Category')->load($category);
if ($cat->getLevel() == 2) {
$catName = $cat->getName().","; ?>
<div class="brand_bg">
<label><?php /* #escapeNotVerified */ echo __('Category :') ?></label>
<?php echo $catName; ?>
</div>
<?php } ?>
<?php } ?>
?>

Codeigniter: Not inserting data in table

Update: It is solved ..
For some reason, the data is not getting inserted into the table. It is however being posted from the form as I could see with var dump, but further than that, won't do. So, here are the 3 modules. It is a very simple test scheme: Just a form with two fields, you press Submit and should be inserted. (I can do all that in ordinary PHP with one page, but, the MVC frameworks are a nightmare in this regard, you write about 30 times more code than you would need in procedural.
<?php
class Inserting_controller extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('Inserting_model');
}
public function index ()
{
$this->load->view('inserting_view');
}
// Controller
public function insert()
{
$data = array(
'username' => $this->input->post('username', TRUE),
'password' => sha1($this->input->post('password', TRUE)
));
var_dump($data); // We do get the data posted
exit;
$this->Inserting_model->insertdata($data); // this should forward them to the Model
}
}
?>
==============
MODEL
<?php
class Inserting_model extends CI_Model{
function __construct()
{
// Call the Model constructor
parent::__construct();
$this->load->database();
}
public function insertdata($data)
{
$this->db->insert('users', $data);
}
}
?>
========
VIEW
<div id="inserting_form">
<?php echo form_open('index.php/Inserting_controller/insert/'); ?>
<ul>
<li>
<label>Username</label>
<div><?php echo form_input(array('id' => 'username', 'name' => 'username')); ?></div>
</li>
<li>
<label>Password</label>
<div><?php echo form_password(array('id' => 'password', 'name' => 'password')); ?></div>
</li>
<li><?php echo validation_errors();?></li>
<li><?php echo form_submit(array('name' =>'submit'),'Insert');?> </li>
</ul>
<?php echo form_close(); ?>
</div>
Blushing :/
On writing the debugging code, I forgot to delete the exit; thus, the program exited right after that ....

Displaying Specific Magento Categories From Category ID

As of yet, I haven't managed to find anything online that already caters for what I'm trying to achieve. I simply want to call in specific categories to a list but I want to be able to define which categories by ID, so for example, I would like to be able to call them in like something such as the below:-
{{block type="catalog/navigation" name="catalog.category" template="developer/extension/script.phtml" ids="3,6,17,143,57"}}
I'm already displaying a list of sub categories in various places based on the parent category ID but in instances where there are hundreds of sub categories, it isn't always practical to display all of them, so I'm wondering if the existing script can possibly be tweaked to only include specific categories as per above?
<?php
//gets all sub categories of parent category 'cat-id-4'
$cats = Mage::getModel('catalog/category')->load(4)->getChildren();
$catIds = explode(',',$cats);
$categories = array();
foreach($catIds as $catId) {
$category = Mage::getModel('catalog/category')->load($catId);
$categories[$category->getName()] = array(
'url' => $category->getUrl(),
'img' => $category->getImageUrl()
);
}
ksort($categories, SORT_STRING);
?>
<ul>
<?php if($category->getIsActive()): ?>
<?php foreach($categories as $name => $data): ?>
<li>
<?php echo $name; ?>
</li>
<?php endforeach; ?>
<?php endif; ?>
</ul>
If anyone could advise how I could possibly achieve this please, that would be fantastic - Thanks in advance.
This should work with your given CMS block code:
<?php
$catIds = explode(',', $this->getIds()); //<-- ONLY CHANGE MADE
$categories = array();
foreach($catIds as $catId) {
$category = Mage::getModel('catalog/category')->load($catId);
$categories[$category->getName()] = array(
'url' => $category->getUrl(),
'img' => $category->getImageUrl()
);
}
ksort($categories, SORT_STRING);
?>
<ul>
<?php if($category->getIsActive()): ?>
<?php foreach($categories as $name => $data): ?>
<li>
<?php echo $name; ?>
</li>
<?php endforeach; ?>
<?php endif; ?>
</ul>

How to create new fields for customer

I am developing a website with magento ver-1.6. I am try to create new fields for customer registration, but it not created. I followed the same way what we followed in ver-1.5.
Any variation in create customer fields in 1.6?
I don't know what you tried so I'm just going to list all the steps needed to add a new schooL customer attribute to the Magento 1.6.1 registration form.
Create a module preferably, or place similiar code to this in some .phtml file and run it once. If you're doing this proper and creating a module, put code like this into the mysql_install file:
<?php
$installer = $this;
$installer->startSetup();
$setup = Mage::getModel('customer/entity_setup', 'core_setup');
$setup->addAttribute('customer', 'school', array(
'type' => 'int',
'input' => 'select',
'label' => 'School',
'global' => 1,
'visible' => 1,
'required' => 0,
'user_defined' => 1,
'default' => '0',
'visible_on_front' => 1,
'source'=> 'profile/entity_school',
));
if (version_compare(Mage::getVersion(), '1.6.0', '<='))
{
$customer = Mage::getModel('customer/customer');
$attrSetId = $customer->getResource()->getEntityType()->getDefaultAttributeSetId();
$setup->addAttributeToSet('customer', $attrSetId, 'General', 'school');
}
if (version_compare(Mage::getVersion(), '1.4.2', '>='))
{
Mage::getSingleton('eav/config')
->getAttribute('customer', 'school')
->setData('used_in_forms', array('adminhtml_customer','customer_account_create','customer_account_edit','checkout_register'))
->save();
}
$installer->endSetup();
?>
In your module config.xml file. Note that the name of my module is Excellence_Profile.
<profile_setup> <!-- Replace with your module name -->
<setup>
<module>Excellence_Profile</module> <!-- Replace with your module name -->
<class>Mage_Customer_Model_Entity_Setup</class>
</setup>
</profile_setup>
Here we will add our attribute, to the customer registration form. In version 1.6.0(+) the phtml file used is persistance/customer/register.phtml and in version 1.6.0(-) the phtml file used is customer/form/register.phtml
So we need to open the phtml file, based on magento version and add this code in the tag.
<li>
<?php
$attribute = Mage::getModel('eav/config')->getAttribute('customer','school');
?>
<label for="school" class="<?php if($attribute->getIsRequired() == true){?>required<?php } ?>"><?php if($attribute->getIsRequired() == true){?><em>*</em><?php } ?><?php echo $this->__('School') ?></label>
<div class="input-box">
<select name="school" id="school" class="<?php if($attribute->getIsRequired() == true){?>required-entry<?php } ?>">
<?php
$options = $attribute->getSource()->getAllOptions();
foreach($options as $option){
?>
<option value='<?php echo $option['value']?>' <?php if($this->getFormData()->getSchool() == $option['value']){ echo 'selected="selected"';}?>><?php echo $this->__($option['label'])?></option>
<?php } ?>
</select>
</div>
</li>
For magento 1.4.2(+) that is all that is required for the registration step. If you create a user from here, you should see the school text field in admin.
For magento 1.4.1(-), we need to do another thing open the your modules config.xml file and add:
<global>
<fieldsets>
<customer_account>
<school><create>1</create><update>1</update><name>1</name></school>
</customer_account>
</fieldsets>
</global>
Once, user has created his account in the MyAccount->Account Information section he should be able to edit the school field as well. For this open the phtml file customer/form/edit.phtml and put in the code in the :
<?php
<li>
<?php
$attribute = Mage::getModel('eav/config')->getAttribute('customer','school');
?>
<label for="is_active" class="<?php if($attribute->getIsRequired() == true){?>required<?php } ?>"><?php if($attribute->getIsRequired() == true){?><em>*</em><?php } ?><?php echo $this->__('School') ?></label>
<div class="input-box">
<select name="school" id="school" class="<?php if($attribute->getIsRequired() == true){?>required-entry<?php } ?>">
<?php
$options = $attribute->getSource()->getAllOptions();
foreach($options as $option){
?>
<option value='<?php echo $option['value']?>' <?php if($this->getCustomer()->getSchool() == $option['value']){ echo 'selected="selected"';}?>><?php echo $this->__($option['label'])?></option>
<?php } ?>
</select>
</div>
</li>
A registration form also shows up at the checkout page in magento. To add you field here, you need to edit checkout/onepage/billing.phtml for magento version 1.6(-) and persistant/checkout/onepage/billing.phtml for magento version 1.6(+) file and then find the code:
<?php if(!$this->isCustomerLoggedIn()): ?>
inside this if condition add your field
<li>
<li>
<?php
$attribute = Mage::getModel('eav/config')->getAttribute('customer','school');
?>
<label for="school" class="<?php if($attribute->getIsRequired() == true){?>required<?php } ?>"><?php if($attribute->getIsRequired() == true){?><em>*</em><?php } ?><?php echo $this->__('School') ?></label>
<div class="input-box">
<select name="billing[school]" id="school" class="<?php if($attribute->getIsRequired() == true){?>required-entry<?php } ?>">
<?php
$options = $attribute->getSource()->getAllOptions();
foreach($options as $option){
?>
<option value='<?php echo $option['value']?>'><?php echo $this->__($option['label'])?></option>
<?php } ?>
</select>
</div>
</li>
Next open your module config.xml or any other config.xml file, add the following lines:
<global>
<fieldsets>
<checkout_onepage_quote>
<customer_school>
<to_customer>school</to_customer>
</customer_school>
</checkout_onepage_quote>
<customer_account>
<school>
<to_quote>customer_school</to_quote>
</school>
</customer_account>
</fieldsets>
</global>
Next we need to make some changes in the quote table i.e sales_flat_quote table in magento. If you have a module then create an upgrade version of your sql file and put in this code:
$tablequote = $this->getTable('sales/quote');
$installer->run("
ALTER TABLE $tablequote ADD `customer_school` INT NOT NULL
");
After doing this make sure to clear you magento cache, specifically “Flush Magento Cache” and “Flush Cache Storage”.
Now when you place order, the customer is created with the correct school attribute.
I had problems to save the new fields in the checkout_register form.
I had to extend the global->fieldsets node:
<global>
<fieldsets>
<checkout_onepage_quote>
<customer_school>
<to_customer>school</to_customer>
</customer_school>
</checkout_onepage_quote>
<checkout_onepage_billing>
<school>
<to_customer>*</to_customer>
</school>
</checkout_onepage_billing>
<customer_account>
<school>
<to_quote>customer_school</to_quote>
</school>
</customer_account>
<sales_convert_order>
<customer_school>
<to_quote>*</to_quote>
</customer_school>
</sales_convert_order>
</fieldsets>
</global>

Resources