Created
June 27, 2013 15:46
-
-
Save Modicrumb/5877588 to your computer and use it in GitHub Desktop.
rest of it
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
foreach ( $prodIds as $productId ) { | |
//echo '. '; | |
//flush(); | |
//echo 'Loop start: '.memory_get_usage(false).'<br>'; | |
//flush(); | |
$product = Mage::getModel('catalog/product')->load ( $productId ); | |
//check stock info so we dont add a product thats out of stock | |
//also check if upc is valid | |
$qty=number_format($product->getData('stock_item')->getData('qty'),0); | |
$instock=$product->getData('stock_item')->getData('is_in_stock'); | |
$upc = $product->getData ('upc'); | |
if($instock && $qty>0 && is_upca($upc) && $product->getDescription () && $product->getImage()) | |
{ | |
$product_data = array (); | |
$product_data ['sku'] = $product->getSku (); | |
$product_data ['title'] = $product->getName (); | |
$product_data ['description'] = $product->getDescription (); | |
$product_data ['link'] = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB).$product->getUrlPath(); | |
//image handling | |
if ($product->getImage ()) | |
{ | |
$product_data ['image_link'] = Mage::getBaseUrl ( Mage_Core_Model_Store::URL_TYPE_MEDIA ) . 'catalog/product' . $product->getImage (); | |
} | |
else | |
{ | |
$product_data ['image_link'] = ''; | |
} | |
$product_data ['price'] = number_format ( $product->getPrice (), 2 ); | |
$product_data ['brand'] = $product->getResource ()->getAttribute ( 'manufacturer' )->getFrontend ()->getValue ( $product ); | |
$product_data ['product_type'] = ''; | |
$product_data ['condition'] = 'New'; | |
if($product->getSpecialPrice()>0) | |
{ | |
$product_data ['specialprice'] = number_format ( $product->getSpecialPrice (), 2 ); | |
} | |
else | |
{ | |
$product_data ['specialprice'] = number_format ( $product->getPrice (), 2 ); | |
} | |
$product_data ['availability'] = "in stock"; | |
$product_data ['mpn'] = $product->getSku (); | |
$product_data ['gtin'] = $product->getData ('upc'); | |
$product_data ['google_product_category'] = ""; | |
$_cat=Mage::getModel('catalog/product')->load($product->getId())->getCategoryIds(); | |
$_cat=$_cat[count($_cat)-1]; | |
$_cat=Mage::getModel('catalog/category')->load($_cat); | |
$category=explode('/', $_cat->getData('path')); | |
$category = Mage::getModel('catalog/category')->load($category[2])->getName(); | |
$cids=$product->getCategoryIds(); | |
foreach($cids as $cid) | |
{ | |
$tmp=Mage::getModel('catalog/category')->load($cid); | |
if($tmp->getData("level")==3) | |
{ | |
$subcategorylink=$tmp->getData('url_key'); | |
$subcategory=$tmp->getName(); | |
break; | |
} | |
} | |
$extendeddescription=Mage::getModel('cms/block')->load('extended_description')->getLayout(); | |
$extendeddescription=str_ireplace('[by manufacturer]', " by ".$product->getAttributeText("manufacturer"), $extendeddescription); | |
$extendeddescription=str_ireplace('[name]', $product->getName(), $extendeddescription); | |
$extendeddescription=str_ireplace('[parentsubcatlink]', $subcategorylink, $extendeddescription); | |
$extendeddescription=str_ireplace('[category]', $category, $extendeddescription); | |
$extendeddescription=str_ireplace('[sku]', $product->getSku(), $extendeddescription); | |
$extendeddescription=str_ireplace('[parentsubcat]', $subcategory, $extendeddescription); | |
$product_data ['description'].=$extendeddescription; | |
//$product_data ['color'] = $product->getResource()->getAttribute('color')->getFrontend()->getValue($product); | |
//echo 'Product load: '.memory_get_usage(false).'<br>'; | |
//flush(); | |
//get the product categories | |
//echo 'Category load: '.(memory_get_usage(false)).'<br>'; | |
$biggestlvl=0; | |
foreach ( $product->getCategoryIds () as $cid ) | |
{ | |
$category = Mage::getModel ( 'catalog/category' )->load ( $cid ); | |
$level=$category->getData('level'); | |
if($level>$biggestlvl) | |
{ | |
$biggestlvl=$level; | |
$id=$cid; | |
} | |
} | |
$category = Mage::getModel ( 'catalog/category' )->load ( $id ); | |
$path=explode("/",str_ireplace("1/2/","",$category->getPath())); | |
foreach($path as $id) | |
{ | |
$category = Mage::getModel ( 'catalog/category' )->load ( $id ); | |
$name=$category->getName(); | |
$product_data ['product_type'].=$name." > "; | |
} | |
$product_data ['product_type']=rtrim($product_data ['product_type']," > "); | |
$parent=$path[0]; | |
if ($parent ==47) | |
{ | |
$product_data ['google_product_category'] = "Home & Garden > Pool & Spa > Pool & Spa Accessories > Pool Cleaners & Chemicals"; | |
} | |
else if ($parent ==3 || $parent==56 || $parent==11) | |
{ | |
$product_data ['google_product_category'] = "Home & Garden > Pool & Spa > Pool & Spa Accessories"; | |
} | |
else if ($parent ==22) | |
{ | |
$product_data ['google_product_category'] = "Home & Garden > Pool & Spa > Pool & Spa Accessories > Pool Toys"; | |
} | |
else if ($parent ==78) | |
{ | |
$product_data ['google_product_category'] = "Home & Garden > Pool & Spa > Saunas"; | |
} | |
else | |
{ | |
$product_data ['google_product_category'] = "Home & Garden > Pool & Spa"; | |
} | |
//sanitize data | |
foreach ( $product_data as $k => $val ) { | |
$bad = array ('"', "\r\n", "\n", "\r", "\t" ); | |
$good = array ("", " ", " ", " ", "" ); | |
$product_data [$k] = str_replace ( $bad, $good, $val ); | |
} | |
$feed_line = implode ( "\t", $product_data ) . "\r\n"; | |
if($product_data ['price']>0) | |
{ | |
fwrite ( $handle, $feed_line ); | |
$aGAPITracker["products"]["ingooglebase"]++; | |
} | |
fflush ( $handle ); | |
} | |
//echo 'Loop end: '.memory_get_usage(false).'<br>'; | |
//flush(); | |
} | |
//---------------------- WRITE THE FEED | |
fclose ( $handle ); | |
} catch ( Exception $e ) { | |
die ( $e->getMessage () ); | |
} | |
//mail("gvaught@mail.turtlehut.com", "Google Shopping", "Google shopping script should be done running. Google will load it at 8pm. Remind me to turn off this automated message."); | |
die(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment