0

Hi guys, so I need to know how to add Products programtically without getting this error.

Fatal error: Call to a member function getPosition() on a non-object in \app\code\core\Mage\Bundle\Model\Product\Type.php on line 854

This is my code:

class TCK_Forty_CartController extends Mage_Core_Controller_Front_Action {

    public function addAction() {
        $prdId = $this->getRequest()->getParam('product');

        $cart = Mage::getModel('checkout/cart');
        $cart->init();

        $_return = array('code' => 0, 'message' => 'Something went wrong...');

        if (!empty($prdId)) {
            $product = new Mage_Catalog_Model_Product();
            $product->load($prdId);

            if ($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_BUNDLE) {
                $typeInstance = $product->getTypeInstance(true);
                $optionCollection = $typeInstance->getOptionsCollection($product);

                $selectionCollection = $typeInstance->getSelectionsCollection(
                    $typeInstance->getOptionsIds($product),
                    $product
                    );

                $this->_options = $optionCollection->appendSelections($selectionCollection, false,
                    Mage::helper('catalog/product')->getSkipSaleableCheck()
                    );

                $bundle_option = array();

                foreach ($this->_options as $key => $value) {
                    foreach ($value['selections'] as $selKey => $selection) {
                        $bundle_option[$selection->option_id][] = $selection['selection_id'];
                    }
                }

                $params = array(
                    'product' => $prdId,
                    'related_product' => '',
                    'bundle_option' => $bundle_option,
                    'qty' => '1',
                );

                $cart->addProduct($product, $params);
                $cart->save();
            }
        }
    }
}
2
Contributors
2
Replies
15
Views
2 Years
Discussion Span
Last Post by veedeoo
0

I don't have the source of Magento, but what the error tells us is that whatever is the object of the method getPosition(), it is not being instantiated.

You will have to find this on your codes. Please read the codes below as instructions and not the actual method of the core type.php, because I am shooting lots of blanks and guesses here.

Assuming that the instance of an object of a getPosition() method is $some_x, then the source codes will have some kind of method that can instantiate the object from either a remote class or within its namespace.

public function above_line_854($some_x, $some_y, $some_z){

## I am not sure if there is an actual $some_y and $some_z, but I am sure there has to be some $some_x in the code. Otherwise, you will not be getting and error of $some_x.

$x_position = array(
                ## my first guess
                $some_x->method_of_x()->getPosition(),
                ## my other guess
                $some_x->another_method(), 
                ## my not so sure of a guess, or maybe something like this
                $some_x->getPosition(),
                );

}                

I hope my assumption on the source code is close enough. Anyways, what I am trying to tell you here is that your script have lost its connection or its ability to trigger an instance of of $some_x whatever it maybe in the source code. You just have to figure how to get that $some_x.

If some_x is a lone function and is not a dependent of any object, then look for that function and make sure the file where it reside is included in your script.

Magento is build on top of the Zend framework and unlike other frameworks e.g. Kohana and CI, it does not check if the function exists or method exists before even calling a function or instantiating an object. It is highly relying on the autoloaders and namespaces.

I hope I don't add more confusion on the subject matter..

Edited by veedeoo: more info added

0

it does not check if the function exists or method exists before even calling a function or instantiating an object.

Just for clarification, what I meant by the above statement is that it is more aimed for the instance of rather than a literal function_exists. Instance of can be carried out by either factory or a singleton of which I strongly believe magento must have either one or both.

Edited by veedeoo: more info added

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.