Tuesday 29 July 2014

Bestseller products in Magento

If you search around the web for how to create a Magento bestsellers page then the core PHP snippet suggested to generate the bestsellers data is a variation on the following: - See more at: http://www.zodiacmedia.co.uk/blog/magento-bestselling-products#sthash.ZS65Z56j.dpuf
If you search around the web for how to create a Magento bestsellers page then the core PHP snippet suggested to generate the bestsellers data is a variation on the following: - See more at: http://www.zodiacmedia.co.uk/blog/magento-bestselling-products#sthash.ZS65Z56j.dpuf
If you search around the web for how to create a Magento bestsellers page then the core PHP snippet suggested to generate the bestsellers data is a variation on the following: - See more at: http://www.zodiacmedia.co.uk/blog/magento-bestselling-products#sthash.ZS65Z56j.dpuf
If you search around the web for how to create a Magento bestsellers page then the core PHP snippet suggested to generate the bestsellers data is a variation on the following: - See more at: http://www.zodiacmedia.co.uk/blog/magento-bestselling-products#sthash.ZS65Z56j.dpuf

If you search around the web for how to create a Magento bestsellers page then the core PHP snippet suggested to generate the bestsellers data is a variation on the following:

<?php
            $_categoryId = 'CATEGORY_ID';
            $catagory_model = Mage::getModel('catalog/category')->load($_categoryId); //where $category_id is the id of the category
            $collection = Mage::getResourceModel('catalog/product_collection');
            $collection->addCategoryFilter($catagory_model1); //category filter
            $collection->addAttributeToFilter('status',1); //only enabled product
            $collection->addAttributeToSelect(array('name','price','url','small_image')); //add product attribute to be fetched
            $collection->setOrder('id', 'DESC');
            $collection->setPage(1,8);
            $collection->addStoreFilter();   ?>

 <?php 
        if(!empty($collection)):
                foreach ($collection as $_product): ?>
                  <li>
          <a href="<?php echo $_product1->getProductUrl(); ?>">
      <img src="<?php echo $this->helper('catalog/image')->init($_product1, 'small_image')->resize(218,115); ?>"  alt="" />
    </a>
 <a href="<?php echo $_product1->getProductUrl(); ?>">
                    <h3><?php echo $_product1->getName(); ?></h3>
                    </a>
<span class="price"><?php echo $_product1->getFinalPrice(); ?></span>
</li>
                  <?php
            endforeach;
            endif;       
        ?>        

User is not able to add product into wishlist in Magento

All the code you need can be found in app/code/core/Mage/Wishlist/controllers/IndexController.php method _addItemToWishList.
You will need to load the product by it's ID and load the wish list by customer. The code would look roughly something like this.



<?php
$wishlist = Mage::getModel('wishlist/wishlist')->loadByCustomer($customerId, true);
$product = Mage::getModel('catalog/product')->load($productId);

$buyRequest = new Varien_Object(array()); // any possible options that are configurable and you want to save with the product

$result = $wishlist->addNewItem($product, $buyRequest);
$wishlist->save(); ?>
 
 
Just out of curiosity. Why not use the addAction provided by the wishlist controller to add the products?
You can obtain the url via echo Mage::helper('wishlist')->getAddUrl($product);
 

Friday 25 July 2014

social sharing not working in magento

Simple social sharing

Here is a list of URLs for some popular social sites that you can use for sharing:
  • Google Plus: https://plus.google.com/share?url=[URL TO SHARE]
  • Facebook: https://www.facebook.com/sharer/sharer.php?u=[URL TO SHARE]&t=[TITLE]
  • Twitter: http://twitter.com/home/?status=[TITLE] ([URL TO SHARE])
  • Pinterest: https://pinterest.com/pin/create/button/?url=[LINKBACK URL]&media=[IMAGE TO SHARE]& description=[DESCRIPTION]

Implementation

