Monday 28 November 2011

Add Date field with Datepicker in Magento Contact Form

Magento has a built in library for calendar functionalities. However the calendar library is available only on Admin pages like Custom Module, Customer Account view page, etc. We can extend the prototype Javascript library's calendar functions to frontend.

Edit the page.xml file in your current theme in the directory app/design/frontend/default/your-theme/layout/page.xml Around line 61 add the below lines in between

 
js_csscalendar/calendar-win2k-1.css
jscalendar/calendar.js
jscalendar/calendar-setup.js

This will include the calendar library functions in the page head of all front end pages.

Now add Date Field to your contact form

Next step would be to add Javascript code at bottom of contact form template file. The id of the date input field and calendar image should be the same. The javascript code shown below should be added below the contact form coding.


Sunday 27 November 2011

Magento Product Question

You wish to give users the option to quickly ask a question about a particular product then this simple extension will do the trick. This will present the user with a contact form with the product in question liked to at the top. You can download extension from here

Install the extension in the normal manner. To add the question you will need to edit your product page. Add the following where you wish to code to appear.

/app/design/frontend/default/your_theme/template/catalog/product/view.phtml

<?php if(Mage::getStoreConfigFlag('productquestion/productquestion/enabled')):
$url = $this->helper("productquestion")->getAddUrl($_product);
?>
        <?php echo $this->__('Ask a question about this product') ?>
 <?php endif; ?>

Tuesday 15 November 2011

Magento: Getting product attributes values and labels

I have found that it is very useful to be able to get attributes from the system and use them in places other than a products category page. This is how to get a drop down lists options. I don't think it will work for a mulit-select attribute. I stick the value/label pairs into an array to use how I please.
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'attribute_id');

foreach ( $attribute->getSource()->getAllOptions(true, true) as $option){
 $attributeArray[$option['value']] = $option['label'];
}
I had a trickier time getting values for a multi-select attribute. I don't think that this is the best method, but it is one that worked for me. First the multi-select attribute must be set to be used in the advanced search. You can set this in the manage attributes area.
$attributes = Mage::getModel('catalogsearch/advanced')->getAttributes();
$attributeArray=array();

foreach($attributes as $a){
 if($a->getAttributeCode() == 'desired_attribute_code'){
  foreach($a->getSource()->getAllOptions(false) as $option){
   $attributeArray[$option['value']] = $option['label'];
  }
 }
}
Here is a better way to get the multi-select attribute values.
$attributeId = Mage::getResourceModel('eav/entity_attribute')
->getIdByCode('catalog_product','attribute_code_here');

$attribute = Mage::getModel('catalog/resource_eav_attribute')
->load($attributeId);

$attributeOptions = $attribute ->getSource()->getAllOptions();
If you only need to retrieve a value for a product you can try this way
//Referenced from /app/code/core/Mage/Eav/Model/Config/php @ line 443
$_product->getResource()->getAttribute('club_type')
->getFrontend()->getValue($_product)
The code below will get an attribure collection. Set {entityType} to 4 to get attributes for products. You can remove the "setCodeFilters" line if you want to get all the attributes. To get anything really useful you will probably need to get the resulting attribute ids and do someting with them, like use them in a filter for the products collection or something.
$attributesInfo = Mage::getResourceModel('eav/entity_attribute_collection')
->setEntityTypeFilter({entityType})
->setCodeFilter($attributes)
->addSetInfo()
->getData();

Delete an attribute associated with a configurable product?

Go to table catalog_product_super_attribute and search with the condition
product_id= < "id of your configurable product">.
This will list all the attributes associated with this particular configurable product. Find the record with the corresponding attribute_id and delete it.

Monday 14 November 2011

Magento – Get URL Paths for Skin, Media, JS, Base & Store URL

Tips to get Magento URL paths of skin, Media, JS, Base and Store URL in CMS pages for Magento customization or programming. How we can retrieve URL path from static blocks in CMS, PHP and phtml pages


