استدعاء الدالة يُرجع خطأً فادحًا:استدعاء دالة غير محددة getRecentProducts()

magento.stackexchange https://magento.stackexchange.com/questions/108609

سؤال

مرحبا أنا مبتدئ في الماجنتو.أحاول الاتصال بكتلة وحدة نمطية مخصصة باستخدام getChildHtml() الوظيفة التي للأسف تظهر خطأ فادح:استدعاء إلى وظيفة غير محددة getRecentProducts()

أنا أستخدم الماجنتو 1.9.2.4

لقد قمت بإعداد سمة فرعية (سمة الطفل) ضمن سمة rwd الافتراضية للماجنتو.

لقد قمت بنسخ page.xml من السمة الافتراضية لحزمة rwd.

لقد قمت أيضًا بإنشاء ملف تخطيط جديد (home.phtml) وأحاول الحصول على الكتلة في هذا الملف.

لقد قمت بإنشاء كتلة جديدة في صفحة السمات النشطة page.xml ضمنها <default> بطاقة شعار:

<block type="core/text_list" name="newreference" as="newreference"/>

ثم في المواضيع النشطة local.xml:

<default>
    <reference name="newreference">
        <block type="recentproducts/recentproducts" name="recentproducts_recentproducts" template="recentproducts/recentproducts.phtml"></block>
    </reference>
</default>

وفي ملف home.phtml الخاص بي يتم الاتصال: getChildHtml('newreference');

ملف التكوين للوحدة:

<?xml version="1.0"?>
<config>
  <modules>
    <Test_Recentproducts>
      <version>1.0</version>
    </Test_Recentproducts>
  </modules>
  <global>
    <blocks>
      <recentproducts>
        <class>Test_Recentproducts_Block</class>
      </recentproducts>
    </blocks>
    <models>
      <recentproducts>
        <class>Test_Recentproducts_Model</class>
      </recentproducts>
    </models>
  </global>
</config>

newproducts.phtml

<?php
$products = $this­->getRecentProducts();
?>

<div id="product_list">
  <h1>Recent Products</h1>
  <?php if (is_array($products) && count($products)) { ?>
    <?php foreach($products as $product) { ?>
      <div>
        <a href="<?php echo $product['url'] ?>"><?php echo $product['name'] ?></a>
      </div>
    <?php } ?>
  <?php } ?>
</div>

المنتجات الحديثة.php (كتلة)

class Test_Recentproducts_Block_Recentproducts extends Mage_Core_Block_Template {
  public function getRecentProducts() {
    // call model to fetch data
    $arr_products = array();
    $products = Mage::getModel("recentproducts/recentproducts")->getRecentProducts();

    foreach ($products as $product) {
      $arr_products[] = array(
        'id' => $product->getId(),
        'name' => $product->getName(),
        'url' => $product->getProductUrl()
      );
    }

    return $arr_products;
  }
}

أحدث المنتجات.php (نموذج)

class Test_Recentproducts_Model_Recentproducts extends Mage_Core_Model_Abstract {
  public function getRecentProducts() {
    $products = Mage::getModel("catalog/product")
                ->getCollection()
                ->addAttributeToSelect('*')
                ->setOrder('entity_id', 'DESC')
                ->setPageSize(5);

    return $products;
  }
}

أي مساعدة سيكون موضع تقدير كبير.

تحديث:

انا حاولت get_class($this) وهو يعيد الطبقة الصحيحة تمامًا وهي Test_Recentproducts_Block_Recentproducts.

عودتها أيضا getRecentProducts() كأسلوب الطبقة.

هل كانت مفيدة؟

المحلول

يحرر:لقد اختبرت الكود الخاص بك على التثبيت المحلي، وأفترض أنك نسخته من مكان ما.المشكلة هناك هي أن - ليس في الحقيقة ناقصًا ولكن بعض السمات الخاصة المختلفة (لم أجري المزيد من التحقيق، آسف)، وكنت أتلقى نفس الخطأ الذي حصلت عليه.كتبت $this-> نفسي وعملت.

من المحتمل $this­>getRecentProducts(); يجب ان يكون $this­->getRecentProducts();

نصائح أخرى

تأكد من أنك قمت بإنشاء ملف في lastproducts/block/recentproducts.php وأن هذا الملف lastproducts.php يحتوي على وظيفة getRecentProducts().

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top