Irfan Ahmed
Irfan Ahmed

Reputation: 23

Magento remove bundle item using API

I am looking for an API that can remove bundle items added in a main bundle product.

Can some one guide me to that API?

regards, Irfan

Upvotes: 1

Views: 1920

Answers (3)

eric ramahatra
eric ramahatra

Reputation: 165

above does not work , Bundled items inside a bundle product are treated differently. There are inside Options and are called Selections.

    // get all the options of your bundle product assumed as $bundle
    $optionCollection = $bundle->getTypeInstance()->getOptionsCollection($bundle);
    $selectionIds = $optionIds = array();
    // and their Id into an array
    foreach($optionCollection as $opt) {
        $optionIds[] = $opt->getOptionId();
    }
    // fetch all the selections from all the previous Ids
    $selectionCollection = $bundle->getTypeInstance()->getSelectionsCollection($optionIds);
    foreach($selectionCollection as $sc) {
        if ($sc->getId()!=$itemToRemoveId) $selectionIds[] = $sc->getSelectionId();
    }

    // remove the Selection/Bundle association from database, we need to pass all the others except the one we need to drop
    Mage::getModel('bundle/mysql4_bundle')->dropAllUnneededSelections($bundle->getId(), $selectionIds);

Another Easyier way is to Delete your Item from the bundle/Selection table :

$sql = "DELETE FROM " . $this->getTable('bundle/selection') . " WHERE 'product_id' = " . $YOUR_ITEM_TO_REMOVE_ID ;

Upvotes: 1

Benjamin Powers
Benjamin Powers

Reputation: 3256

Something like this should work:

    $bundled_product = Mage::getModel('catalog/product');
    $bundled_product->load($product->getId());

    $optionCollection = $product->getTypeInstance(true)->getOptionsCollection($product);
    $selectionCollection = $bundled_product
    ->getTypeInstance(true)
    ->getSelectionsCollection(
    $bundled_product
    ->getTypeInstance(true)
    ->getOptionsIds($bundled_product),
    $bundled_product
    );
    foreach($optionCollection as $option){
        $option->delete();
    }

Upvotes: 0

B00MER
B00MER

Reputation: 5491

If your just trying to remove a simple product from a configurable product:

http://www.magentocommerce.com/wiki/doc/webservices-api/api/catalog_product#catalog_product.delete

Upvotes: 0

Related Questions