Created
December 18, 2012 10:24
-
-
Save fRAiLtY-/4326909 to your computer and use it in GitHub Desktop.
app/code/local/Bendart/Matrix/Catalog/Model/Product/Type/Configurable.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Extension of SCP with custom methods for matrix tables | |
* | |
* User: Tom Davison | |
* Email: t0mbop@ntlworld.com | |
* Date: 17/12/2012 | |
* File: Configurable.php | |
* | |
* @class Bendart_Matrix_Catalog_Model_Product_Type_Configurable | |
* @see OrganicInternet_SimpleConfigurableProducts_Catalog_Model_Product_Type_Configurable | |
* @see Mage_Catalog_Model_Product_Type_Configurable | |
*/ | |
class Bendart_Matrix_Catalog_Model_Product_Type_Configurable extends OrganicInternet_SimpleConfigurableProducts_Catalog_Model_Product_Type_Configurable | |
{ | |
/** | |
* Return Matrix data based on stock, applicable for most except brochures | |
* | |
* @param null $requiredAttributeIds | |
* @param null $product | |
* @param $stock | |
* @return array|mixed | |
*/ | |
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); | |
} | |
/** | |
* Return alternative data, basically as above without the name sort, used for labels etc | |
* | |
* @param null $requiredAttributeIds | |
* @param null $product | |
* @param $stock | |
* @return mixed | |
*/ | |
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); | |
} | |
/** | |
* Converts the data into a table, standard table for most products | |
* | |
* @param bool $product | |
* @param $stock | |
* @return bool|string | |
*/ | |
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; | |
} | |
/** | |
* Alternate table, axis swapped for banners and different sorting | |
* | |
* @param bool $product | |
* @param $stock | |
* @return bool|string | |
*/ | |
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; | |
} | |
/** | |
* Another alternative table, messy names at the moment, for labels due to odd sizes | |
* | |
* @param bool $product | |
* @param $stock | |
* @return bool|string | |
* | |
*/ | |
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment