Skip to content

Instantly share code, notes, and snippets.

@eyemagine2
Last active August 29, 2015 14:05
Show Gist options
  • Save eyemagine2/512ca4286ec019a9a103 to your computer and use it in GitHub Desktop.
Save eyemagine2/512ca4286ec019a9a103 to your computer and use it in GitHub Desktop.
<?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