Magento 自动自定义选项

Magento Custom Options Automatically

我做的这个扩展在我保存产品时工作正常。它只是添加了自定义选项。

这里是config.xml

<?xml version="1.0"?>
<config>
<modules>
    <Custom_Options>
        <version>0.0.1</version>
    </Custom_Options>
</modules>
<global>
    <models>
        <custom_options>
            <class>Custom_Options_Model</class>
        </custom_options>
    </models>
</global>
<adminhtml>
    <events>
        <catalog_product_save_before><!-- observe the event -->
            <observers>
                <custom_options>
                    <class>custom_options/observer</class>
                    <method>autoMetaDescription</method>
                </custom_options>
            </observers>
        </catalog_product_save_before>
    </events>
</adminhtml>
</config>

Observer.php

<?php 
class Custom_Options_Model_Observer {
public function autoMetaDescription($observer) {
    $product = $observer->getEvent()->getProduct();

    //check that we haven't made the option already
    $options = $product->hasCustomOptions();
   if( $product->getData('has_options') && ($product->getTypeID() == 'simple')){

} else
 {
 $option6 = array(
        'title' => 'Hardware Finish',
        'type' => 'drop_down',
        'is_require' => 1,
        'sort_order' => 4,
        'is_delete' => '',
        'previous_type' => '',
        'previous_group' => '',
        'price' => '0.00',
        'price_type' => 'fixed',
        'sku' => '',
        'values' => array(
            array(
                'is_delete' => 0,
                'title' => 'Black Nickel',
                'price_type' => 'fixed',
                'price' => 0,
                'option_type_id' => -1,
            )

              );


           //don't use it like this because it has no effect
     //$product->setProductOptions($options);
    $product->setCanSaveCustomOptions(true);

   $product->getOptionInstance()->addOption($option6);

  //don't forget to state that the product has custom options
    $product->setHasOptions(true);
    //$product->save();
}
  }
 }

基本上有些产品没有自定义选项,但它总是会为它们添加选项。我认为有一种方法可以解决这个问题,就是在创建产品时使用调用的操作。 As In this image

请告诉我按下此按钮时调用了哪个操作或控制器,或者任何其他解决此问题的方法。

回答问题

Please tell me which action or controller is called when this button is pressed, or any other method to overcome this problem is appreciated.

要观察产品的初始创建,您可以观察 catalog_product_new_action

听起来您正试图阻止某些产品被您的观察者操纵。如果是这种情况,我建议您创建一个 Yes/No 属性 enables/disables AutoMetaDescription 函数。在操作产品之前检查属性的值。