Magento

Magento design, technology, customizations, modules and training.
  • Magento: Add checkout/cart_sidebar block programmatically in custom controller

    There are many ways to create a block within Magento and there is very little documentation to compare the methods of adding one programmatically through a Magento controller vs creating one via Magento's XML structure.

    Quick Tip: Actions in layout xml configs are just a block method call.

    In following example I show how to call "checkout/cart_sidebar" and the different equivalents of xml vs a controller

    <block type     = "checkout/cart_sidebar"
           name     = "cart_sidebar"
           as       = "cartExplorer"
           template = "checkout/cart/sidebar.phtml"
           before   = "-">
    <action method="addItemRender">
        <type>configurable</type>
        <block>checkout/cart_item_renderer_configurable</block>
        <template>checkout/cart/sidebar/default.phtml</template>
    </action>
    
    
    <!-- Programmatically create the block -->
    <?php
        $this->getLayout()
             ->createBlock('checkout/cart_sidebar', 'cart_sidebar')
             ->setTemplate('checkout/cart/sidebar.phtml');
             ->addItemRender(
                 'configurable',
                 'checkout/cart_item_renderer_configurable',
                 'checkout/cart/sidebar/default.phtml'
             )
    
    ?>
    
    
    <!-- This is if it was already created in a layout.xml file -->
    <?php
    
    $this->getLayout()
         ->getBlock('cart_sidebar')
         ->addItemRender(
             'configurable',
             'checkout/cart_item_renderer_configurable',
             'checkout/cart/sidebar/default.phtml'
         )
    
    ?>
    Hope this helps!!

  • Magento: Show subcategories with images and products on category page.

    The question was asked:

    How do I sort products on a category page by subcategory as well as limit the number of products from each subcategory:

    For example if the category was Food I would want to display the following:

    Drinks:  Coke 12oz, Orange Juice 8oz, Milk Gallon,

    Pasta:  Spaghetti 1lb, Pesto 12 pc, Tortellini 1 PC.

    And so on, displaying each subcategory name followed 3 products (images etc.)

    There are multiple locations you could use the following code, however, you have to have access to the current active category for this to work.

    <!-- Finding Current Category and Finding it's children -->
    <?php
    $category = Mage::getSingleton('catalog/layer')->getCurrentCategory();
    $categories = $category->getCollection()
            ->addAttributeToSelect(array('name', 'thumbnail'))
            ->addAttributeToFilter('is_active', 1)
            ->addIdFilter($category->getChildren())
    ?>
    <div class="subcategories">
        <p>Select a category to view products:</p>
        <ul class="clearfix">
        <!-- Display Each Subcategory Image and Name  -->
    
        <?php foreach ($categories as $category): ?>
            <li class="grid12-3">
                <a href="<?php echo $category->getUrl() ?>" class="clearfix">
                    <?php if($thumbFile = $category->getThumbnail()): ?>
                    <img src="<?php echo Mage::getBaseUrl('media') . 'catalog' . DS . 'category' . DS . $thumbFile;?>" alt="<?php echo $this->htmlEscape($category->getName()) ?>" />
                    <?php endif;?>
                    <span><?php echo $category->getName() ?></span></a>
            </li>
            <!-- Load (3) Products from within each subcategory  -->
            <?php
            $_helper = $this->helper('catalog/output');
            $products = Mage::getModel('catalog/product')
                ->getCollection()
                ->addCategoryFilter($category)
                ->setPageSize(3)
                ->addAttributeToSelect(array('name', 'product_url', 'small_image'))
                ->load();
            ?>
            <!-- Display Each product's detailed info  -->
            <?php foreach ($products as $product): ?>
                <li>
                <?php // Product Image ?>
                <a href="<?php echo $product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($product, 'small_image'), null, true) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($product, 'small_image')->resize(135); ?>" width="135" height="135" alt="<?php echo $this->stripTags($this->getImageLabel($product, 'small_image'), null, true) ?>" /></a>
                <?php // Product description ?>
                <?php $_productNameStripped = $this->stripTags($product->getName(), null, true); ?>
                <h2 class="product-name"><a href="<?php echo $product->getProductUrl() ?>" title="<?php echo $productNameStripped; ?>"><?php echo $_helper->productAttribute($product, $product->getName() , 'name'); ?></a></h2>
                </li>
            <?php endforeach ; ?>
    
        <?php endforeach; ?>
        </ul>
    </div>
    I commented within the code what I am doing within each step. Good luck, hope this helps!!

  • Move Magento mini search form to my template's header or footer

    Below is a quick beginners guide to moving the search bar to your desired location within Magento. To begin, you first you need to create or update your local.xml file if you do not have a local.xml file you can create one in app->frontend->[Package Name]->[Theme Name]->layout->local.xml Once this is created you can copy exactly what I have in this post into that file for a start of how to use one. DO ALL UPDATES THROUGH A LOCAL.XML file not through catalog.xml !! This will make upgrades significantly easier later down the road. Additionally, you will be able to quickly see all changes you have made to your site in one file. The below example will add it to the root reference name which will be available on all pages but easily called in template->page->1column.phtml or 2column-left.phtml 3column.phtml etc.

    <?xml version="1.0" encoding="UTF-8"?>
    <layout>
        <default>
            <reference name="root">
                <block type="core/template" name="top.search" as="topSearch" template="catalogsearch/form.mini.phtml"/>
            </reference>
        </default>
    </layout>
    Then call it in your current theme's header.phtml by adding this line of code
    <?php echo $this->getChildHtml(‘topSearch’) ?>
    If this file does not exist in your theme, create a copy of: app/design/frontend/base/default/template/page/html/header.phtml and place it into: app/design/frontend/yourPackage/yourTheme/template/page/html/header.phtml Now you can use the "reference name" and "as" name like the above section. For example you could use the similar setup below to reference the footer block to add the search feature. For education "as" names are what are used in .phtml files. and "name" is how a block is referenced within xml files. So in the above example. I am adding the search field to the root content area and then calling it in my .phtml file with the "as" name of "topSearch"
    <?xml version="1.0" encoding="UTF-8"?>
        <layout>
        <default>
            <reference name="footer">
                <block type="core/template" name="footer.search" as="footerSearch" template="catalogsearch/form.mini.phtml"/>
            </reference>
        </default>
    </layout>
    Then call it in footer.phtml with
    <?php echo $this->getChildHtml('footerSearch') ?>
      Hopefully this helps some of you new to Magento, happy coding and good luck!!

  • Need a Magento Upgrade?

    Is your E-commerce store stuck in an older version of Magento Community Edition?

    As of May 15, 2014 the current version of Magento Community Edition is 1.9.0.1.

    Depending what version you are using, there are steps you may have to take get your site back up to the latest version. For instance, in cases of much older versions, you'll need to upgrade your shop to 1.7.0.2 version first.

    At Swarming Technology (based in Milwaukee, WI) we are experts in helping new and existing clients upgrade their versions of Magento. We strive to find cost effective ways to move older versions to the latest.

    A few things to factor in the cost of your upgrade:

    1. Was my site set-up correctly by my developer?
    2. Were the core files tampered with?
    3. Are the modules and extensions my site uses compatible with newer versions?

    Contact Swarming Technology for an estimate today!

    We are Magento Certified and can also provide you optimized Magento hosting to get your sites performance at top speed.

  • How to change a product dropdown attribute to a multiselect in Magento

    First, update the attribute input type to multiselect:

    UPDATE eav_attribute SET
    entity_type_id = '4',
    attribute_model = NULL,
    backend_model = 'eav/entity_attribute_backend_array',
    backend_type = 'varchar',
    backend_table = NULL,
    frontend_model = NULL,
    frontend_input = 'multiselect',
    frontend_class = NULL
    WHERE attribute_id = 'YOUR_ATTRIBUTE_ID_HERE';
    Next, copy the attribute values from the old table to the new:
    INSERT INTO catalog_product_entity_varchar ( entity_type_id, attribute_id, store_id, entity_id, value)
    SELECT entity_type_id, attribute_id, store_id, entity_id, value
    FROM catalog_product_entity_int
    WHERE attribute_id = YOUR_ATTRIBUTE_ID_HERE;
    Finally,  remove the old values or they will conflict with the new setup (the old values will load, but Magento will save new values to the varchar table):
    DELETE FROM catalog_product_entity_int
    WHERE entity_type_id = 4 and attribute_id = YOUR_ATTRIBUTE_ID_HERE;
    Credit to Meabed's post for the initial concept: http://www.meabed.net/2012/10/18/convert-attribute-drop-down-to-multiselect-magento.html Update regarding entity_type_id value referenced in the above example: The value for "entity_type_id" that you'll want to use may not always be "4" as referenced above. You'll want to check the table "eav_entity_type" and find the entry that has the "entity_type_code" set to "catalog_product". The "entity_type_id" for that record is the value you need to use. In most cases this value is either "4" or "10", but this may vary in future versions of Magento. Thanks to David Line for the heads up and providing these instructions.

  • Setting Default Product Values in Magento

    When creating new products in the Magento Admin, there are a few fields that you often fill out with the same values over and over again. Values such as the product status, tax class, stock availability, weight, etc. Wouldn't it be nice to have the system automatically assign default values to these fields every time you create a new product? Well good news! You can easily accomplish this by hooking into Magento's catalog_product_new_action event. I'm not going to cover how to create new modules in Magento, but here is the basic structure and files you'll need to accomplish this. Make sure to provide your own value anywhere Namespace is referenced below. /app/code/local/Namespace/Catalog/etc/config.xml /app/code/local/Namespace/Catalog/Model/Product/Observer.php

    1) Place the following in the config.xml file listed above.

    <?xml version="1.0"?>
    <config>
    	<modules>
            <Namespace_Catalog>
                <version>1.0.0</version>
            </Namespace_Catalog>
        </modules>
    
    	<global>
    
            <models>
    			<namespace_catalog>
    	             <class>Namespace_Catalog_Model</class>
    	        </namespace_catalog>
    		</models>
    
            <events>
    			<catalog_product_new_action>
    	        	<observers>
    		          <namespace_catalog_product_observer>
    		            <type>singleton</type>
    		            <class>namespace_catalog/product_observer</class>
    		            <method>catalog_product_new_action</method>
    		          </namespace_catalog_product_observer>
    		        </observers>
    			</catalog_product_new_action>
    		</events>
    
    	</global>
    </config>

    2) Place the following in the Observer.php file. Set product default values as desired.

    <?php
    
    class Namespace_Catalog_Model_Product_Observer
    {
    	public function catalog_product_new_action($observer) {
    
    		$product = $observer->getEvent()->getProduct();
    		$product->setStatus(1);
    		$product->setTaxClassId(2);
    
    		$stockItem = Mage::getModel('cataloginventory/stock_item');
    		$stockItem->assignProduct($product);
    		$stockItem->setData('is_in_stock', 1);
    		$stockItem->setData('qty', 1);
    
    		$product->setStockItem($stockItem);
    	}
    }

    3) Finally, you'll need to activate your new module by creating the file below with the following contents.

    /app/etc/modules/Namespace_All.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <config>
    	<modules>
    		<Namespace_Catalog>
    			<active>true</active>
    			<codePool>local</codePool>
    		</Namespace_Catalog>
    	</modules>
    </config>
    Make sure to clear the Magento cache when you're done with this step as well! Check out the Magento wiki article on using Event/Observer for more information on this topic.

  • Using console.log() in Magento

    Using the JavaScript Console to do basic debugging is very useful. Unfortunately, Magento hijacks the console object, rendering console.log() useless. There are a number of ways to get around this, but I've found this snippet to be particularly successful in restoring the console:

    var i = document.createElement('iframe');
    i.style.display = 'none';
    document.body.appendChild(i);
    window.console = i.contentWindow.console;

  • Magento - Making the telephone field not required at checkout

    Out of the box, the Magento checkout process requires users to enter a telephone number for both billing and shipping addresses. If your business doesn't have this requirement, you may want to remove this validation from one or both address forms. The steps below will remove the telephone requirement on the shipping address form during checkout. This has been tested with Magento Community Edition 1.7.0.1.

    Step 1

    The first step is to modify the one page checkout templates for the address form(s) in which you want to remove the telephone requirement. The shipping address form is located here: /app/design/frontend/base/default/template/checkout/onepage/shipping.phtml DO NOT modify the base template file directly. Instead, copy this file into your site's own theme and make your modifications there. Base template files can be overwritten during Magento upgrades, so if you modify these files directly, you'll probably loose all of your work! Open your copy of shipping.phtml and look for the line:

    <label for="shipping:telephone" class="required"><?php echo $this->__('Telephone') ?></label>
    Replace it with (remove required class):
    <label for="shipping:telephone"><?php echo $this->__('Telephone') ?></label>
    Next, look for the line:
    <input type="text" name="shipping[telephone]" value="<?php echo $this->escapeHtml($this->getAddress()->getTelephone()) ?>" title="<?php echo $this->__('Telephone') ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('telephone') ?>" id="shipping:telephone" onchange="shipping.setSameAsBilling(false);" />
    And replace it with (comment out echo):
    <input type="text" name="shipping[telephone]" value="<?php echo $this->escapeHtml($this->getAddress()->getTelephone()) ?>" title="<?php echo $this->__('Telephone') ?>" class="input-text <?php //echo $this->helper('customer/address')->getAttributeValidationClass('telephone') ?>" id="shipping:telephone" onchange="shipping.setSameAsBilling(false);" />
    That's it. Save your new template file.

    Step 2

    Next, we need to modify the Magento Customer Address class to comment out some validation code. The code is located in an abstract class, so we can't override it. Instead, we'll make a copy of the file in the /app/code/local, which Magento will find and use instead of the core file. Copy the file: /app/code/core/Mage/Customer/Model/Address/Abstract.php To: /app/code/local/Mage/Customer/Model/Address/Abstract.php NOTE: You'll likely have to create the proper folder under /app/code/local. Now, open /app/code/local/Mage/Customer/Model/Address/Abstract.php and on line 375 find the following code:

    if (!Zend_Validate::is($this->getTelephone(), 'NotEmpty')) {
            $errors[] = Mage::helper('customer')->__('Please enter the telephone number.');
    }
    Replace the above code with (comment out the entire block):
    /*
            // Disable telephone field validation
            if (!Zend_Validate::is($this->getTelephone(), 'NotEmpty')) {
                $errors[] = Mage::helper('customer')->__('Please enter the telephone number.');
            }
    */
    Save your modified Abstract.php file and proceed to the next step.

    Step 3

    Some of Magento's field validation logic is determined by the configuration of EAV attribute values, so we'll need to update a few rows in the Magento database. You'll need to make two small edits:

    1. Open the table eav_attribute and search for the row with attribute_code = "telephone". Make note of the attribute_id of this row. Next, set the column is_required to 0 (zero).
    2. Now, open the table customer_eav_attribute and search for the row with the same attribute_id as you noted in the previous step. Set the column validation_rules on this row to NULL.

    Step 4

    Finally, you'll need to clear all of Magento's caches so that your changes take effect. In the Magento admin, got to System -> Cache Management, and clear all caches. Make sure to click the 'Flush Magento Cache' and 'Flush Cache Storage' buttons as well. This will force Magento to take note of your database changes.

    Wrap Up

    That's it! When entering a new shipping address at checkout, Magento should no longer require a value for telephone. If you have any suggestions for improving these steps, please leave a comment below.

  • Multi-Store Magento Platforms from Swarming Technology (in Milwaukee, WI)

    NEED TO MANAGE MULTIPLE ONLINE STORES? Magento is an extremely powerful e-commerce platform for businesses operating stores for multiple clients, customer groups, or departments. Ask us about our multi-store Magento solutions for: • Promotional Products Companies • Multi-Channel Distributors • Dealer or Distibutor Networks Call Swarming Technology today and we’ll guide you through the entire process. (888) 855-8632

  • Magento Enterprise Development in Milwaukee Wisconsin!

    Magento Enterprise Development | Swarming Technology - Milwaukee Wi MAGENTO ENTERPRISE DEVELOPMENT Looking to launch a store running on Magento Enterprise Edition? Look no further. Unlike most Magento Enterprise developers, we have years of experience developing and running stores using Magento Enterprise Edition. From start to finish, we'll be there to do the installations and following through till you and your staff are trained. Call Swarming Technology today and we’ll guide you through the entire process. (888) 855-8632

Items 1 to 10 of 11 total

Page:
  1. 1
  2. 2