1

I am using below web service to get a product collection sorted by price :

www.magentohost.com/api/rest/products?order=price&dir=asc

It is giving the following error:

{"messages":{"error":[{"code":400,"message":"Resource collection ordering error."}]}}

What is the cause and how can I fix this?

1

3 Answers 3

1

The error is thrown in app/code/core/Mage/Api2/Model/Resource.php

 if (!is_string($orderField)
     || !array_key_exists($orderField, $this->getAvailableAttributes($this->getUserType(), $operation))
 ) {
     $this->_critical(self::RESOURCE_COLLECTION_ORDERING_ERROR);
 }

So I think your $orderField is either not a string or the attribut is not available for your operation.

Following back the code from the above function, it retrieves the attributes from the modules resources/' . $node . '/attributes XML nodes such as app/code/core/Mage/Catalog/etc/api2.xml.

app/code/core/Mage/Catalog/etc/api2.xml for example has a definition of:

  <config>
     <api2>
        <resources>
           <product> <!-- your $node -->
              <attributes translate="..." module="api2">
                    <entity_id>Product ID</entity_id>
                    <type_id>Product Type</type_id>
                    <attribute_set_id>Attribute Set</attribute_set_id>
                    <stock_data>Inventory Data</stock_data>
                    <image_url>Default Image</image_url>
                    <is_saleable>Salability Status</is_saleable>
                    <total_reviews_count>Total Reviews Count</total_reviews_count>
                    <url>Product URL</url>
                    <buy_now_url>Buy Now URL</buy_now_url>
                    <has_custom_options>Has Custom Options</has_custom_options>
                    <is_in_stock>Stock Status</is_in_stock>
                    <regular_price_with_tax>Regular Price With Tax</regular_price_with_tax>
                    <regular_price_without_tax>Regular Price Without Tax</regular_price_without_tax>
                    <final_price_with_tax>Final Price With Tax</final_price_with_tax>
                    <final_price_without_tax>Final Price Without Tax</final_price_without_tax>
                    <use_config_gift_message_available>Use Config Settings for Allow Gift Message</use_config_gift_message_available>
                    <use_config_gift_wrapping_available>Use Config Settings for Allow Gift Wrapping</use_config_gift_wrapping_available>
                    <url_key_create_redirect>Create Permanent Redirect for old URL</url_key_create_redirect>
                </attributes>
           </product>
        </resources>
     </api2>
  </config>

As I do not see a price in there, I think that's the reason why you get the error.

2
  • okay great.Error is resolved now. Aug 25, 2015 at 6:44
  • Great! Would you please add your own answer how you resolved your problem to help future readers? Aug 25, 2015 at 7:05
1

in app/code/core/Mage/api2/Model/Resource.php _applyCollectionModifiers function need to be editted below is the edited new code. This solves my problem

if (null !== $orderField) {
        $operation = Mage_Api2_Model_Resource::OPERATION_ATTRIBUTE_READ;
        if (!is_string($orderField)
            || !array_key_exists($orderField, $this->getAvailableAttributes($this->getUserType(), $operation))
        ) {
            $this->_critical(self::RESOURCE_COLLECTION_ORDERING_ERROR);
        }
        if("price" == $orderField){
            $dir = $this->getRequest()->getOrderDirection();
            return $collection->getSelect()->order("price_index.min_price {$dir}");
        }
        $collection->setOrder($orderField, $this->getRequest()->getOrderDirection());
    }
0

I have added two lines in /app/code/local/Mage/Catalog/Model/Api2/Product/Rest.php file in function "_prepareProductForResponse" after line $product->setCustomerGroupId($this->_getCustomerGroupId()).

protected function _prepareProductForResponse(Mage_Catalog_Model_Product $product)
{
    /** @var $productHelper Mage_Catalog_Helper_Product */
    $productHelper = Mage::helper('catalog/product');
    $productData = $product->getData();
    $product->setWebsiteId($this->_getStore()->getWebsiteId());
    // customer group is required in product for correct prices calculation
    $product->setCustomerGroupId($this->_getCustomerGroupId());
    // calculate prices
    $finalPrice = $product->getFinalPrice();
    $productData['price'] = $finalPrice;
    $productData['regular_price_with_tax'] = $this->_applyTaxToPrice($product->getPrice(), true);
    $productData['regular_price_without_tax'] = $this->_applyTaxToPrice($product->getPrice(), false);
    $productData['final_price_with_tax'] = $this->_applyTaxToPrice($finalPrice, true);
    $productData['final_price_without_tax'] = $this->_applyTaxToPrice($finalPrice, false);
    $productData['is_saleable'] = $product->getIsSalable();
    $productData['image_url'] = (string) Mage::helper('catalog/image')->init($product, 'image');
    $productData['small_image'] = (string) Mage::helper('catalog/image')->init($product, 'small_image')->resize(225,300);

    if ($this->getActionType() == self::ACTION_TYPE_ENTITY) {

        // define URLs
        $productData['url'] = $productHelper->getProductUrl($product->getId());
        /** @var $cartHelper Mage_Checkout_Helper_Cart */
        $cartHelper = Mage::helper('checkout/cart');
        $productData['buy_now_url'] = $cartHelper->getAddUrl($product);

        /** @var $stockItem Mage_CatalogInventory_Model_Stock_Item */
        $stockItem = $product->getStockItem();
        if (!$stockItem) {
            $stockItem = Mage::getModel('cataloginventory/stock_item');
            $stockItem->loadByProduct($product);
        }
        $productData['is_in_stock'] = $stockItem->getIsInStock();

        /** @var $reviewModel Mage_Review_Model_Review */
        $reviewModel = Mage::getModel('review/review');
        $productData['total_reviews_count'] = $reviewModel->getTotalReviews($product->getId(), true,
            $this->_getStore()->getId());

        $productData['tier_price'] = $this->_getTierPrices();
        $productData['has_custom_options'] = count($product->getOptions()) > 0;
    } else {
        // remove tier price from response
        $product->unsetData('tier_price');
        unset($productData['tier_price']);
    }
    $product->addData($productData);
}

and the problem is resolevd.Price is coming in product collection.

Your Answer

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

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