0

I want to query the database for all products with urls starting "afr-" and print them on a cms page in a table. For example, the final printed table would look like:

    <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tbody>
    <tr>
  <td><a href="https://example.com/afr-753954329">753954329</a></td>
  <td><a href="https://example.com/afr-753954329">https://example.com/afr-753954329</a></td>
</tr>

<tr>
  <td><a href="https://example.com/afr-498563241">498563241</a></td>
  <td><a href="https://example.com/afr-498563241">https://example.com/afr-498563241</a></td>
</tr>

<tr>
  <td><a href="https://example.com/afr-392736177">392736177</a></td>
  <td><a href="https://example.com/afr-392736177">https://example.com/afr-392736177</a></td>
</tr>

    </tbody>
    </table>
0

You can try this code to get the url of all products in magento

<?php
$_productCollection = Mage::getModel('catalog/product')
                        ->getCollection()
                        ->addAttributeToSort('created_at', 'DESC')
                        ->addAttributeToSelect('*')
                        ->load();
$base_url = Mage::getBaseUrl();

foreach ($_productCollection as $_product){
   $product_url = $_product->getProductUrl();
   echo $base_url.$product_url.'<br>';
}
?>
0

Try below code

<?php $product = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*')->addAttributeToFilter('product_url',array('like' =>"afr-"."%"));?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tbody>
<?php foreach($product as $_product):?>
    <tr>
     <td><a href="<?php echo $_product->getProductUrl();?>">753954329</a> 
    </td>
   </tr>
<?php endforeach;?>

    </tbody>
    </table>
0

Try this code to print all the product URLs starting with "-afr", please try below code:

<?php
$_productCollection = Mage::getModel('catalog/product')
        ->getCollection()
        ->addAttributeToSort('created_at', 'DESC')
        ->addAttributeToSelect('*')
        ->load();
if (count($_productCollection)) {
    ?>
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tbody>
        <?php
        }

        foreach ($_productCollection as $_product) {
            $product_url = $_product->getProductUrl();
            if (stripos($product_url, 'afr-')) {
                ?>
                <tr>
                    <td><a href="<?php echo $product_url; ?>"><?php echo $_product->getSku(); ?></a></td>
                    <td><a href="<?php echo $product_url; ?>"><?php echo $product_url; ?></a></td>
                </tr>
                <?php
            }
        }
        if (count($_productCollection)) {
            ?>
        </tbody></table>
    <?php
}
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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