Skip to content

Magento’da Anasayfaya Rastgele Ürünler Çekme

Merhaba arkadaşlar,

Bugünkü makalemde sizlere Magento e-ticaret sistemi ile ilgili bir yöntemi anlatıyorum. Daha doğrusu nasıl yapılacağını gösteriyorum. Magento anasayfasında belirli kategorilerden nasıl rastgele ürün çekebileceğinizi anlatacağım.

Öncelikle Magentocommerce‘den Magento’nun son sürümünü indirin.

Şimdi yavaş yavaş başlayalım. Öncelikle anasayfa_rastgele.phtml adlı bir dosya oluşturalım. Bu dosyayı/app/design/frontend/base/default/template/catalog/product içine kaydediyoruz bu dosyayı. Bu dosyanın içeriği şu şekilde olsun:

<?php
/**
 * TR Kodlama
 * @author oralunal
 * @copyright 2011
 * @link http://www.trkodlama.com
 */
?>
<?php
/**
 * Product list template
 *
 * @see Mage_Catalog_Block_Product_List
 */
?>
<?php
    $_productCollection=$this->getLoadedProductCollection();
    $_helper = $this->helper('catalog/output');
?>
<?php if(!$_productCollection->count()): ?>
<p class="note-msg"><?php echo $this->__('There are no products matching the selection.') ?></p>
<?php else: ?>
<div class="category-products">

    <?php $_collectionSize = $_productCollection->count() ?>
    <?php $_columnCount = 4; //TR Kodlama - www.trkodlama.com $this->getColumnCount(); ?>
    <?php
        // TR Kodlama - www.trkodlama.com
        $urunler = $_productCollection->getItems();
        shuffle($urunler);
        $say = 1;
        $max_say = 4;
        // TR Kodlama - www.trkodlama.com
    ?>
    <?php $i=0; foreach ($urunler as $_product): ?>
        <?php if ($i++%$_columnCount==0): ?>
        <ul class="products-grid">
        <?php endif ?>
            <li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
                <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')->resize(170); ?>" width="170" height="170" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></a>
                <h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></a></h2>
                <?php if($_product->getRatingSummary()): ?>
                <?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
                <?php endif; ?>
                <?php echo $this->getPriceHtml($_product, true) ?>
                <div class="actions">
                    <?php if($_product->isSaleable()): ?>
                        <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
                    <?php else: ?>
                        <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                    <?php endif; ?>
                    <ul class="add-to-links">
                        <?php if ($this->helper('wishlist')->isAllow()) : ?>
                            <li><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="link-wishlist"><?php echo $this->__('Add to Wishlist') ?></a></li>
                        <?php endif; ?>
                        <?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
                            <li><span class="separator">|</span> <a href="<?php echo $_compareUrl ?>" class="link-compare"><?php echo $this->__('Add to Compare') ?></a></li>
                        <?php endif; ?>
                    </ul>
                </div>
            </li>
        <?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
        </ul>
        <?php endif ?>
        <?php
        // TR Kodlama - www.trkodlama.com
        if($say == $max_say){break;}
        else{$say++;}
        // TR Kodlama - www.trkodlama.com
        ?>
        <?php endforeach ?>
        <script type="text/javascript">decorateGeneric($$('ul.products-grid'), ['odd','even','first','last'])</script>

</div>
<?php endif; ?>

Bu sayfayı kaydedin. Daha sonra magento yönetici panelinden CMS menüsünün altından Sayfalar(Pages) linkine tıklayın. Burada Anasayfanızın açıldığı sayfayı bulun ve onu açın. Gelen sayfada soldaki menüden İçerik(Contents) linkine tıklayın. Artık bu sayfa içinde:

{{block type="catalog/product_list" category_id="36" template="catalog/product/anasayfa_rastgele.phtml"}}

yazdığınızda belirlediğiniz kategoriden rastgele ürün çekimi yapabileceksiniz. Kategori numarasını değiştirerek bu kodu tekrar tekrar kullanabilirsiniz:

{{block type="catalog/product_list" category_id="36" template="catalog/product/anasayfa_rastgele.phtml"}}
{{block type="catalog/product_list" category_id="4" template="catalog/product/anasayfa_rastgele.phtml"}}
{{block type="catalog/product_list" category_id="86" template="catalog/product/anasayfa_rastgele.phtml"}}

Umarım anlatabilmişimdir. Herkese kolay gelsin

TR Kodlama GitHub'da!

TR Kodlama'da yer alan makaleleri GitHub üzerinden düzenleyebileceğinizi biliyor muyudunuz? Harika değil mi? Bu makale ile ilgili düzenleme yapmak isterseniz aşağıdaki butona tıklayınız!

GitHub'da Görüntüle ve Düzenle!