【问题标题】:how to create an ubercart product content type within a module如何在模块中创建 ubercart 产品内容类型
【发布时间】:2011-03-14 00:08:26
【问题描述】:

我想从模块中创建产品内容类型。我按照this 非常有用的指南以编程方式创建内容类型。现在我该如何“产品化”它?

如果已经存在我可以用来学习的模块,请指出它的方向。或者也许有一个指南漂浮在某个地方?

谢谢!

【问题讨论】:

    标签: drupal drupal-6 ubercart


    【解决方案1】:

    我想通了。显然,如果您正在创建一个也是 ubercart 产品类的内容类型,您不能简单地按照我上面链接的教程然后“附加” ubercart 的东西。根据上面的教程,你需要实现以下钩子来从你的模块中创建一个内容类型:

    • hook_info()
    • hook_perm()
    • hook_access()
    • hook_form()
    • hook_help()

    要创建一个也是产品类的内容类型,您需要对上面的列表进行以下修改:

    • 删除 hook_info()。不知道为什么这会导致问题,但确实如此。
    • 照常使用 hook_perm()、hook_access()、hook_form() 和 hook_help()。
    • 使用 hook_enable()(在启用模块时触发),并包含以下代码:

      function uc_yourmodule_enable() {
        db_query("INSERT INTO {uc_product_classes} (pcid, name, description) 
                  VALUES ('%s', '%s', '%s')", 
                  'product_class_id', 
                  'Product Class Name', 
                  'Product Class Description.');
      
        node_types_rebuild();
      }
      

    如您所见,sn-p 向 uc_product_classes 表添加了一个条目,我想这就是 ubercart 所需要的。

    最后,我还在我的模块中实现了一个特定于 ubercart 的钩子:hook_product_types()

    我只是在进行过程中弄清楚这一点,所以我很高兴收到更正或建议。

    【讨论】:

      【解决方案2】:

      我只是想出来,这似乎工作正常,不幸的是api不以官方方式支持这一点。

          function create_uc_product_type ( $name , $pcid , $description )
           {
      
           $pcid = preg_replace ( array ( '/\s+/' , '/\W/' ) , array ( '_' , '' ) , strtolower ( $pcid ) );
      
      
          db_query ( "INSERT INTO {uc_product_classes} (pcid, name, description) VALUES ('%s', '%s', '%s')" , $pcid , $name , $description );
          uc_product_node_info ( TRUE );
          variable_set ( 'node_options_' . $pcid , variable_get ( 'node_options_product' , array ( 'status' , 'promote' ) ) );
      
          if ( module_exists ( 'comment' ) ) {
              variable_set ( 'comment_' . $pcid , variable_get ( 'comment_product' , COMMENT_NODE_READ_WRITE ) );
          }
      
          module_invoke_all ( 'product_class' , $pcid , 'insert' );
      
          if ( module_exists ( 'imagefield' ) ) {
              uc_product_add_default_image_field ( $pcid );
          }
      
      
      
          $type = node_get_types('type', $pcid);
          $type->custom = 1;
      
          node_type_save($type);
      
          node_types_rebuild ( );
          menu_rebuild ( );
      
          drupal_set_message ( t ( 'Product class ' . $pcid . ' created.' ) );
      
      }
      

      【讨论】:

        猜你喜欢
        • 2010-12-22
        • 2012-03-25
        • 1970-01-01
        • 2011-09-24
        • 2023-02-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-14
        相关资源
        最近更新 更多