Jump to content

Recommended Posts

Hi guys,

 

I've got a situation where, for example, file1.php contains a method call and the echo'ing of a table. file2.php contains the method and is what processes and ultimately produces the data and sends it back to file1.php for formatting.

 

I need to mix this up a bit by adding the table echo's to file2.php but retain the "call" in file1.php. Here's my 2 files so to speak currently:

 

File1.php - the table view and call

<?php

$childProducts = Mage::getModel('catalog/product_type_configurable')->getMatrix(null, $_product);
$x = array();
foreach ($childProducts as $children) {
	$x[$children->getAttributeText('quantity')][$children->getAttributeText('size')]
	= array('id'=>$children->getId(), 'price'=>number_format($children->getPrice(),'2'), 'name'=>$children->getName());
}
ksort( $x );
echo '<table class="matrix"><tr><th></th>';
foreach( array_keys(current($x)) as $size ) {
	echo '<th>'.$size.'</th>';
}
echo '</tr>';
foreach( $x as $quantity => $data ) {
	echo '<tr><th>'.$quantity.'</th>';
	foreach( $data as $item ) {
		echo '<td><a href="/checkout/cart/add?product='.$item[id].'" title="Add '.$item[name].' to basket">£'.$item[price].'</a></td>';
	}
	echo '</tr>';
}
echo '</table>';
?>

 

File2.php - the method that processes

<?php class OrganicInternet_SimpleConfigurableProducts_Catalog_Model_Product_Type_Configurable
    extends Mage_Catalog_Model_Product_Type_Configurable
{
/**
     * Retrieve array of "subproducts" for 300gsm flyers based on standard getUsedProducts
     *
     * @param  array
     * @param  Mage_Catalog_Model_Product $product
     * @return array
     */
    public function getMatrix($requiredAttributeIds = null, $product = null)
    {	
    	Varien_Profiler::start('CONFIGURABLE:'.__METHOD__);
        if ($this->getProduct($product)->hasData($this->_usedProducts)) {
            if (is_null($requiredAttributeIds)
                and is_null($this->getProduct($product)->getData($this->_configurableAttributes))) {
                // If used products load before attributes, we will load attributes.
                $this->getConfigurableAttributes($product);
                // After attributes loading products loaded too.
                Varien_Profiler::stop('CONFIGURABLE:'.__METHOD__);
                return $this->getProduct($product)->getData($this->_usedProducts);
            }
            
            $usedProducts = array();
            $collection = $this->getUsedProductCollection($product)
                ->addAttributeToSelect('*')
                ->addFieldToFilter('name', array('like' => '%300gsm Flyers%'));
                //->groupByAttribute('size');
            $x = $collection->getSize();
            // ->addFilterByRequiredOptions();

            if (is_array($requiredAttributeIds)) {
                foreach ($requiredAttributeIds as $attributeId) {
                    $attribute = $this->getAttributeById($attributeId, $product);
                    if (!is_null($attribute))
                        $collection->addAttributeToFilter($attribute->getAttributeCode(), array('notnull'=>1));
                }
            }
            
            foreach ($collection as $item) {
                $usedProducts[] = $item;
            }

            $this->getProduct($product)->setData($this->_usedProducts, $usedProducts);
        }
        Varien_Profiler::stop('CONFIGURABLE:'.__METHOD__);
        return $this->getProduct($product)->getData($this->_usedProducts);
    }
}

?>

 

I need the tabular part, from x = array downwards from File1.php to be part of File2.php, however I need to retain the $childProducts = Mage.... part in File1.php and pass any necessary data through to the method, however which ever way I try it I end up with errors about undefined methods etc. Visually I will see no difference on screen as it should still create the same table in the same way, however I'm going to be using AJAX to produce a new table on select so need the table as part of the method.

 

Any help is greatly appreciated as I keep breaking it!

 

Cheers.

Link to comment
https://forums.phpfreaks.com/topic/258725-merging-2-parts-of-code/
Share on other sites

<?php
public function getTable($product = false) {
	if (!$product) return false;
	$childProducts = $this->getMatrix(null, $product);
	$x = array();
	$r = '';
	foreach ($childProducts as $children) {
		$x[$children->getAttributeText('quantity')][$children->getAttributeText('size')] = array('id'=>$children->getId(), 'price'=>number_format($children->getPrice(),'2'), 'name'=>$children->getName());
	}
	ksort( $x );
	$r .= '<table class="matrix"><tr><th></th>';
	foreach( array_keys(current($x)) as $size ) {
		$r .= '<th>'.$size.'</th>';
	}
	$r .= '</tr>';
	foreach( $x as $quantity => $data ) {
		$r .= '<tr><th>'.$quantity.'</th>';
		foreach( $data as $item ) {
			$r .= '<td><a href="/checkout/cart/add?product='.$item[id].'" title="Add '.$item[name].' to basket">£'.$item[price].'</a></td>';
		}
		$r .= '</tr>';
	}
	$r .= '</table>';
	return $r;
}
?>

 

add this to file2.php and use it like

 

<?php echo Mage::getModel('catalog/product_type_configurable')->getTable($_product); ?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.