【问题标题】:ERROR: Call to a member function getProducts() on a non-object错误:在非对象上调用成员函数 getProducts()
【发布时间】:2016-01-15 10:36:04
【问题描述】:

我收到一个错误:

致命错误:在非对象上调用成员函数 getProducts() 在 /var/www/vhosts/designsense.net.au/gypsy/public_html/catalog/controller/module/cart.php 第 23 行

这是我正在使用的代码。

    class ControllerModuleCart extends Controller { 
    protected function index() {
    $this->language->load('module/cart');

    $this->data['heading_title'] = $this->language->get('heading_title');
    $this->data['text_subtotal'] = $this->language->get('text_subtotal');
    $this->data['text_empty'] = $this->language->get('text_empty');
    $this->data['text_remove'] = $this->language->get('text_remove');
    $this->data['text_confirm'] = $this->language->get('text_confirm');
    $this->data['text_cart'] = $this->language->get('text_cart');
    $this->data['text_checkout'] = $this->language->get('text_checkout');
    $this->data['button_checkout'] = $this->language->get('button_checkout');
    $this->data['button_remove'] = $this->language->get('button_remove');
    $this->data['text_cart'] = $this->language->get('text_cart');

    $this->data['cart'] = $this->url->link('checkout/cart');
    $this->data['checkout'] = $this->url->link('checkout/checkout', '', 'SSL');

    // Get Cart Products
    $this->data['products'] = array();

    foreach ($this->cart->getProducts() as $result) {

        $option_data = array();

        foreach ($result['option'] as $option) {
            $option_data[] = array(
                'name'  => $option['name'],
                'value' => (strlen($option['option_value']) > 20 ? substr($option['option_value'], 0, 20) . '..' : $option['option_value'])
            );
        }

关于可能导致此问题的任何建议?

【问题讨论】:

  • 这有关系吗? $this->data['cart'] = $this->url->link('checkout/cart'); ....... $this->cart->getProducts()
  • $this->cart 定义了吗?
  • @SpongePablo 我如何检查它是否是?代码不是我写的,所以不太清楚。
  • 框架??代码点火器?
  • @mongjong 是这个 $this->data['cart'] = $this->url->link('checkout/cart');定义它?

标签: php opencart


【解决方案1】:

在调用 getProducts() 函数之前尝试加载模型

$this->load->model('catalog/product');

【讨论】:

    【解决方案2】:

    你不能在foreach循环中使用$this->cart->getProducts();

    所以你需要给变量赋值

    $products = $this->cart->getProducts();

    这一小行代码最重要,因为它调用了函数getProducts,这是位于system/library/cart.php 中的核心过程。变量$products 现在拥有整个购物车数组 + 所有可能的选项,并且可以“迭代”通过。迭代意味着筛选一个数组,收集它的内容或循环。让我们看看在 function getProducts(); 中发生了什么,在 system/library/cart.php 中找到这个函数朝顶部: 然后在foreach循环中使用它

    foreach ($products as $product) 
    { 
      //your code here
    }
    

    这段代码是foreach() 循环设置来遍历我们刚刚在system/library/cart.php 中设置的数组。它负责显示购物车页面中的所有内容以及产品选项。 foreach ($products as $product) { 下面的所有内容将根据需要重复多次,直到所有产品都已在阵列中处理完毕。

    这会很好。

    阅读此Article as well

    编辑 01

    public function getProducts()
    {
        if ( !$this->data )
        {
            foreach ( $this->session->data['cart'] as $key => $quantity )
            {
                $product = explode(':', $key);
                $product_id = $product[0];
                $stock = true;// Options
                if ( isset($product[1]) )
                {
                    $options = unserialize(base64_decode($product[1]));
                }
                else
                {
                    $options = array();
                }
            }
        }
    }
    

    【讨论】:

    • 我已经这样做了,现在新的错误是:致命错误:在 /var/www/xxxxx/public_html/catalog/controller/module 中的非对象上调用成员函数 getProducts() /cart.php 第 19 行
    • 致命错误:在第 19 行的 /var/www/xxxxx/public_html/catalog/controller/module/cart.php 中的非对象上调用成员函数 getProducts()
    • @Fushy 检查 EDIT 01
    • 我已经尝试添加它,但是下面的所有变量都显示为未定义。我在第 2 行添加了该代码(与上面的代码相同)。不幸的是,我得到的错误是在评论中发布的方式太长了。
    • 检查我在答案中发布的链接
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多