0

I have some difficulties to convert some phtml codes into php (note: simple conversion with online tools html to php does not work therefore please do not answer if it is to refer online tool). Below is the phtml code that I would like to add in .php file:

<?php 
$_product = Mage::getModel('catalog/product')
                ->setStoreId($_item->getOrder()->getStoreId())
                ->load($_item->getProductId());
?> 

<div style="float:left;width:100px;padding-right:10px;">
<img src="<?php echo Mage::helper('catalog/image')
                         ->init($_product, 'image')
                         ->constrainOnly(TRUE)
                         ->keepAspectRatio(TRUE)
                         ->keepFrame(FALSE)
                         ->resize(100,100); ?>" 
alt="<?php echo $_item->getName() ?>" />             
</div> 

When I converted to php I added those lines as below the code is not working:

         $order  = $this->getOrder();
            $item   = $this->getItem();
            $pdf    = $this->getPdf();
            $page   = $this->getPage();
            $lines  = array();
    echo "<?php $_product = Mage::getModel('catalog/product')->setStoreId($_item->getOrder()->getStoreId())->load($_item->getProductId()); ?>";

            // draw Product name
            $lines[0] = array(array(
   echo "<div style=\"float:left;width:100px;padding-right:10px;\"><img src=\"<?php echo Mage::helper('catalog/image')->init($_product, 'image')->constrainOnly(TRUE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize(100,100); ?>\" alt=\"<?php echo $_item->getName() ?>\" /></div>";
                'text' => Mage::helper('core/string')->str_split($item->getName(), 50, true, true),
                'feed' => 35,
            ));

Therefore can you tell me what should I modify to make it work?

1 Answer 1

1

You can assign all the html code into a variable and concatinate with respective php variables.

<?php
$_product = Mage::getModel('catalog/product')
    ->setStoreId($_item->getOrder()->getStoreId())
    ->load($_item->getProductId());

$imageUrl = Mage::helper('catalog/image')
            ->init($_product, 'image')
            ->constrainOnly(TRUE)
            ->keepAspectRatio(TRUE)
            ->keepFrame(FALSE)
            ->resize(100,100);

$outputData = '<div style="float:left;width:100px;padding-right:10px;">
    <img src="'.$imageUrl.'" alt="'.$_item->getName().'"</div>';

echo $outputData;
?>

Hope it helps.

2
  • but within a .php file isn't strange to put <?php ....?> ?
    – Joel
    Jul 21, 2017 at 11:33
  • No. Without php start and end tags, code will not be compiled.
    – Mike
    Jul 21, 2017 at 12:09

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.