Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Remove options in the code
 
wahl
Member
 
Total Posts:  36
Joined:  2009-04-10
France
 

Hello,

I have this code:

$product Mage::getModel('catalog/product')
            ->
setStoreId(Mage::app()->getStore()->getId())
            ->
load($productId);

I need a way to remove an option from $product by the name of the option.
Example product has 2 options text file:
- product::option::text1
- product::option::text2

I need to do something like $product->removeOption(’text1’);
Is there a way to do this?

Even if there’s no magento function to do it automaticly, is it possible to put a null function to:
$product->nameofarray[’nameofoption’] = null; I can’t explain why I need this, but it will help me a lot if somebody has the solution.

Thanks!

 
Magento Community Magento Community
Magento Community
Magento Community
 
wahl
Member
 
Total Posts:  36
Joined:  2009-04-10
France
 

Thanks for your answer, but you didn’t understand what I need.

I need to remove temporaly a custom option in the variable $product, because I use it to display some custom changes on the custom options ^^

By the way when I was looking to a way I found out how to remove for truth custom options…

$options $product->getOptions();
foreach (
$options as $option)
{
    $option
->isDeleted(true);
    
$option->save();
}
This code will delete all custom options for a product (non obligatory option; an error occures for a required custom option but I don’t care to do this).

All I need is to delete an option but only in my variable product, not in the whole product.
Maybe I can make a copy or something like this.

I hope you understand smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
wahl
Member
 
Total Posts:  36
Joined:  2009-04-10
France
 

I joined a print_r of the variable $product. (I let uncommented the main part).

I know the id of the option that I want to unset/remove, I need to do something like this:
unset($product->_options[’ID’]); (in this case the ID is 16).

Can somebody help me to find a solution on how to delete a member of a protected object ?

<?php
Mage_Catalog_Model_Product Object
(
/*
    [_cacheTag:protected] => catalog_product
    [_eventPrefix:protected] => catalog_product
    [_eventObject:protected] => product
    [_canAffectOptions:protected] => 
    [_typeInstance:protected] => 
    [_typeInstanceSingleton:protected] => 
    [_linkInstance:protected] => 
    [_customOptions:protected] => Array
        (
        )
 
    [_urlModel:protected] => 
    [_errors:protected] => Array
        (
        )
 
    [_optionInstance:protected] => Mage_Catalog_Model_Product_Option Object
        (
            [_product:protected] => 
            [_options:protected] => Array
                (
                )
 
            [_valueInstance:protected] => 
            [_values:protected] => Array
                (
                )
 
            [_eventPrefix:protected] => core_abstract
            [_eventObject:protected] => object
            [_resourceName:protected] => catalog/product_option
            [_resource:protected] => 
            [_resourceCollectionName:protected] => catalog/product_option_collection
            [_cacheTag:protected] => 
            [_dataSaveAllowed:protected] => 1
            [_data:protected] => Array
                (
                )
 
            [_origData:protected] => 
            [_idFieldName:protected] => 
            [_isDeleted:protected] => 
        )
*/ 
    
[_options:protected] => Array
        (
            
[16] => Mage_Catalog_Model_Product_Option Object
                
(
                    
[_product:protected] => Mage_Catalog_Model_Product Object
 
*RECURSION*
                    
[_options:protected] => Array
                        (
                        )
 
                    
[_valueInstance:protected] => 
                    
[_values:protected] => Array
                        (
                        )
 
                    
[_eventPrefix:protected] => core_abstract
                    [_eventObject
:protected] => object
                    [_resourceName
:protected] => catalog/product_option
                    [_resource
:protected] => 
                    
[_resourceCollectionName:protected] => catalog/product_option_collection
                    [_cacheTag
:protected] => 
                    
[_dataSaveAllowed:protected] => 1
                    [_data
:protected] => Array
                        (
                            
[option_id] => 16
                            [product_id] 
=> 13
                            [type] 
=> file
                            [is_require] 
=> 1
                            [sku] 
=> 
                            
[max_characters] => 
                            
[file_extension] => 
                            
[image_size_x] => 0
                            [image_size_y] 
=> 0
                            [sort_order] 
=> 0
                            [default_title] 
=> person1
                            [store_title] 
=> 
                            
[title] => person1
                            [default_price] 
=> 0.0000
                            [default_price_type] 
=> fixed
                            [store_price] 
=> 
                            
[store_price_type] => 
                            
[price] => 0.0000
                            [price_type] 
=> fixed
                        
)
 
                    
[_origData:protected] => Array
                        (
                            
[option_id] => 16
                            [product_id] 
=> 13
                            [type] 
=> file
                            [is_require] 
=> 1
                            [sku] 
=> 
                            
[max_characters] => 
                            
[file_extension] => 
                            
[image_size_x] => 0
                            [image_size_y] 
=> 0
                            [sort_order] 
=> 0
                            [default_title] 
=> person1
                            [store_title] 
=> 
                            
[title] => person1
                            [default_price] 
=> 0.0000
                            [default_price_type] 
=> fixed
                            [store_price] 
=> 
                            
[store_price_type] => 
                            
[price] => 0.0000
                            [price_type] 
=> fixed
                        
)
 
                    
[_idFieldName:protected] => option_id
                    [_isDeleted
:protected] => 
                )
 
        )
)
?>
 
Magento Community Magento Community
Magento Community
Magento Community
 
wahl
Member
 
Total Posts:  36
Joined:  2009-04-10
France
 

Thanks for your help I found out. Add this code in app/code/core/Mage/Catalog/Model/Product.php (before copy this php file in the local directory).

public function deleteOptionTitle($optionTitle)
    
{
        $options 
$this->_options;
        foreach (
$options as $option)
        
{
            
if ($option->getTitle() == $optionTitle) :
                unset(
$this->_options[$option->getId()]);
                break ;
            endif;
        
}
        
return $this;
    
}
Just call $product->deleteOptionTitle(’text1’);

I understand now better how objects work, only the function in the product class can access protected variables.
(and I was using a different class to acceed to the product so the options were NULL).

Hope that someday it will help somebody smile

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top