4

I have a simple need which is that I hae a custom product type and that product needs options attached to it. I need these options to be editable and removable so I'm going to use what is built in. I have the observer working and the method is being fired. When I do a var_dump($product);die(); I can search for the title of the sample custom option and find that it is apart of the output. When I get to the new product page I am not seeing the options. This is what I have.

public function catalog_product_new_action($observer) {
    //check that we haven't made the option already
    $product = $observer->getEvent()->getProduct();
    $options = $product->getOptions();
    if ($options){
        foreach ($options as $option) {
            if ($option['title'] == 'Auto Date & Time' && $option['type'] == 'date_time' && !$option['is_delete']) {
                //we've already added the option
                return;
            }
        }
    }
    $option = array(
        'title' => 'Auto Date & Time',
        'type' => 'date_time',
        'is_require' => 1,
        'sort_order' => 0,
        'is_delete' => '',
        'previous_type' => '',
        'previous_group' => '',
        'price' => '0.00',
        'price_type' => 'fixed',
        'sku' => ''
    );
    $product->setProductOptions(array($option));
    $product->setCanSaveCustomOptions(true);
    $product->setHasOptions(true);
    $product->save();


}

It is close, but it's not displaying on the back end. What I don't want to do is force a user to save the product and then auto generate the options after the fact, so I'm not wanting to use any of the on saves events and why I am using that catalog_product_new_action event

Update It is worth noting that the save ( $product->save(); ) will save an empty product but it will not be used when you are landing on the next leg of a new product after choosing the type ( admin/product/new/type/#/ ).

2

The event catalog_product_new_action is triggered when this page is loaded:

New Product

On this request no product gets created! The $product object is only for temporary use, that's why you get an empty product when you save it.

But the action of this form is adminhtml/catalog_product/save, will save the product with id, type and attribute set and redirect you to the edit page. So if you observe the event catalog_product_save_before you will actually see the custom options immediately on the newly created product.

To prevent creating them again on subsequent saves, use isObjectNew():

if ($product->isObjectNew()) {
    // create custom options
}

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.