0

Having carefully followed the answer to handling cross sell items (How to Increase the Number of Crosssell Items in Cart?) everything appears to work fine...except the final instruction of adding:

protected $_maxItemCount = Mage::getStoreConfig('mycompany_section/mycompany_group/mycompany_field');

generates an error thus:

Parse error: syntax error, unexpected '(', expecting ',' or ';' in /home/xxx/public_html/app/code/local/xxx/Checkout/Block/Cart/Crosssell.php

Any help to this Magento/PHP novice would be greatly appreciated :)

Magento ver. 1.9.2.1

EDIT:

Output using Crosssell.php with @simonthesorcerer amendment as follows:

class Abc_Checkout_Block_Cart_Crosssell extends Mage_Catalog_Block_Product_Abstract
{
/**
 * Items quantity will be capped to this value
 *
 * @var int
 */

      protected $_maxItemCount;

      protected function _construct() { 
$this->__maxItemCount = Mage::getStoreConfig('abc_section/abc_group/abc_input');
return parent::_construct(); }

/**
 * Get crosssell items
 *
 * @return array
 */
public function getItems()
{
    $items = $this->getData('items');
    if (is_null($items)) {
        $items = array();
        $ninProductIds = $this->_getCartProductIds();
        if ($ninProductIds) {
            $lastAdded = (int) $this->_getLastAddedProductId();
            if ($lastAdded) {
                $collection = $this->_getCollection()
                    ->addProductFilter($lastAdded);
                if (!empty($ninProductIds)) {
                    $collection->addExcludeProductFilter($ninProductIds);
                }
                $collection->setPositionOrder()->load();

                foreach ($collection as $item) {
                    $ninProductIds[] = $item->getId();
                    $items[] = $item;
                }
            }

            if (count($items) < $this->_maxItemCount) {
                $filterProductIds = array_merge($this->_getCartProductIds(), $this->_getCartProductIdsRel());
                $collection = $this->_getCollection()
                    ->addProductFilter($filterProductIds)
                    ->addExcludeProductFilter($ninProductIds)
                    ->setPageSize($this->_maxItemCount-count($items))
                    ->setGroupBy()
                    ->setPositionOrder()
                    ->load();
                foreach ($collection as $item) {
                    $items[] = $item;
                }
            }

        }

        $this->setData('items', $items);
    }
    return $items;
}

/**
 * Count items
 *
 * @return int
 */
public function getItemCount()
{
    return count($this->getItems());
}

/**
 * Get ids of products that are in cart
 *
 * @return array
 */
protected function _getCartProductIds()
{
    $ids = $this->getData('_cart_product_ids');
    if (is_null($ids)) {
        $ids = array();
        foreach ($this->getQuote()->getAllItems() as $item) {
            if ($product = $item->getProduct()) {
                $ids[] = $product->getId();
            }
        }
        $this->setData('_cart_product_ids', $ids);
    }
    return $ids;
}

/**
 * Retrieve Array of product ids which have special relation with products in Cart
 * For example simple product as part of Grouped product
 *
 * @return array
 */
protected function _getCartProductIdsRel()
{
    $productIds = array();
    foreach ($this->getQuote()->getAllItems() as $quoteItem) {
        $productTypeOpt = $quoteItem->getOptionByCode('product_type');
        if ($productTypeOpt instanceof Mage_Sales_Model_Quote_Item_Option
            && $productTypeOpt->getValue() == Mage_Catalog_Model_Product_Type_Grouped::TYPE_CODE
            && $productTypeOpt->getProductId()
        ) {
            $productIds[] = $productTypeOpt->getProductId();
        }
    }

    return $productIds;
}

/**
 * Get last product ID that was added to cart and remove this information from session
 *
 * @return int
 */
protected function _getLastAddedProductId()
{
    return Mage::getSingleton('checkout/session')->getLastAddedProductId(true);
}

/**
 * Get quote instance
 *
 * @return Mage_Sales_Model_Quote
 */
public function getQuote()
{
    return Mage::getSingleton('checkout/session')->getQuote();
}

/**
 * Get crosssell products collection
 *
 * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Link_Product_Collection
 */
protected function _getCollection()
{
    $collection = Mage::getModel('catalog/product_link')->useCrossSellLinks()
        ->getProductCollection()
        ->setStoreId(Mage::app()->getStore()->getId())
        ->addStoreFilter()
        ->setPageSize($this->_maxItemCount);
    $this->_addProductAttributesAndPrices($collection);

    Mage::getSingleton('catalog/product_status')->addSaleableFilterToCollection($collection);
    Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
    Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($collection);

    return $collection;
}
}

1 Answer 1

1

You are declaring a class property, but you can not call a function when you do this.

Instead, change the declaration like this:

protected $_myPropertyName;

I assume your block extends Mage_Core_Block_Abstract. You should then override the function _construct() and fill the property there:

protected function _construct() { 
$this->_myPropertyName = Mage::getStoreConfig('configname');
return parent::_construct(); }

Sorry for formatting, I am on mobile.

4
  • Thanks for suggestion - it certainly clears the syntax error...but does not appear to grab the perfectly valid data from 'mycompany_section/mycompany_group/mycompany_field'. Yes, extending Mage_Core_Block_Abstract, as in original example above. Stumped :(
    – johnno
    Oct 20, 2015 at 12:44
  • Can you edit your question and show me the line where you try to output it? Oct 20, 2015 at 13:13
  • In _construct(), your property is "__maxItemCount", later you refer to "_maxItemCount"; remove the second underscore in the first one :) Oct 27, 2015 at 7:53
  • Ah...copy & paste coding bites me again. I'll never learn. Thank you so much!
    – johnno
    Oct 27, 2015 at 11:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.