Posted By

chadanuk on 10/25/13

Tagged

Viewed 1 times

Magento - force associate simple products to configurables by sku and option to dewlete un-categorised products


/ Published in: PHP

This will only run in the admin

There are 2 booleans to implement the script:
$run_script = true;
$delete = false;

If delete is true then products that are not within a category will be removed.

  1. Mage::getSingleton('core/session', array('name' => 'adminhtml'))->start();
  2. $run_script = true;
  3. $delete = false;
  4. $admin_logged_in = Mage::getSingleton('admin/session', array('name' => 'adminhtml'))->isLoggedIn();
  5. $admin_logged_in = (stristr(Mage::helper('core/url')->getCurrentUrl(), 'storeadmin') !== FALSE) ? $admin_logged_in : FALSE;
  6. // ..get back to the original.
  7. if($run_script && $admin_logged_in)
  8. {
  9. $wr = Mage::getSingleton('core/resource')->getConnection('core_write');;
  10. echo '<hr />Starting products script<hr />';
  11. $ps = Mage::getModel('catalog/product')->getCollection()->load();
  12.  
  13.  
  14. $count = 0;
  15. foreach ($ps as $p) {
  16. $ti = $p->getTypeID();
  17. $ci = $p->getCategoryIds();
  18.  
  19.  
  20. if($p->getTypeID() == 'configurable')
  21. {
  22. $a = Mage::getModel('eav/entity_attribute_set')->load($p->getAttributeSetId());
  23. $attributeSetName = $a->getAttributeSetName();
  24. Mage::getResourceModel('eav/entity_attribute_set_collection')->setEntityTypeFilter('configurable')->addFilter('attribute_set_name', $attributeSetName);
  25.  
  26. $attributes = $p->getTypeInstance(true)->getSetAttributes($p);;
  27. foreach($attributes as $attribute)
  28. {
  29.  
  30. if ($p->getTypeInstance(true)->canUseAttribute($attribute, $p)) {
  31.  
  32.  
  33. try {
  34. $s = "INSERT INTO wf_catalog_product_super_attribute (`product_id`, `attribute_id`) VALUES ('".$p->getId()."', '".$attribute['attribute_id']."')";
  35. $wr->query($s);
  36. $lastInsertId = $wr->lastInsertId();
  37.  
  38. $s = "INSERT INTO wf_catalog_product_super_attribute_label (`product__super_attribute_id`, `value`) VALUES ('".$lastInsertId."', '".$attribute->getStoreLabel()."')";
  39. $wr->query($s);
  40.  
  41. echo $attribute->getStoreLabel().'associated to product '.$p->getName().'<br />' ;
  42.  
  43. } catch (Exception $e) {
  44.  
  45. }
  46. }
  47. }
  48.  
  49.  
  50. // Check all products for the name of the item
  51. $cfg_prs = Mage::getModel('catalog/product')->getCollection()
  52. ->addAttributeToFilter('type_id', 'simple')
  53. ->addAttributeToFilter('sku', array('like' => $p->getSku().'-%'));
  54.  
  55. foreach ($cfg_prs as $_cp)
  56. {
  57. try {
  58.  
  59. $s = "INSERT INTO wf_catalog_product_super_link (`parent_id`, `product_id`) VALUES ('".$p->getId()."', '".$_cp->getId()."')";
  60. $wr->query($s);
  61. echo $_cp->getSku().' associated correctly<br />';
  62. } catch (Exception $e) {
  63.  
  64. }
  65. }
  66.  
  67. }
  68. // Delete
  69. if($delete && empty($ci) && $p->getTypeID() == 'configurable')
  70. {
  71. $cps = $p->getUsedProducts(null, $product);
  72. foreach ($cps as $cp) {
  73. echo $cp->getName().' deleted<br />';
  74. $cp->delete();
  75. $count++;
  76. }
  77. echo $p->getName().' deleted<br />';
  78. $p->delete();
  79. $count++;
  80. }
  81. }
  82. echo '<hr />Finishing products script. '.$count.' product(s) deleted<hr />';
  83. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.