2
$baseUrl = $this->getBaseUrl();
$skinUrl = $this->getSkinUrl();
$currcategory = Mage::getSingleton('catalog/layer')->getCurrentCategory();
$products = file_get_contents('c_node.json');
$arr = json_decode($products,TRUE);
$temp = array_values($arr);

<ol class="product-grid">
<?php foreach ($temp as $product): ?>
    <?php
    $id = $product['entity_id'];
    $media = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);
    $url = $product['url_path'];
    $small_image = $media.'catalog/product/'. $product['small_image'];
    $small_img_lable = $product['small_image_label'];
    $thumbnail = $media.'catalog/product/'. $product['thumbnail'];
    $name = $product['name'];
    $price = $product['price'];
    $special_price = $product['special_price'];
    $size = $product['size'];
    $imageUrl = $media.'catalog/product/'. $product['small_image'];
    ?>
    <li class="product-grid_item product-tile">
        <a class="product-image" href="<?php echo $url; ?>">
            <div class="quickview"><button class="button button--s button--inverted">Quick View</button></div>
                <picture class="pimg1">
                    <source srcset="<?php echo $small_image; ?>" media="(min-width: 400px)">
                    <img srcset="<?php echo $small_image; ?>" alt="<?php echo $small_img_lable;?>">
                </picture>
                <picture class="pimg2">
                    <source srcset="<?php echo $thumbnail; ?>" media="(min-width: 400px)">
                    <img srcset="<?php echo $thumbnail; ?>" alt="<?php echo $thumbnail;?>">
                </picture>
            <?php
            // GravDept:
            // Check if product is new.
            //$isNewProduct = Mage::helper('magefrontend')->isNewProduct($id);
            ?>
            <?php //if ($isNewProduct): ?>
                <!--
                <ul class="badge-list">
                    <li class="badge-list_item badge-list_item--new">
                        <?php// echo $this->__('New'); ?>
                    </li>
                </ul>
                -->
            <?php //endif; ?>
        </a>
        <?php
        $showMessage = false;
        if($size == 'XXL'){ // Use your option text to match
            $showMessage = true;
            break;
        }
        ?>
        <?php if($showMessage): ?>
            <div class="product-banner-box">
                <span class="plussize-banner">now in plus sizes</span>
            </div>
        <?php endif; ?>

            <!--
            <div class="product-banner-box">
                <span class="plussize-banner">now in plus sizes</span>
            </div>
            -->

        <?php //if ($id->getRatingSummary()): ?>
            <?php //echo  ?>
        <?php //endif; ?>

        <?php //echo $this->getPriceHtml($product, true); ?>
            <div class="price-box">
                <?php if (!$special_price == 'null'):?>
                    <p class="old-price"> <span class="price-label">Regular Price:</span> <span class="price"><?php echo $price;?></span> </p>
                    <p class="special-price"> <span class="price-label">Special Price</span> <span class="price"> <?php echo $special_price; ?></span> </p>
                 <?php elseif($special_price == 'null'):?>
                     <span class="regular-price">
                        <span class="price"><?php echo $price;?></span>
                    </span>
                <?php endif;?>
            </div>

        <?php //if ($this->helper('wishlist')->isAllow()): ?>
            <a class="product-addwishlist" href="<?php //echo $this->helper('wishlist')->getAddUrl($product); ?>">
                <span class="trigger-icon icon-heart"></span>
            </a>
        <?php //endif; ?>

        <a class="product-name" href="<?php echo $url; ?>">
            <?php echo $name; ?>
        </a>

Observer:

public function productDataToJson(Varien_Event_Observer $observer)
{
    $array = array();
    $products = Mage::getModel('catalog/product')->getCollection()
        ->addAttributeToSelect(array('name', 'thumbnail','size','price','special_price','url_path','thumb‌​nail','small_image',‌​'small_image_label')‌​)
        ->addStoreFilter(Mage::app()->getStore()->getId());

    foreach ($products as $_product) {
        $array[] = $_product->getData();
    }

    $jsonData = Mage::helper('core')->jsonEncode($array);
    file_put_contents("c_node.json", $jsonData);
}
5
  • 1
    Great you got ans :) Commented Jul 11, 2017 at 11:33
  • Need one more help, I want the json files of all category products, Means I want products json file as per categories. Commented Jul 12, 2017 at 4:57
  • Parse error: syntax error, unexpected '​' (T_STRING), expecting ')' :( @sv3n I getting this error, now i want multiple json file with category products as you suggest. but got this error :( Commented Jul 12, 2017 at 10:53
  • @sv3n need urgent help Commented Jul 12, 2017 at 11:27
  • Sorry, I'm busy today. As error says there seems to be a missing )
    – sv3n
    Commented Jul 12, 2017 at 11:33

3 Answers 3

2

If you want wo have only products from current category, you can try this in you observer code:

$array = array();
$category = Mage::registry('current_category');
# $category = Mage::getModel('catalog/category')->load(41);
$products = $category->getProductCollection()
    ->addAttributeToSelect(array('name', 'thumbnail', 'size', 'price', 'special_price', 'url_path', 'thumb‌​nail', 'small_image',‌ ​'small_image_label')‌​);

foreach ($products as $product) {
    $array[] = $product->getData();
}

$jsonData = Mage::helper('core')->jsonEncode($array);
# save json file for each category
file_put_contents('c_' . $category->getId() . '_node.json', $jsonData);

Edit:

You can add products category ids in your observer using $_product->getCategoryIds():

foreach ($products as $_product) {
    $array[] = array_merge($_product->getData(), array('category_ids' => $_product->getCategoryIds()));
}

For you outp you can try something like this:

<?php foreach ($temp as $product): ?>
    <?php
    $catIds = $product['category_ids'];
    ...
    if (in_array(Mage::registry('current_category')->getId(), $catIds)) {
        ...
    }
6
  • Its not working, actually I want all products in json but along with its category ids Commented Jul 11, 2017 at 11:03
  • OK, then please add a better description to your question. Which event do you observe? What you need in detail?
    – sv3n
    Commented Jul 11, 2017 at 11:05
  • If user add/update/delete product, json file needs to get updtaed with the product details, So that i will use this details on product listing page. But I category product listing page needs to be contain only that current category products. Commented Jul 11, 2017 at 11:08
  • 1
    Just to mention ... your code may work for small product catalog, but I guess on larger ones this may be not effective.
    – sv3n
    Commented Jul 11, 2017 at 11:36
  • Okay, I got this. If any doubt, I will ask . Thnk you once agian. :) Commented Jul 11, 2017 at 11:40
3

Check like that in your json you will get $_product->getCategoryIds().

Please write a condition like that

<?php foreach ($temp as $product): ?>
if (in_array($currcategory , $_product->getCategoryIds()) {

Hope it will helpful for you

13
  • My json is not getting category_id, How can I get this? Commented Jul 11, 2017 at 8:50
  • 1
    >joinField('category_id','catalog/category_product','category_id','product_id=entity_id',null,'left'); add this and get category id Commented Jul 11, 2017 at 9:25
  • 1
    before ->addstorefilter() in productdatatojson code Commented Jul 11, 2017 at 9:34
  • 1
    @RamaChandran +1 for right idea :) Sorry "stealing" this but i knew code from previous quesetion :)
    – sv3n
    Commented Jul 11, 2017 at 14:15
  • 1
    @sv3n No problem , We are providing right right solution to Magento community and helping other. Thanks for vote :) Commented Jul 11, 2017 at 14:21
1
  public function productDataToJson(Varien_Event_Observer $observer)
{
  $category = Mage::getModel('catalog/category');
  $tree = $category->getTreeModel();
  $tree->load();
  $ids = $tree->getCollection()->getAllIds();
  foreach ($ids as $id) {
    $array = array();
    $products = Mage::getModel('catalog/category')->load($id)
        ->getProductCollection()
         ->addAttributeToSelect('*')
         ->addStoreFilter(Mage::app()->getStore()->getId());
    foreach ($products as $_product) {
        $array[] = array_merge($_product->getData(), array('category_ids' => $_product->getCategoryIds()));
    }
    $jsonData = Mage::helper('core')->jsonEncode($array);
    # save json file for each category
    file_put_contents('c_' . $id . '_node.json', $jsonData);
  }
}

I solved this!! Thank You, If anything wrong, please suggest.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.