Created
April 15, 2013 06:02
-
-
Save jaseclamp/5386006 to your computer and use it in GitHub Desktop.
Migrate data from pinnacle cart to magento. Put script in magento root and update db connection details. This script migrates product relationships / recommendations. It's the third out of three scripts because you need the products to be there before relating them.
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 | |
require_once 'app/Mage.php'; | |
Mage::app(0); // best to leave as default as prods are created in admin and you can apply to stores as needed | |
//connect to your pinnacle cart database | |
mysql_connect("host", "user", "pass") or die(mysql_error()); | |
//your pinnacle cart database name | |
mysql_select_db("db") or die(mysql_error()); | |
//collect product and brand information and create a magento product field array. | |
//This query could also be used to generate a CSV import file. | |
//note the below assumes you're not managing stock. | |
$query = " | |
SELECT replace(p.`product_id`,',','') as sku, | |
'1' as store, | |
'Default' as attribute_set, | |
'simple' as type, | |
'' as category, | |
'' as root_category, | |
'base' as websites, | |
'' as color, | |
'' as cost, | |
'' as country_of_manufacture, | |
p.`added` as created_at, | |
'' as custom_design, | |
'' as custom_design_from, | |
'' as custom_design_to, | |
'' as custom_layout_update, | |
p.`description` as description, | |
'1' as enable_googlecheckout, | |
'' as gallery, | |
'' as gift_message_available, | |
'0' as has_options, | |
concat('/',replace(lower(p.`product_id`),'/','_'),'.jpg') as image, | |
'' as image_label, | |
'' as manufacturer, | |
'' as media_gallery, | |
p.`meta_description` as meta_description, | |
p.`meta_keywords` as meta_keyword, | |
p.`meta_title` as meta_title, | |
'' as minimal_price, | |
'' as msrp, | |
'Use config' as msrp_display_actual_price_type, | |
'Use config' as msrp_enabled, | |
p.`title` as name, | |
'' as news_from_date, | |
'' as news_to_date, | |
'Block after Info Column' as options_container, | |
'' as page_layout, | |
p.`price2` as price, | |
'0' as required_options, | |
p.`overview` as short_description, | |
concat('/',replace(lower(p.`product_id`),'/','_'),'.jpg') as small_image, | |
'' as small_image_label, | |
'' as special_from_date, | |
p.`price` as special_price, | |
'2015-12-31 00:00:00' as special_to_date, #note this is an arbitrary date | |
replace(replace( | |
p.`is_visible`,'Yes','Enabled'), | |
'No','Disabled') as status, | |
'Taxable Goods' as tax_class_id, | |
'' as thumbnail, | |
'' as thumbnail_label, | |
now() as updated_at, | |
lower( | |
concat( | |
replace(replace(replace(replace(replace(replace(replace( | |
p.`title`,' ','-'), | |
'(',''), | |
')',''), | |
'/',''), | |
',',''), | |
'&',''), | |
'--','-'), | |
'-' ,p.`pid` )) as url_key, #note that you'll have to strip the '-cid' from inline links later | |
lower( | |
concat( | |
replace(replace(replace(replace(replace(replace(replace( | |
p.`title`,' ','-'), | |
'(',''), | |
')',''), | |
'/',''), | |
',',''), | |
'&',''), | |
'--','-'), | |
'-' ,p.`pid`, '.html' )) as url_path, | |
'Catalog, Search' as visibility, | |
p.`weight` as weight, | |
'0.0000' as qty, #we're adjusting this later from our ERP system | |
'0.0000' as min_qty, | |
'1' as use_config_min_qty, | |
'0' as is_qty_decimal, | |
'0' as backorders, | |
'1' as use_config_backorders, | |
'1.0000' as min_sale_qty, | |
'1' as use_config_min_sale_qty, | |
'0.0000' as max_sale_qty, | |
'1' as use_config_max_sale_qty, | |
'1' as is_in_stock, | |
'' as notify_stock_qty, | |
'1' as use_config_notify_stock_qty, | |
'0' as manage_stock, | |
'1' as use_config_manage_stock, | |
'1' as stock_status_changed_auto, | |
'1' as use_config_qty_increments, | |
'0,0000' as qty_increments, | |
'1' as use_config_enable_qty_inc, | |
'0' as enable_qty_increments, | |
'0' as is_decimal_divided, | |
'' as links_related_sku, | |
'' as links_related_position, | |
'' as links_crosssell_sku, | |
'' as links_crosssell_position, | |
'' as links_upsell_sku, | |
'' as links_upsell_position, | |
'' as associated_sku, | |
'' as associated_default_qty, | |
'' as associated_position, | |
'' as tier_price_website, | |
'' as tier_price_customer_group, | |
'' as tier_price_qty, | |
'' as tier_price_price, | |
'' as group_price_website, | |
'' as group_price_customer_group, | |
'' as group_price_price, | |
'' as media_attribute_id, | |
'' as media_image, | |
'' as media_lable, | |
'' as media_position, | |
'' as media_is_disabled, | |
m.`manufacturer_name` as manufacturer, | |
p.`pid` as id #note this is not a magento field and can be removed for csv | |
FROM `products` p | |
left join `manufacturers` m on m.`manufacturer_id` = p.`manufacturer_id` | |
#WHERE p.`is_active` = 'Yes' | |
#WHERE p.`pid` = '111' | |
order by p.`pid` ASC"; | |
//uncomment the above to leave out disabled products | |
$result = mysql_query( $query ) or die(mysql_error()); | |
echo "<pre>"; | |
while ($prod = mysql_fetch_assoc($result)) { | |
$product = Mage::getModel('catalog/product')->loadByAttribute( 'sku', $prod['sku'] ); | |
echo "Working on: ".$prod['sku']." <Br> "; | |
//set associated products... | |
$result2 = mysql_query(" | |
SELECT replace(p.`product_id`,',','') as sku FROM `products_families_dependencies` d right join products_families_content f on f.pf_id = d.pf_id | |
left join products p on p.pid = f.pid | |
WHERE d.`pid` = '".$prod['id'] ."';") or die(mysql_error()); | |
$c=0; | |
if (mysql_num_rows($result2)) while($opts = mysql_fetch_assoc( $result2 ) ) { | |
$c++; | |
$params[ $product->getIdBySku($opts['sku']) ] = array( | |
'position'=>$c | |
); | |
} | |
if($params) { | |
//we're setting old related products as both related prods and cross sells | |
$product->setRelatedLinkData($params); | |
$product->setCrossSellLinkData($params); | |
try { | |
//if (!$product->getId()) $product->save(); | |
//else $product->getResource()->save($product); | |
$product->save(); | |
echo "Loaded associations for: ".$prod['sku']."<br>\n"; | |
} | |
catch (Exception $ex) { | |
echo "err issue with: ".$prod['sku']."<br>\n"; | |
//print_r($ex); | |
//die; | |
} | |
} | |
unset($product,$params,$result2,$opts); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment