Return to Snippet

Revision: 65112
at October 25, 2013 19:07 by chadanuk


Initial Code
Mage::getSingleton('core/session', array('name' => 'adminhtml'))->start();
$run_script = true;
$delete = false;
$admin_logged_in = Mage::getSingleton('admin/session', array('name' => 'adminhtml'))->isLoggedIn();
$admin_logged_in = (stristr(Mage::helper('core/url')->getCurrentUrl(), 'storeadmin') !== FALSE) ? $admin_logged_in : FALSE;
// ..get back to the original.
if($run_script && $admin_logged_in)
{
    $wr = Mage::getSingleton('core/resource')->getConnection('core_write');;
    echo '<hr />Starting products script<hr />';
    $ps = Mage::getModel('catalog/product')->getCollection()->load();


    $count = 0;
    foreach ($ps as $p) {
        $ti = $p->getTypeID();
        $ci = $p->getCategoryIds();


        if($p->getTypeID() == 'configurable')        
        {
            $a = Mage::getModel('eav/entity_attribute_set')->load($p->getAttributeSetId());
            $attributeSetName = $a->getAttributeSetName();
            Mage::getResourceModel('eav/entity_attribute_set_collection')->setEntityTypeFilter('configurable')->addFilter('attribute_set_name', $attributeSetName);

            $attributes = $p->getTypeInstance(true)->getSetAttributes($p);;
            foreach($attributes as $attribute)
            {

                if ($p->getTypeInstance(true)->canUseAttribute($attribute, $p)) {

                    
                    try {
                        $s = "INSERT INTO wf_catalog_product_super_attribute (`product_id`, `attribute_id`) VALUES ('".$p->getId()."', '".$attribute['attribute_id']."')";
                        $wr->query($s);
                        $lastInsertId = $wr->lastInsertId();

                        $s = "INSERT INTO wf_catalog_product_super_attribute_label (`product__super_attribute_id`, `value`) VALUES ('".$lastInsertId."', '".$attribute->getStoreLabel()."')";   
                        $wr->query($s);

                        echo $attribute->getStoreLabel().'associated to product '.$p->getName().'<br />' ;
                     
                    } catch (Exception $e) {
                        
                    }
                }
            }

            
            // Check all products for the name of the item
            $cfg_prs = Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToFilter('type_id', 'simple')
            ->addAttributeToFilter('sku', array('like' => $p->getSku().'-%'));
            
             foreach ($cfg_prs as $_cp) 
             {
                try {
                    
                    $s = "INSERT INTO wf_catalog_product_super_link (`parent_id`, `product_id`) VALUES ('".$p->getId()."', '".$_cp->getId()."')";
                    $wr->query($s);
                    echo $_cp->getSku().' associated correctly<br />';
                } catch (Exception $e) {
            
                }
             }

        }
        // Delete
        if($delete && empty($ci) && $p->getTypeID() == 'configurable')
        {
            $cps = $p->getUsedProducts(null, $product);
            foreach ($cps as $cp) {
                echo $cp->getName().' deleted<br />';
                $cp->delete();
                $count++; 
            }
            echo $p->getName().' deleted<br />';
            $p->delete();
            $count++;
        }
    }
    echo '<hr />Finishing products script. '.$count.' product(s) deleted<hr />';
}

Initial URL


Initial Description
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.

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

Initial Tags


Initial Language
PHP