0

我有 3d 方扩展,可以在后端编辑产品时添加新选项卡。现在我想在那里再添加一个标签。

新标签应该有“添加”按钮,让用户添加新项目,此外它应该有添加项目的列表。首先,我查看了扩展代码。他们使用添加了类似的选项卡

extends Mage_Adminhtml_Block_Widget
implements Varien_Data_Form_Element_Renderer_Interface 

所以我试着按照他们的方式添加我的。代码如下。

    $this->addTab('cancellpolicy', array(
            'label'     => Mage::helper('catalog')->__('Cancellation Policies'),
            'content'   => $this->_translateHtml($this->getLayout()->createBlock('Apptha_Reservation_Block_Adminhtml_Catalog_Product_Edit_Tab_Cancellationpolicy')->toHtml()),
        )); 

上面我添加了新选项卡,然后我在下面创建了新的块类

class Apptha_Reservation_Block_Adminhtml_Catalog_Product_Edit_Tab_Cancellationpolicy 

extends Mage_Adminhtml_Block_Widget
implements Varien_Data_Form_Element_Renderer_Interface 
{

   /**
 * Form element instance
 *
 * @var Varien_Data_Form_Element
 */
protected $_element;

/**
 * Customer Groups cache
 *
 * @var array
 */
protected $_customerGroups;

/**
 * Websites cache
 *
 * @var array
 */
protected $_websites;



public function __construct(){
    $this->setTemplate('reservation/product/edit/tab/cancellationpolicy.phtml');
}

 public function getProduct(){
    return Mage::registry('product');
}
public function render(Varien_Data_Form_Element_Abstract $element){
    $this->setElement($element);
    return $this->toHtml();
}


protected function _prepareLayout()
{
    $this->setChild('add_button',
        $this->getLayout()->createBlock('adminhtml/widget_button')
            ->setData(array(
                'label'     => Mage::helper('catalog')->__('Add Room Type(s)'),
                'onclick'   => 'roomtypesControl.addItem()',
                'class' => 'add'
            )));   

    return parent::_prepareLayout();
}


 /**
 * Set form element instance
 *
 * @param Varien_Data_Form_Element_Abstract $element
 * @return Apptha_Reservation_Block_Adminhtml_Catalog_Product_Edit_Tab_Cancellationpolicy
 */   
public function setElement(Varien_Data_Form_Element_Abstract $element){
    $this->_element = $element;
    return $this;
}



/**
 * Retrieve form element instance
 *
 * @return Apptha_Reservation_Block_Adminhtml_Catalog_Product_Edit_Tab_Cancellationpolicy
 */
public function getElement(){
    return $this->_element;
}    


public function getWebsites()
{

    if (!is_null($this->_websites)) {
        return $this->_websites;
    }
    $websites = array();
    $websites[0] = array(
        'name'      => $this->__('All Websites'),
        'currency'  => Mage::app()->getBaseCurrencyCode()
    );
    if (Mage::app()->isSingleStoreMode() || $this->getElement()->getEntityAttribute()->isScopeGlobal()) {
        return $websites;
    }
    elseif ($storeId = $this->getProduct()->getStoreId()) {
        $website = Mage::app()->getStore($storeId)->getWebsite();
        $websites[$website->getId()] = array(
            'name'      => $website->getName(),
            'currency'  => $website->getConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_BASE),
        );
    }
    else {
        $websites[0] = array(
            'name'      => $this->__('All Websites'),
            'currency'  => Mage::app()->getBaseCurrencyCode()
        );
        foreach (Mage::app()->getWebsites() as $website) {
            if (!in_array($website->getId(), $this->getProduct()->getWebsiteIds())) {
                continue;
            }
            $websites[$website->getId()] = array(
                'name'      => $website->getName(),
                'currency'  => $website->getConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_BASE),
            );
        }
    }
    $this->_websites = $websites;
    return $this->_websites;
}

public function getValues(){
    return Mage::getModel('reservation/roomtypes')->getCollection()
                ->addEntityIdFilter($this->getProduct()->getId())
                ->addStoreIdFilter($this->getProduct()->getStoreId())
                ->getItems();
}

}

然后添加reservation/product/edit/tab/cancellationpolicy.phtml 模板文件。我得到的模板的开头

<?php   Mage::log(get_class($this->getElement())); ?>
<?php $_htmlId      = $this->getElement()->getHtmlId() ?>
<?php $_htmlClass   = $this->getElement()->getClass() ?>
<?php $_storeId     = $this->getProduct()->getStoreId() ?>
<?php $_htmlName    = $this->getElement()->getName() ?>
<?php $_readonly    = $this->getElement()->getReadonly() ?>
<?php $_multiWebsite= 0 && !Mage::app()->isSingleStoreMode() ?>

在这里我得到错误:

致命错误:在 /var/www/vhosts/bluning.com/httpdocs/app/design/adminhtml/default/default/template/reservation/product/edit/tab/ 中的非对象上调用成员函数 getHtmlId()第 10 行的 cancelpolicy.phtml

Mage::log(get_class($this->getElement()));

给我“Mage_Core_Block_Template”,但为什么呢?根据我的代码 getElement() 应该返回“Apptha_Reservation_Block_Adminhtml_Catalog_Product_Edit_Tab_Cancellationpolicy”

那么为什么 Magento 在 .phtml 文件中返回错误的类呢?

UPDATE config.xml 有部分

      <blocks>
        <reservation>
            <class>Apptha_Reservation_Block</class>
        </reservation>

    </blocks>

UPDATE2 我已将 Mage::log 放在 getElement 函数中并在调用之后。它返回不同的值:

inside: Apptha_Reservation_Block_Adminhtml_Catalog_Product_Edit_Tab_Cancellationpolicy
outside: Mage_Core_Block_Template

疯狂的

4

2 回答 2

0

该行:

$this->getLayout()->createBlock('Apptha_Reservation_Block_Adminhtml_Catalog_Product_Edit_Tab_Cancellationpolicy')

完全无效。您需要尊重 magento 框架语法。(它不仅仅是一个类实例化)

有关如何在 magento 中访问资源的详细信息,请参阅我的答案:Get all invoices in Magento

要使其工作,您需要通过以下方式调用它:

$this->getLayout()->createBlock('apptha_reservation/adminhtml_catalog_product_edit_tab_cancellationpolicy')

ps:如果“apptha_reservation”是您在 config.xml 中声明的块 XML 标记

于 2012-11-20T12:33:06.930 回答
0

使用调试器我发现了问题。

$this->getElement()

它返回 NULL 因为它没有被正确设置。它应该设置在观察者中。代码如下:

  public function attachPolicyEditor($observer) {
    $form = $observer->getForm();
    if ($policies = $form->getElement('apptha_hotel_cancellationpolicy')) {
        $policies->setRenderer(
            Mage::getSingleton('core/layout')->createBlock('reservation/adminhtml_catalog_product_edit_tab_cancellationpolicy')
        );
    }
}

attachPolicyEditor 由 adminhtml_catalog_product_edit_prepare_form 观察者调用。

于 2012-11-21T07:27:01.447 回答