Jump to content

Cleaning Up Of Code


fRAiLtY-

Recommended Posts

Hi all,

 

Got some code which works, it's been extended then extended again over the past few months, and I'm just wondering if there's a tidier or more efficient way of putting altogether perhaps in less functions maybe or anything really.

 

If anyone can provide some advise or feel free to move it around that would be a massive help. I like things to be neat and tidy and this certainly isn't.

 

public function getMatrixData($requiredAttributeIds = null, $product = null, $stock)
{
 Varien_Profiler::start('CONFIGURABLE:' . __METHOD__);
 $this->_usedProducts = '_cache_instance_products';
 if ($this->getProduct($product)->hasData($this->_usedProducts)) {
	 if (is_null($requiredAttributeIds) and is_null($this->getProduct($product)->getData($this->_configurableAttributes))) {
		 $this->getConfigurableAttributes($product);
		 Varien_Profiler::stop('CONFIGURABLE:' . __METHOD__);
		 return $this->getProduct($product)->getData($this->_usedProducts);

	 }

	 $usedProducts = array();
	 $collection = $this->getUsedProductCollection($product)->addAttributeToSelect('*')->addFieldToFilter('name', array(
		 'like' => '%' . $stock . '%'
	 ))->addAttributetoSort('size', 'DESC');

	 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);
}

public function getAltMatrixData($requiredAttributeIds = null, $product = null, $stock)
{
 Varien_Profiler::start('CONFIGURABLE:' . __METHOD__);
 $this->_usedProducts = '_cache_instance_products';
 if ($this->getProduct($product)->hasData($this->_usedProducts)) {
	 if (is_null($requiredAttributeIds) and is_null($this->getProduct($product)->getData($this->_configurableAttributes))) {
		 $this->getConfigurableAttributes($product);
		 Varien_Profiler::stop('CONFIGURABLE:' . __METHOD__);
		 return $this->getProduct($product)->getData($this->_usedProducts);

	 }

	 $usedProducts = array();
	 $collection = $this->getUsedProductCollection($product)->addAttributeToSelect('*')->addFieldToFilter('name', array( 'like' => '%' . $stock . '%' ));

	 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);
}

public function getTable($product = false, $stock)
{
 if (!$product)
	 return false;

 $childProducts = $this->getMatrixData(null, $product, $stock);
 $x			 = array();
 $r			 = '';
 $sizes		 = array();
 foreach ($childProducts as $children) {
	 $size											 = $children->getAttributeText('size');
	 $x[$children->getAttributeText('quantity')][$size] = array(
		 'id' => $children->getId(),
		 'price' => number_format($children->getPrice(), '2'),
		 'name' => $children->getName()
	 );
	 if (!in_array($size, $sizes)) {
		 $sizes[] = $size;
	 }
 }

 ksort($x);
 $r .= '<table id="' . strtolower(str_replace(' ', '-', $stock)) . '" class="matrix"><tr><th></th>';

 foreach ($sizes as $size) {
	 $r .= '<th>' . $size . '</th>';
 }
 $r .= '</tr>';

 foreach ($x as $quantity => $data) {
	 $i = 1;
	 $r .= '<tr><th id=' . $quantity . '>' . $quantity . '</th>';
	 foreach ($sizes as $size) {
		 if (isset($data[$size])) {
			 $item = $data[$size];
			 $r .= '<td id=' . $quantity . ' class="col' . $i++ . '"><a href="/checkout/cart/add?product=' . $item['id'] . '" title="Add ' . $item['name'] . ' to basket">£' . $item['price'] . '</a></td>';
		 } else {
			 $r .= '<td id=' . $quantity . ' class="col' . $i++ . '">-</td>';
		 }
	 }

	 $r .= '</tr>';
 }
 $r .= '</table>';
 $r .= '<p class="note"><strong>Note:</strong> Prices in this table do not include additional options as specified above!</p>';
 return $r;
}

public function getAltTable($product = false, $stock)
{
 if (!$product)
	 return false;

 $childProducts = $this->getMatrixData(null, $product, $stock);
 $x			 = array();
 $r			 = '';
 $sizes		 = array();
 foreach ($childProducts as $children) {
	 $size											 = $children->getAttributeText('size');
	 $x[$children->getAttributeText('quantity')][$size] = array(
		 'id' => $children->getId(),
		 'price' => number_format($children->getPrice(), '2'),
		 'name' => $children->getName()
	 );
	 if (!in_array($size, $sizes)) {
		 $sizes[] = $size;
	 }
 }

 function bannerSizeSort($a,$B) {
$pattern = '/(\d+(\.\d+)?)ft x (\d+(\.\d+)?)ft/';
if (preg_match($pattern, $a, $n1) && preg_match($pattern, $b, $n2)) {
if ($n1[1] == $n2[1] && $n1[3] == $n2[3]) return 0;
else if ($n1[1] > $n2[1] || ($n1[1] == $n2[1] && $n1[3] > $n2[3])) return 1;
else return -1;
}
return 0;

}

usort($sizes, 'bannerSizeSort');
 $r .= '<table id="banner" class="matrix"><tr><th></th>';

 foreach ($x as $quantity => $data) {
	 $r .= '<th id=' . $quantity . '>Each</th>';
 }
 $r .= '</tr>';

 foreach ($sizes as $size) {
	 $i = 1;
	 $r .= '<tr><th>' . $size . '</th>';
	 if (isset($data[$size])) {
		 $item = $data[$size];
		 $r .= '<td id=' . str_replace(array(".", ","," "), "", $size) . ' class="col' . $i++ . '"><a href="/checkout/cart/add?product=' . $item['id'] . '" title="Add ' . $item['name'] . ' to basket">£' . $item['price'] . '</a></td>';
	 } else {
		 $r .= '<td id=' . str_replace(array(".", ","," "), "", $size) . ' class="col' . $i++ . '">-</td>';
	 }

	 $r .= '</tr>';
 }

 $r .= '</tr>';
 $r .= '</table>';
 $r .= '<p class="note"><strong>Note:</strong> Prices in this table do not include additional options as specified above!</p>';
 return $r;
}

public function getAlt2Table($product = false, $stock)
{
 if (!$product)
	 return false;

 $childProducts = $this->getAltMatrixData(null, $product, $stock);
 $x			 = array();
 $r			 = '';
 $sizes		 = array();
 foreach ($childProducts as $children) {
	 $size											 = $children->getAttributeText('size');
	 $x[$children->getAttributeText('quantity')][$size] = array(
		 'id' => $children->getId(),
		 'price' => number_format($children->getPrice(), '2'),
		 'name' => $children->getName()
	 );
	 if (!in_array($size, $sizes)) {
		 $sizes[] = $size;
	 }
 }

natsort($sizes);
 $r .= '<table id="label" class="matrix"><tr><th></th>';

 ksort($x);
 foreach ($x as $quantity => $data) {
	 $r .= '<th id=' . $quantity . '>' . $quantity . '</th>';
 }
 $r .= '</tr>';

 foreach ($sizes as $size) {		
	 $i = 1;
	 $r .= '<tr><th>' . $size . '</th>';
	 foreach ($x as $quantity => $data) {
			 $item = $data[$size];
			 $r .= '<td id=' . str_replace(" ","",$size) . ' class="col' . $i++ . '"><a href="/checkout/cart/add?product=' . $item['id'] . '" title="Add ' . $item['name'] . ' to basket">£' . $item['price'] . '</a></td>';
	 }
 }

 $r .= '</tr>';
 $r .= '</table>';
 $r .= '<p class="note"><strong>Note:</strong> Prices in this table do not include additional options as specified above!</p>';
 return $r;
}

Link to comment
Share on other sites

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.