To Retrieve URL path in STATIC BLOCK
To get SKIN URL
{{skin url='images/sampleimage.jpg '}}
To get Media URL
{{media url='/sampleimage.jpg'}}
To get Store URL
{{store url='mypage.html'}}
To get Base URL
{{base url='yourstore/mypage.html'}}


To Retrieve URL path in PHTML
Note: In editing PHTML don't forget to enclode the following code with PHP tag Not secure Skin URL
getSkinUrl('images/sampleimage.jpg') ?>
Secure Skin URL
getSkinUrl('images/ sampleimage.gif', array('_secure'=>true)) ?>
Get Current URL
$current_url = Mage::helper('core/url')->getCurrentUrl();
Get Home URL
$home_url = Mage::helper('core/url')->getHomeUrl();
Get Magento Media URL
Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);

Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);
Get Magento Skin URL
Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN);
Get Magento Store URL
Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
Get Magento Js URL
Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_JS);

Thursday 10 November 2011

How To Simultaneously Add Multiple Products To A Magento Shopping Cart

Magento's product list view lets customers add products to the shopping cart one at a time. I wanted customers to be able to add multiple products to the shopping cart simultaneously. I created an adhoc AJAX method to accomplish this feature request. Adding a product to a Magento shopping cart is accomplished through an HTTP GET request. It will look like or similar to this:
/path/to/app/checkout/cart/add?product=[id]&qty=[qty] 

That URL is output by the tem­plate helper:

$this->getAddToCartUrl($_product)

Since adding a product to the shopping cart is nothing more than a GET request, then all that needs to be done is queue up the URLs of the desired products, make each request in order, and then reload the page when done. First, in app/design/frontend/default/your-theme/template/catalog/product/list.html, I first added checkboxes to allow customers to select which products they want and also hidden fields for storing the URLs to add the product and text fields for the quantity.

<?php $_iterator = $_product->getId(); >?


<?php if(!$_product->isGrouped()): ?>

<?php endif; ?>

I added this code within the loop that generate the HTML for the product line items for the list. Next, I added the JavaScript that does the actual processing, also within the list section right after the script block that contains: decorateList('products-list', 'none-recursive').

At the bottom of the list I added a submit button.

Clicking the button calls the function addItemsToCart(). The function hides the button to prevent a double click and unhides the status message. Next, the function determines which checkboxes are checked. For each checked checkbox, the function finds the corresponding URL field and quantity field, concatenates the two values, and stores the new URL in an array. If the length of the array is greater than 0, then the function calls processNext(), otherwise it displays an error message and resets the submit button.

The function processNext() first updates the process­ing message. The function takes the array of URLs and an index, and then creates an AJAX GET request using the URL at the given index in the array. If the AJAX request completes, it calls processNext() with the same array but with an incremented index while the index is less than the array length. If the incremented index is greater than the array length, then that ends the processing and the function reloads the page.

 That's it.

Wednesday 9 November 2011

Magento: set number of products in new products

You can set number of products to be displayed in new product in your new.phtml file. Find:
 <?php $i=0; foreach ($_products->getItems() as $_product): ?>
and add above, so the code looks like this:
    <?php $_products->setOrder('news_from_date')->setPageSize(6)
  ->setCurPage(1);?>
    >?php $i=0; foreach ($_products->getItems() as $_product): ?>
Set the number (6) as you like. Or you can display all new product by $size = $_products->getSize(); set the number ($size). On the other hand you can set this number in administration on home cms page. Under Custom desing set the code for new products to look like this:

    
    8
    
    bundle
    bundle/catalog_product_price
    
    
    
    
Just set the number in tags.

Magento Cron error: /bin/sh: -c: line 0: syntax error near unexpected token `newline'

I got below error in my mail when i set up cron for magento.

/bin/sh: -c: line 0: syntax error near unexpected token `newline'
/bin/sh: -c: line 0: `php /home/pooja/public_html/magento/journal/cron1.php >'

I found solution for that is i remove newline space from my cron1.php file.
and its work.