doulu2011 2015-12-15 22:55
浏览 37
已采纳

Magento自定义选项

This extension I made works fine when I save the product. It simply adds the custom options.

Here is 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();
}
  }
 }

Basically there are some products which doesn't have custom options but it always adds options to them too. I think there's a way to overcome this problem is to use action called when creating a product. 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.

  • 写回答

1条回答 默认 最新

  • doushi5752 2015-12-16 02:41
    关注

    Answering the question

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

    To observe the initial creation of a product you can observe the catalog_product_new_action.

    It sounds like you're trying to prevent certain products from being manipulated by your observer. If this is the case, I would recommend that you create an attribute that is Yes/No that enables/disables the the AutoMetaDescription function. Just check the value of the attribute before manipulating the product.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题