Follow these simple steps and you will have social sharing links on your product pages in no time:
  • Openapp/design/frontend/YOUR_PACKAGE/YOUR_THEME/template/catalog/product/view.phtml
  • Find the place where you want to add your links/buttons and add the following:

    <?php $productName = $_helper->productAttribute($_product, $_product->getName(), 'name'); ?>
    <?php $productUrl = $_helper->productAttribute($_product, $_product->getProductUrl(), 'product_url'); ?>
    <?php $productImage = $_product->getImageUrl() ?>
    We will use these variables to set URL, title, description or any other parameter required.
  • Add the following code below the variables we defined in the previous step:


    // Google Plus
    <a href="javascript:popWin('https://plus.google.com/share?url=<?php echo urlencode($productUrl); ?>', 'google', 'width=640,height=480,left=0,top=0,location=no,status=yes,scrollbars=yes,resizable=yes');" title="<?php echo $this->__('Share on Google Plus') ?>">Google Plus</a>
    // Facebook
    <a href="javascript:popWin('https://www.facebook.com/sharer/sharer.php?u=<?php echo urlencode($productUrl); ?>&t=<?php echo urlencode($productName); ?>', 'facebook', 'width=640,height=480,left=0,top=0,location=no,status=yes,scrollbars=yes,resizable=yes');" title="<?php echo $this->__('Share on Facebook') ?>">Facebook</a>
    // Twitter
    <a href="javascript:popWin('http://twitter.com/home/?status=<?php echo urlencode($productName . ' (' . $productUrl . ')'); ?>', 'twitter', 'width=640,height=480,left=0,top=0,location=no,status=yes,scrollbars=yes,resizable=yes');" title="<?php echo $this->__('Tweet') ?>">Twitter</a>
    // Pinterest
    <a href="javascript:popWin('https://pinterest.com/pin/create/button/?url=<?php echo urlencode($productUrl); ?>&media=<?php echo urlencode($productImage); ?>&description=<?php echo urlencode($productName); ?>', 'pinterest', 'width=640,height=480,left=0,top=0,location=no,status=yes,scrollbars=yes,resizable=yes');" title="<?php echo $this->__('Pin it') ?>">Pinterest</a>
Although all social sites are quite good at extracting content form the links you share (product name, image, description …)
Adding OpenGraph meta tags to your Magento store is quite easy. Just add the following code below the last meta tag (should be a meta tag named “robots” if you are using a default Magento head file) inapp/design/frontend/YOUR_PACKAGE/YOUR_THEME/template/page/html/head.phtml

<?php $product = Mage::registry('current_product');
if ($product): ?>
<meta property="og:title" content="<?php echo $product->getName(); ?>" />
<meta property="og:type" content="product" />
<meta property="og:url" content="<?php echo $this->helper('catalog/product')->getProductUrl($product); ?>" />
<meta property="og:image" content="<?php echo $this->helper('catalog/image')->init($product, 'image')->resize(300, 300); ?>" />
<meta property="og:description" content="<?php echo strip_tags($product->getShortDescription()); ?>" />
<meta property="og:site_name" content="<?php echo Mage::app()->getStore()->getName() ?>" />
<?php endif; ?>
We are sharing a product so we need to add meta tags to product pages only but head.phtml is loaded on every page. This means that we need to check if the current page is a product page and only then add our meta tags.

Add Bundle Product In Magento

Step 1: Create the Bundle Product

  1. From the Admin panel, select Catalog > Manage Products. Then, click the Add Product button.
  2. In the Create Product Settings section, select the Attribute Set for the product.
  3. Make sure the Use Complex Product Types checkbox is selected.  Set Product Type to “Bundle Product,” and click the Continue button.

Step 2: Complete the Product Information

  1. Complete the Product Information as you would for a Simple product, with the exception of the following:
    1. On the General tab, set both the SKU and Weight fields to one of the following:
      • Dynamic
      • Fixed
        If using a Fixed value, enter the actual value in the field to the right.
    2. On the Prices tab, set the Price to one of the following:
      • Dynamic
      • Fixed
        If using a Fixed value, enter the actual value in the field to the right.
    3. On the Prices tab, set the Price View to one of the following:
      • As Low as
      • Price Range

Step 1: Create the Bundle Product

  1. From the Admin panel, select Catalog > Manage Products. Then, click the Add Product button.
  2. In the Create Product Settings section, select the Attribute Set for the product.
  3. Make sure the Use Complex Product Types checkbox is selected.  Set Product Type to “Bundle Product,” and click the Continue button.

Step 2: Complete the Product Information

  1. Complete the Product Information as you would for a Simple product, with the exception of the following:
    1. On the General tab, set both the SKU and Weight fields to one of the following:
      • Dynamic
      • Fixed
        If using a Fixed value, enter the actual value in the field to the right.
    2. On the Prices tab, set the Price to one of the following:
      • Dynamic
      • Fixed
        If using a Fixed value, enter the actual value in the field to the right.
    3. On the Prices tab, set the Price View to one of the following:
      • As Low as
      • Price Range

Step 3: Add Bundle Items

  1. In the Product Information panel on the left, select the last option, Bundle Items.
  2. Then in the Shipment section, set Ship Bundle Items to one of the following:
    • Together
    • Separately
  3. In the Bundle Items section, click the Add New Option button.
  4. In the Default Title box, enter a label for the bundle item, as you want it to appear on the Customize Product page.  Then, do the following:
    1. Set the Input Type to one of the following:
      • Drop-down
      • Radio Buttons
      • Checkbox
      • Multiple Select
    2. Set Is Required as needed.
    3. In the Position field, enter the order that you want this item listed in relation to other items included in the bundle.
  5. Click the Add Selection button. Then, do the following:
    1. Click the Reset Filter button to display the list of products.
    2. Select the checkbox of each product that you want to include in this item.
    3. Then, in the Qty to Add column, enter the quantity of each item to be included.
  6. Click the Add Selected Product(s) to Option button.
  7. When the items appear in the Bundle Items list, do the following:
    1. If you want to prevent the customer from changing the quantity of the item, set User Defined Qty to “No.” Otherwise, accept the default setting of “Yes” which displays an input box so the customer can enter the quantity.
    2. To change the order in which the items are listed, enter a number in the Positioncolumn of each item to determine its position in relation to other items.
    3. Select the Default option of the item you want to be pre-selected in the form.
  8. Repeat steps 3-7 for each item you want to include in the bundle.
  9. When complete, click the Save button to save the Bundle product.

Configurable Product Settings

By default, Manage Stock is set to No because the inventory of a configurable product is derived from the quantities of its associated simple products. If you want to manage the inventory manually, clear the Use Config Settings check box and select Yes for Manage Stock. The fields Enable Qty Increments andStock Availability are enabled.
  • Enable Qty Increments: If you want to sell your products in batches (increments), set the desired quantity number here. A customer will be able to purchase this product only in the increments that you specify.
  • Stock Availability: If you want to temporarily remove an item from sale, select the Out of Stock option in the drop-down menu.

Creating a Simple Associated Product

This section enables you to create simple products to associate to your configurable product.
To create a simple associated product:
  1. In the Create Simple Associated Product box, click Create Empty. A popup window opens with a regular product creation page, but with a preselected product type (Simple Product) and an attribute set that is the same as the configurable product.
  2. Specify the settings as you would when creating a Simple Product.
  3. Click Save to add your product to your configurable product list.
  4. Repeat for additional simple associated products.
    The Copy from Configurable button works in a similar manner to the above, but it pre-populates many of the fields with information from the configurable product.

How to Add Simple Product In magento

Step 1: Choose the Attribute Set.

  1. From the Admin panel, go to Catalog > Manage Products.
  2. In the upper-right corner of the Manage Products page, click the Add Product button.
  3. In the Create Product Settings section, accept the default settings for Attribute Set and Product Type. Then, click the Continue button.

Step 2: Complete the Required Fields.

  1. On the General tab, complete the required fields as follows:
    1. Type the product Name as you want it to appear in all catalog listings.
    2. In the Description box, type the main description that will appear on the Product View page.
    3. Type a Short Description of the product.
    4. Assign a unique SKU for the product.
    5. Type the product Weight, which is used for shipping calculations.
    6. Set Status to “Enabled.”
    7. Set Visibility to “Catalog, Search.”
  2. On the Prices tab, complete the required fields as follows:
    1. Enter the Price that you want to charge for the product.
    2. Set Tax Class to identify the appropriate tax classification for the product.
  3. Although not a required field, the product must be assigned to a category to be visible in your store. To assign the product to a category, do the following:
    1. In the Product Information panel on the left, select Categories.
    2. In the category tree, click to expand the section where the item belongs.
    3. Select the checkbox of each category where you want the product to appear.
      Product Categories
  4. If your store has multiple views, the product must be assigned to each store view where it is available for sale. (This option appears only if multiple store views are available.) To assign the product to a store view, do the following:
    1. In the Product Information panel on the left, select Websites.
    2. On the Website tab, select the checkbox of each website where the product is available for sale.
  5. After completing these steps, click the Save and Continue button.

Step 3: Complete the Remaining Product Information.

  1. If you plan to track inventory for the product, do the following:
    1. In the Product Information panel on the left, select Inventory.
    2. Under Manage Stock, clear the Use Config Settings checkbox. Then, set Manage Stock to “Yes.”
    3. In the Qty field, type the quantity of the item that is currently in stock.
    4. Set Stock Availability to “In Stock.”
    5. Click the Save and Continue button.
    6. To upload images for your product, see: “ Adding Product Images.”

Thursday 24 July 2014

Addattribute to filter Conditionals In Magento

addAttributeToFilter is a function that can be called on a product collection in Magento. In short, it adds a condition to the WHERE part of the MySQL query used to extract a product collection from the database.
$_products = Mage::getModel('catalog/product')->getCollection()
   ->addAttributeToSelect(array('name', 'product_url', 'small_image'))
   ->addAttributeToFilter('sku', array('like' => 'UX%'))
    ->load();
The above code would get a product collection, with each product having it's name, url, price and small image loaded in it's data array. The product collection would be filtered and contain only products that have an SKU starting with UX.

addAttributeToFilter Conditionals

Notice above, I used the LIKE operator? There are many more operators in SQL and addAttributeToFilter will accept them all. I include them below as well as a reference for you. Hopefully this will save you some time.

Equals: eq

$_products->addAttributeToFilter('status', array('eq' => 1));

Not Equals - neq

$_products->addAttributeToFilter('sku', array('neq' => 'test-product'));

Like - like

$_products->addAttributeToFilter('sku', array('like' => 'UX%'));
One thing to note about like is that you can include SQL wildcard characters such as the percent sign.

Not Like - nlike

$_products->addAttributeToFilter('sku', array('nlike' => 'err-prod%'));

In - in

$_products->addAttributeToFilter('id', array('in' => array(1,4,98)));
When using in, the value parameter accepts an array of values.

Not In - nin

$_products->addAttributeToFilter('id', array('nin' => array(1,4,98)));

NULL - null

$_products->addAttributeToFilter('description', 'null');

Not NULL - notnull

$_products->addAttributeToFilter('description', 'notnull');

Greater Than - gt

$_products->addAttributeToFilter('id', array('gt' => 5));

Less Than - lt

$_products->addAttributeToFilter('id', array('lt' => 5));

Greater Than or Equals To- gteq

$_products->addAttributeToFilter('id', array('gteq' => 5));

Less Than or Equals To - lteq

$_products->addAttributeToFilter('id', array('lteq' => 5));

addFieldToFilter()

Debugging The SQL Query

There are two ways to debug the query being executed when loading a collection in Magento.
// Method 1
Mage::getModel('catalog/product')->getCollection()->load(true);

// Method 2 (Quicker, Recommended)
$collection = Mage::getModel('catalog/product')->getCollection();

echo $collection->getSelect();
Both method 1 and method 2 will print out the query but both will do it in slightly different ways. Method 1 prints the query out as well as loading the products while method 2 will just convert the query object to a string (ie. will print out the SQL). The second method is definitely better as it will be executed much quicker but I include them both here for reference.

bulk product important in magento

  1. Log in to the Admin Panel as an administrator.
  2. Click System > Import/Export > Dataflow - Profiles.
  3. On the Profiles page, click Import All Products.
    This option can be used to import any number of products into Magento CE or EE. You can also use it to update existing products.
  4. In the Import/Export Profile section in the left column, click Upload File.
  5. Click Browse and follow the prompts on your screen to locate the file you previously exported and edited.
  6. Click Save and Continue Edit.
  7. In the left column, click Run Profile.
  8. From the list, click the name of the file you previously uploaded.
    The following figure shows an example.
         



  1. Click Run Profile in Popup.
  2. Wait while your products import.
    Importing typically takes longer than exporting. Progress messages displays during the import. Don't interrupt the import while it's happening.
    The following message displays when all products have been imported:
    Imported number records.
  3. The following message displays to confirm products were imported successfully: