Monday 21 October 2013

Magento change product image on hover in catalog page

If you want to show thumbnail image on mouse over & after mouse out show small image then go through

template->catalog->product->list.pthml

in this Search this code:

<a href=”<?php echo $_product->getProductUrl() ?>” title=”<?php echo $this->stripTags($this->getImageLabel($_product, ‘small_image’), null, true) ?>”><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>

in place of that put this code :

<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')->constrainOnly(FALSE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize(217); ?>" width="147" height="217" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" onmouseover="this.src='<?php echo $this->helper('catalog/image')->init($_product, 'thumbnail')->constrainOnly(FALSE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize(217) ?>';" onmouseout="this.src='<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->constrainOnly(FALSE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize(217) ?>';" />

Display Configure Product Option in Product List Page in Magento.

Add following script in product listing page catalog/product/list.phtml

<?php echo $this->helper(‘checkout’)->formatPrice($_product->getFinalPrice()) ?>
<form action=”<?php echo Mage::getBaseUrl(); ?>checkout/cart/add/” method=”post” style=”display:block; clear:both;”>

<?php
$product = Mage::getModel(‘catalog/product’);
$product->load($_product->getId());
$xml = “”;
$hasAtts = 0;

// configurable products
$productType = $product->getTypeId();
if($productType == “configurable”) {

// configurable products
$attValConfig = $product->getTypeInstance()->getConfigurableAttributesAsArray();

if(sizeof($attValConfig)) {

$hasAtts++;

foreach($attValConfig as $attValConfigSingle) {

$xml .= “<div class=’floatoption1′>”.$attValConfigSingle['frontend_label'].”: “;

$xml .= “<select style=’display:block; clear:both; margin-bottom:10px;’ name=’super_attribute[".$attValConfigSingle['attribute_id'].”]’><option selected=’selected’>Choose an option</option>”;

foreach($attValConfigSingle['values'] as $attValConfigSingleVal) {

$baseprice = $this->getPriceHtml($product, true);
$strippedprice = str_replace(“£”, “”, $baseprice);
$numprice = strip_tags($strippedprice);
$numpricea = (float)$numprice;
$optionprice = $attValConfigSingleVal['pricing_value'];
$numpriceb = (float)$optionprice;
$catprice = $numpricea + $numpriceb;
$catpricea = number_format($catprice,2);

if (is_numeric($numpriceb)) {
$xml .= “<option value=’”.$attValConfigSingleVal['value_index'].”‘>”.$attValConfigSingleVal['label'].” £”.$catpricea.”</option>”;
} else { $xml .= “<option value=’”.$attValConfigSingleVal['value_index'].”‘>Not Mumeric</option>”; }
}
$xml .= “</select></div>”;

}

}

}

// end configurable products
$attVal = $product->getOptions();

if(sizeof($attVal)) {

$hasAtts++;

foreach($attVal as $optionVal) {

$xml .= “<div class=’floatoption1′>Size:<select style=’display:block; clear:both; margin-bottom:10px;’ name=’options[".$optionVal->getId()."]‘><option selected=’selected’>Choose an option</option>”;

foreach($optionVal->getValues() as $valuesKey => $valuesVal) {
$xml .= “<option value=’”.$valuesVal->getId().”‘>”.$valuesVal->getTitle().” +”.Mage::helper(‘core’)->currency($valuesVal->getPrice()).”</option>”;
}

$xml .= “</select></div>”;

}

}

$xml .= “<div class=’floatoption2′>Qty: <input style=’display:block; clear:both; margin-bottom:20px;’ id=’qty’ class=’input-text qty’ type=’text’ value=’1′ maxlength=’12′ name=’qty’/></div>”;

echo($xml);

?>

<input type=”hidden” name=”product” value=”<?php echo($_product->getId()); ?>” />
<div>
<button type=”submit”><span><span><?php echo $this->__(‘Add to Cart’) ?></span></span></button></div>

</form>
<script type=”text/javascript”>
var productAddToCartForm_<?php echo $_product->getId(); ?> = new VarienForm(‘product_addtocart_form_<?php echo $_product->getId(); ?>’);
productAddToCartForm_<?php echo $_product->getId(); ?>.submit = function(){
if (this.validator.validate()) {
this.form.submit();
}
}.bind(productAddToCartForm_<?php echo $_product->getId(); ?>);
</script>

// <![CDATA[
var productAddToCartForm_getId(); ?> = new VarienForm('product_addtocart_form_getId(); ?>');
productAddToCartForm_getId(); ?>.submit = function(){
if (this.validator.validate()) {
this.form.submit();
}
}.bind(productAddToCartForm_getId(); ?>);
// ]]>

Adding product from fornt-end with image in mangento programmatically In Magento

We can add a product from frond end in magneto Paste this code in file and place the file in root folder of project run this file in browser product will created and also give image correct path the image also uploaded

<?php
//$product = Mage::getModel('catalog/product');
$product = new Mage_Catalog_Model_Product();
echo time();
// Build the product
$product->setAttributeSetId(4);// #4 is for default
$product->setTypeId('simple');
$product->setName('Some cool product name');
$product->setDescription('Full description here');
$product->setShortDescription('Short description here');
$product->setSku(time());
$product->setWeight(4.0000);
$product->setStatus(1);
$product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);//4
print_r(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
$product->setPrice(39.99);// # Set some price
$product->setTaxClassId(0);// # default tax class
$product->setStockData(array(
'is_in_stock' => 1,
'qty' => 100
));

//$product->setCategoryIds(array(27));// # some cat id's,
$product->setWebsiteIDs(array(1));// # Website id, 1 is default

//Default Magento attribute
$product->setCreatedAt(strtotime('now'));

try {
    $product->save();
    echo "Product Created";
}
catch (Exception $ex) {
    echo "Product Creation Failed";
}
?>