0

バックエンドでの製品編集中に新しいタブを追加するサードパーティの拡張機能があります。ここで、もう 1 つタブを追加します。

新しいタブには「追加」ボタンがあり、ユーザーが新しいアイテムを追加できるようにする必要があります。さらに、追加されたアイテムのリストが必要です。まず、拡張コードを見てきました。を使用して同様のタブを追加しました

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->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 に答える
0

この線 :

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

は完全に無効です。Magento フレームワークの構文を尊重する必要があります。(単なるクラスのインスタンス化ではありません)

Magentoでリソースにアクセスする方法の詳細については、こちらの回答を参照してください: Magento ですべての請求書を取得する

それを機能させるには、次の方法で呼び出す必要があります。

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

ps: 「apptha_reservation」が config.xml で宣言されたブロック XML タグの場合

于 2012-11-20T12:33:06.930 に答える