0

I need to convert bundle products to configurable products using a required bundle field as the configurable product. It seemed like my script had worked. It converts the product type to a configurable and correctly makes the associations to it. However, now when i try to add to cart i get the following :

Fatal error: Call to undefined method Mage_Catalog_Model_Product_Type_Simple::getConfigurableAttributes() in /vagrant/public/app/code/core/Mage/Catalog/Model/Product/Type/Configurable/Price.php on line 77

My script is a bit dirty because the data isn't that straight forward and i've been frantically trying to get it to work (hence resorting to direct sql queries) can anyone see why, during the process of converting to a configurable and making the associations something might cause the above ?

I have obviously checked that data, the configurable products do have type_id configurable and the associated products are simples.

The first part of the script isn't that important - this updates all of my simple products to have the required attribute data i.e. shirt_size = XL

The function convertToConfigurable() is where something is going wrong.

$bundle_products = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('type')
    ->addFieldToFilter('type_id', array('eq' => 'bundle'))
->addFieldToFilter('has_options',array('eq' => true));

$bundle_options = array();

foreach ($bundle_products as $product) {

    $optionCollection = $product->getTypeInstance()->getOptionsCollection();
    $selectionCollection = $product->getTypeInstance()->getSelectionsCollection($product->getTypeInstance()->getOptionsIds());
    $options = false;
    if($optionCollection) {
        $options = $optionCollection->appendSelections($selectionCollection);
    }

    if($options) {

        $attribute = false;
        $attributeCode = false;
        $attribute_ids = array();
        $associatedProducts = array();
        $productIds = array();

        foreach ($options as $option) {

            if ($option->getData('required')) {

                $_selections = $option->getSelections();

                foreach ($_selections as $selection) {

                    $shortname = $selection->getShortName();

                    if ($shortname) {
                        $attributeCode = getAttributeCode(
                            $option->getDefaultTitle(), $attribute_map,
                            $shortname
                        );

                        $attribute = Mage::getModel('eav/entity_attribute')
                            ->loadByCode(4, $attributeCode);


                        if ($attribute) {

                            $collection = Mage::getResourceModel(
                                'eav/entity_attribute_option_collection'
                            )
                                ->setAttributeFilter($attribute->getId())
                                ->setStoreFilter(
                                    Mage_Core_Model_App::ADMIN_STORE_ID
                                )
                                ->addFieldToFilter('tdv.value', $shortname);


                            // Update simple product with correct configurable sizing attribute
                            if ($collection->getSize() > 0) {


                                $optionId = $collection->getFirstItem()->getId(
                                );
                                $productId = $selection->getProductId();

                                $productIds[] = $productId;

                                Mage::getModel("catalog/product_action")
                                    ->updateAttributes(
                                        array($productId),
                                        array($attributeCode => $optionId), 0
                                    );

                                $associatedProducts[$attribute->getId()][] = $productId;


                            }

                        }
                    }
                }

            }

        }



        if ($attribute && $associatedProducts && $product) {
            convertToConfigurable($product, $associatedProducts, $productIds, $attributeCode, $attribute_data);

        }

    }

}

function getAttributeCode($default_title, $attribute_map, $shortname) {

    $default_title = strtolower($default_title);
    $formatted_title = str_replace(' ', '_', $default_title);

    $code = $attribute_map[$formatted_title];

    // size is a bit jumbled so let's split it into letter and number sizes
    if ($code == 'size') {

        if (preg_match("/[a-z]/i", $shortname)) {
            $code = 'size_letters';
        } else {
            $code = 'size_numbers';
        }

    }

    return $code;
}

function convertToConfigurable($product, $associatedProducts, $productIds, $code, $attribute_data) {

    $attributeIds = array();
    $allProductIds = array();


//    $query = "UPDATE catalog_product_entity SET type_id = 'configurable' where entity_id =" . $product->getEntityId();

   // getWriteConnection()->query($query);

    $configProduct = Mage::getModel('catalog/product')->load($product->getEntityId());

    $configProduct->setTypeId('configurable');

    $configProduct->setCanSaveConfigurableAttributes(true);

    $configProduct->save();



    foreach($associatedProducts as $attbiuteId => $products)
    {


        $writeConnection =  getWriteConnection();
        $currentAttributeId = $attbiuteId;
        $query = 'DELETE FROM catalog_product_relation WHERE parent_id = '.$product->getEntityId() .';';
        $query .= 'INSERT INTO catalog_product_super_attribute VALUES (NULL,'.$product->getEntityId().','.$currentAttributeId.',0);';
        $writeConnection->query($query);

        $lastInsert = getLastInsertId('catalog_product_super_attribute', 'product_super_attribute_id');

        $frontendLabel = $attribute_data[$code]['frontend'];
        $query = 'INSERT INTO catalog_product_super_attribute_label VALUES (NULL,'.$lastInsert.',0,0,"'.$frontendLabel.'");';

        $writeConnection->query($query);
        $query = '';

        foreach($products as $productId) {
            $query .= 'INSERT INTO catalog_product_super_link VALUES (NULL,'.$productId.','.$product->getEntityId().');';
        }

        $writeConnection->query($query);

    }
0

If anyone else has similar issues i found that there was a left over price_type value of 0. This is part of the bundle fixed vs dynamic pricing functionality. Remove this value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.