Created
January 24, 2017 08:36
-
-
Save LogIN-/f629b4e2b7ddebba9e3fe764af2cb128 to your computer and use it in GitHub Desktop.
Magento direct database helper functions for faster speed
This file contains 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 | |
/** | |
* Get array of available product SKUs in the Database | |
* | |
* @param array $skus | |
* | |
* @return array | |
*/ | |
private function _getProductIds($skus) | |
{ | |
$connection = Mage::getSingleton('core/resource')->getConnection('core_read'); | |
$catalog_product_entity = Mage::getSingleton('core/resource')->getTableName('catalog_product_entity'); | |
$select = $connection->select() | |
->from( | |
array('cpe' => $catalog_product_entity), | |
array('cpe.entity_id', 'cpe.sku') | |
) | |
->where('cpe.sku IN (?)', $skus); | |
$stmt = $connection->query($select); | |
$result = array(); | |
while ($cfgLinkRow = $stmt->fetch()) { | |
$result[$cfgLinkRow['sku']] = intval($cfgLinkRow['entity_id']); | |
} | |
return $result; | |
} | |
/** | |
* Change product status (Enable/Disable) on specific store-view ID | |
* | |
* @param int $productId | |
* @param int $storeId | |
* @param int $value | |
* @throws Exception | |
* | |
*/ | |
private function _updateProductStatus($productId, $storeId, $value) | |
{ | |
$statusAttributeId = 96; | |
$statusEntityTypeId = 4; | |
$catalog_product_entity_int = Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_int'); | |
$writeAdapter = Mage::getSingleton('core/resource')->getConnection('core_write'); | |
try { | |
$writeAdapter->insertOnDuplicate( | |
$catalog_product_entity_int, | |
array( | |
'entity_type_id' => $statusAttributeId, | |
'attribute_id' => $statusEntityTypeId, | |
'store_id' => $storeId, | |
'entity_id' => $productId, | |
'value' => $value | |
), | |
array('value') | |
); | |
} catch (Exception $e) { | |
throw $e; | |
} | |
} | |
/** | |
* Change product status on specific store-view | |
* | |
* @param int $productId | |
* @param int $qty | |
* @param int $is_in_stock | |
* @throws Exception | |
* | |
* @return bool $status | |
*/ | |
private function _updateProductInventory($productId, $qty, $is_in_stock) | |
{ | |
$status = false; | |
$cataloginventory_stock_item = Mage::getSingleton('core/resource')->getTableName('cataloginventory/stock_item'); | |
$cataloginventory_stock_status = Mage::getSingleton("core/resource")->getTableName('cataloginventory/stock_status'); | |
$writeAdapter = Mage::getSingleton('core/resource')->getConnection('core_write'); | |
$sql = "UPDATE $cataloginventory_stock_item AS item_stock, | |
$cataloginventory_stock_status AS status_stock | |
SET item_stock.qty = $qty, | |
item_stock.is_in_stock = $is_in_stock, | |
status_stock.qty = $qty, "; | |
if ($qty === 0 && $is_in_stock === 0) { | |
$low_stock_date = new DateTime(); | |
$low_stock_date = $low_stock_date->format('Y-m-d'); | |
$sql .= " item_stock.low_stock_date = $low_stock_date, "; | |
} | |
$sql .= " status_stock.stock_status = $is_in_stock | |
WHERE item_stock.product_id = $productId | |
AND item_stock.product_id = status_stock.product_id"; | |
try{ | |
$writeAdapter->query($sql); | |
$status = true; | |
}catch(exception $e){ | |
throw $e; | |
} | |
return $status; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment