The Magento Cookbook and Tricks Bag

Stupid tricks that you can do using PHP in the Magento CE backend:

1) Search the Product Catalog
function getProductIdsBySearch($searchstring, $storeId = '') {
     $ids = array();     

     // Code to Search Product by $searchstring and get Product IDs
     $product_collection = Mage::getResourceModel('catalog/product_collection')
        ->addAttributeToSelect('*')
        ->addAttributeToFilter('name', array('like' => '%'.$searchstring.'%'))
        ->load();

     foreach ($product_collection as $product) {
         $ids[] = $product->getId();
     }
    //return array of product ids
    return $ids;
}
Magento 1.7+ Source: StackOverflow


2) Find an image in a product

3) Automatic Color Swatches for Products with different colors, each being their own product
Search the product catalog for items with the exact same name, find the image called 'swatch' then show the color swatches in a little div in the product page. Put this code in your theme's view.phtml page:
in:  "./app/design/frontend/default/<theme folder>/template/catalog/product/view.phtml"
If you have multiple products called "Panties" and each are a different color, and each has an image called "swatch" which is a color swatch, then this code would locate all of the swatches.

<?php
      $ids = array();
      $swatches = array();
      $urls = array();
      $titles = array();
      $searchstring=$_product->getMetaTitle();
      $product_collection = Mage::getResourceModel('catalog/product_collection')
          ->addAttributeToSelect('*')
          ->addAttributeToFilter('name', array('like' => '%'.$searchstring.'%'))
          ->load();
      echo "\n<!-- SWATCH FIND RESULTS \n";
      echo $_product->getMetaTitle();
      echo "\n";
      foreach ($product_collection as $product) {
          echo "Found Item: ".$product->getName()."\n";
          echo "MetaTitle: ".$product->getMetaTitle()."\n";
          echo "ID: ".$product->getId()." ";
          echo "URL: ".$product->getProductUrl()."\n";
          $_xprod = Mage::getModel('catalog/product')->load($product->getId());
          echo "\n";
          foreach ($_xprod->getMediaGalleryImages() as $_image) {
               echo $_image->getLabel()."\n";
               echo $_image->getUrl()."\n";
               echo "\n";
               if ((preg_match("/swatch/i", $_image->getLabel())) &&
                   ($searchstring == $product->getMetaTitle())) {
                    $swatches[] = $_image->getUrl();
                    $urls[] = $product->getProductUrl();
                    $titles[] = $product->getName();
               }
          }
          $ids[] = $product->getId();
   }
   echo "There are: ".count($ids);
   echo "-->\n";
?>


Using the results of the above code: in the same view.phtml file: "./app/design/frontend/default/<theme folder>/template/catalog/product/view.phtml"

<div class="swatch-box">
     <ul>
     <?php
        for ($ix=0; $ix<count($swatches); $ix++) {
             echo "<li><a href=\"".$urls[$ix]."\"><img src=\"".$swatches[$ix]."\" height=\"32\" title=\"".$titles[$ix]."\"></a></li>\n";
        }
     ?>
     </ul>
</div>

Source: blog

4) Store a Hidden Gallery in your Magento Product Pages, and use it in your jquery code to quickly retreive hidden images.

<div class="hidden-gallery" id="hidden-gallery1" style="display:none;">
    <?php foreach ($this->getGalleryImages() as $_image): ?>
        <img src="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'image', $_image->getFile()); ?>"
           title="<?php echo $this->htmlEscape($_image->getLabel()) ?>"
           alt="<?php echo $this->htmlEscape($_image->getLabel()) ?>">
    <?php endforeach; ?>
</div>


Be Careful with Magento

There are many hacks out there.
https://www.exploit-db.com/exploits/37977

Comments

Popular posts from this blog

Microsoft Visio 2010 Premium Product Keys

Mercedes Benz Diesel CDI EGR Emulator Circuit Diagrams

Fix: The Diagnostic Service Host service failed to start due to the following error. [ solved, no kidding ]