Last active
August 29, 2015 14:05
-
-
Save eyemagine2/512ca4286ec019a9a103 to your computer and use it in GitHub Desktop.
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 | |
protected $_attrMap = array( | |
'sku' => 'BaseId', | |
'description' => 'Description', | |
'short_description' => 'ExpWebDesc', | |
'msrp' => 'MSRP', | |
// etc | |
); | |
public function run() | |
{ | |
$this->_initDb(); | |
$stmt = $this->_db->query('SELECT * FROM product LIMIT 10'); | |
while ($attributes = $stmt->fetch(PDO::FETCH_ASSOC)) { | |
$attrStmt = $this->_db->prepare('SELECT AttrId, Value | |
FROM prodatt | |
WHERE ProductId = ?'); | |
$attrStmt->execute(array($attributes['ProductId'])); | |
while ($attr = $attrStmt->fetch(PDO::FETCH_ASSOC)) { | |
$attributes[$attr['AttrId']] = $attr['Value']; | |
} | |
$productId = Mage::getSingleton('catalog/product')->getIdBySku($attributes[$this->_attrMap['sku']]); | |
if ($productId && $productId > 0) { | |
$this->updateProduct($productId, $attributes); | |
} else { | |
$this->createProduct($attributes); | |
} | |
} | |
} | |
public function updateProduct($productId, array $attributes) | |
{ | |
$product = Mage::getModel('catalog/product')->load($productId); | |
$product = $this->_setAttributeValues($product, $attributes); | |
$product->save(); | |
} | |
public function createProduct(array $attributes) | |
{ | |
$product = Mage::getModel('catalog/product'); | |
$product = $this->_setAttributeValues($product, $attributes); | |
$product->save(); | |
} | |
protected function _setAttributeValues(Mage_Catalog_Model_Product $product, array $attributes) | |
{ | |
foreach ($this->_attrMap as $mageCode => $key) { | |
switch ($mageCode) { | |
// handle special attributes | |
case 'type_id': | |
$product->setTypeId(psuedoGetType($attributes[$key])); | |
break; | |
// cases for stock items, selects, Y/N -> 1/0, etc | |
default: | |
$product->setData($mageCode, $attributes[$key]); | |
} | |
} | |
return $product; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment