Advertisement
Guest User

Configurable.php (For Magento SCP 1.7)

a guest
Oct 29th, 2013
5,081
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.27 KB | None | 0 0
  1. <?php
  2. class OrganicInternet_SimpleConfigurableProducts_Catalog_Model_Resource_Eav_Mysql4_Product_Indexer_Price_Configurable
  3.     extends Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Indexer_Price_Configurable
  4. {
  5.     protected function _isManageStock()
  6.     {
  7.         return Mage::getStoreConfigFlag(Mage_CatalogInventory_Model_Stock_Item::XML_PATH_MANAGE_STOCK);
  8.     }
  9.  
  10.     #Don't pay any attention to cost of specific conf product options, as SCP doesn't use them
  11.    protected function _applyConfigurableOption()
  12.     {
  13.         return $this;
  14.     }
  15.  
  16.     #This calculates final price using SCP logic: minimal child product finalprice
  17.    #instead of the just the entered configurable price
  18.    #It uses a subquery/group-by hack to ensure that the various column values are all from the row with the lowest final price.
  19.    #See Kasey Speakman comment here: http://dev.mysql.com/doc/refman/5.1/en/example-maximum-column-group-row.html
  20.    #It's all quite complicated. :/
  21.    protected function _prepareFinalPriceData($entityIds = null)
  22.     {
  23.         $this->_prepareDefaultFinalPriceTable();
  24.  
  25.         $write  = $this->_getWriteAdapter();
  26.         $select = $write->select()
  27.             ->from(
  28.                 array('e' => $this->getTable('catalog/product')),
  29.                 array())
  30.             ->joinLeft(
  31.                 array('l' => $this->getTable('catalog/product_super_link')),
  32.                 'l.parent_id = e.entity_id',
  33.                 array())
  34.             ->join(
  35.                 array('ce' => $this->getTable('catalog/product')),
  36.                 'ce.entity_id = l.product_id',
  37.                 array())
  38.             ->join(
  39.                 array('pi' => $this->getIdxTable()),
  40.                 'ce.entity_id = pi.entity_id',
  41.                 array())
  42.             ->join(
  43.                 array('cw' => $this->getTable('core/website')),
  44.                 'pi.website_id = cw.website_id',
  45.                 array())
  46.             ->join(
  47.                 array('csg' => $this->getTable('core/store_group')),
  48.                 'csg.website_id = cw.website_id AND cw.default_group_id = csg.group_id',
  49.                 array())
  50.             ->join(
  51.                 array('cs' => $this->getTable('core/store')),
  52.                 'csg.default_store_id = cs.store_id AND cs.store_id != 0',
  53.                 array())
  54.             ->join(
  55.                 array('cis' => $this->getTable('cataloginventory/stock')),
  56.                 '',
  57.                 array())
  58.             ->joinLeft(
  59.                 array('cisi' => $this->getTable('cataloginventory/stock_item')),
  60.                 'cisi.stock_id = cis.stock_id AND cisi.product_id = ce.entity_id',
  61.                 array())
  62.             ->where('e.type_id=?', $this->getTypeId()); ## is this one needed?
  63.  
  64.  
  65.         $productStatusExpr  = $this->_addAttributeToSelect($select, 'status', 'ce.entity_id', 'cs.store_id');
  66.  
  67.         if ($this->_isManageStock()) {
  68.             $stockStatusExpr = new Zend_Db_Expr('IF(cisi.use_config_manage_stock = 0 AND cisi.manage_stock = 0,' . ' 1, cisi.is_in_stock)');
  69.         } else {
  70.             $stockStatusExpr = new Zend_Db_Expr('IF(cisi.use_config_manage_stock = 0 AND cisi.manage_stock = 1,' . 'cisi.is_in_stock, 1)');
  71.         }
  72.         $isInStockExpr = new Zend_Db_Expr("IF({$stockStatusExpr}, 1, 0)");
  73.  
  74.         $isValidChildProductExpr = new Zend_Db_Expr("{$productStatusExpr}");
  75.  
  76.         $select->columns(array(
  77.             'entity_id'         => new Zend_Db_Expr('e.entity_id'),
  78.             'customer_group_id' => new Zend_Db_Expr('pi.customer_group_id'),
  79.             'website_id'        => new Zend_Db_Expr('cw.website_id'),
  80.             'tax_class_id'      => new Zend_Db_Expr('pi.tax_class_id'),
  81.             'orig_price'        => new Zend_Db_Expr('pi.price'),
  82.             'price'             => new Zend_Db_Expr('pi.final_price'),
  83.             'min_price'         => new Zend_Db_Expr('pi.final_price'),
  84.             'max_price'         => new Zend_Db_Expr('pi.final_price'),
  85.             'tier_price'        => new Zend_Db_Expr('pi.tier_price'),
  86.             'base_tier'         => new Zend_Db_Expr('pi.tier_price'),
  87.             'group_price'       => new Zend_Db_Expr('pi.group_price'),
  88.             'base_group_price'  => new Zend_Db_Expr('pi.group_price'),
  89.         ));
  90.  
  91.  
  92.  
  93.         if (!is_null($entityIds)) {
  94.             $select->where('e.entity_id IN(?)', $entityIds);
  95.         }
  96.  
  97.         #Inner select order needs to be:
  98.        #1st) If it's in stock come first (out of stock product prices aren't used if not-all products are out of stock)
  99.        #2nd) Finalprice
  100.        #3rd) $price, in case all finalPrices are NULL. (this gives the lowest price for all associated products when they're all out of stock)
  101.        $sortExpr = new Zend_Db_Expr("${isInStockExpr} DESC, pi.final_price ASC, pi.price ASC");
  102.         $select->order($sortExpr);
  103.  
  104.         /**
  105.          * Add additional external limitation
  106.          */
  107.         Mage::dispatchEvent('prepare_catalog_product_index_select', array(
  108.             'select'        => $select,
  109.             'entity_field'  => new Zend_Db_Expr('e.entity_id'),
  110.             'website_field' => new Zend_Db_Expr('cw.website_id'),
  111.             'store_field'   => new Zend_Db_Expr('cs.store_id')
  112.         ));
  113.  
  114.  
  115.         #This uses the fact that mysql's 'group by' picks the first row, and the subselect is ordered as we want it
  116.        #Bit hacky, but lots of people do it :)
  117.        $outerSelect = $write->select()
  118.             ->from(array("inner" => $select), 'entity_id')
  119.             ->group(array('inner.entity_id', 'inner.customer_group_id', 'inner.website_id'));
  120.  
  121.         $outerSelect->columns(array(
  122.             'customer_group_id',
  123.             'website_id',
  124.             'tax_class_id',
  125.             'orig_price',
  126.             'price',
  127.             'min_price',
  128.             'max_price'     => new Zend_Db_Expr('MAX(inner.max_price)'),
  129.             'tier_price',
  130.             'base_tier',
  131.             'group_price',
  132.             'base_group_price',
  133.             #'child_entity_id'
  134.        ));
  135.  
  136.         $query = $outerSelect->insertFromSelect($this->_getDefaultFinalPriceTable());
  137.         $write->query($query);
  138.         #Mage::log("SCP Price inner query: " . $select->__toString());
  139.        #Mage::log("SCP Price outer query: " . $outerSelect->__toString());
  140.  
  141.         return $this;
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement