Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process only required attributes in api/rest/products/ #4517

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
6 changes: 3 additions & 3 deletions app/code/core/Mage/Api2/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public function isApiTypeSupported($type)
*
* @param string $userType
* @param string $resourceId
* @param string $operation One of Mage_Api2_Model_Resource::OPERATION_ATTRIBUTE_... constant
* @param Mage_Api2_Model_Resource::OPERATION_ATTRIBUTE_* $operation
* @return array
*/
public function getAllowedAttributes($userType, $resourceId, $operation)
Expand Down Expand Up @@ -169,8 +169,8 @@ public function isAllAttributesAllowed($userType)
/**
* Get operation type for specified operation
*
* @param string $operation One of Mage_Api2_Model_Resource::OPERATION_... constant
* @return string One of Mage_Api2_Model_Resource::OPERATION_ATTRIBUTE_... constant
* @param Mage_Api2_Model_Resource::OPERATION_* $operation
* @return Mage_Api2_Model_Resource::OPERATION_ATTRIBUTE_*
* @throws Exception
*/
public function getTypeOfOperation($operation)
Expand Down
2 changes: 1 addition & 1 deletion app/code/core/Mage/Api2/Model/Acl/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function collectionOut($items)
/**
* Fetch array of allowed attributes for given resource type, operation and user type.
*
* @param string $operationType OPTIONAL One of Mage_Api2_Model_Resource::OPERATION_ATTRIBUTE_... constant
* @param Mage_Api2_Model_Resource::OPERATION_ATTRIBUTE_* $operationType OPTIONAL
* @return array
*/
public function getAllowedAttributes($operationType = null)
Expand Down
4 changes: 2 additions & 2 deletions app/code/core/Mage/Api2/Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public function getResourceForcedAttributes($resource, $userType)
*
* @param string $resource API resource ID
* @param string $userType API user type
* @param string $operationType Type of operation: one of Mage_Api2_Model_Resource::OPERATION_ATTRIBUTE_... constant
* @param Mage_Api2_Model_Resource::OPERATION_ATTRIBUTE_* $operationType
* @return array
*/
public function getResourceIncludedAttributes($resource, $userType, $operationType)
Expand All @@ -295,7 +295,7 @@ public function getResourceIncludedAttributes($resource, $userType, $operationTy
*
* @param string $resource API resource ID
* @param string $userType API user type
* @param string $operationType Type of operation: one of Mage_Api2_Model_Resource::OPERATION_ATTRIBUTE_... constant
* @param Mage_Api2_Model_Resource::OPERATION_ATTRIBUTE_* $operationType
* @return array
*/
public function getResourceEntityOnlyAttributes($resource, $userType, $operationType)
Expand Down
20 changes: 8 additions & 12 deletions app/code/core/Mage/Api2/Model/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,12 @@ abstract class Mage_Api2_Model_Resource
protected $_userType;

/**
* One of Mage_Api2_Model_Resource::ACTION_TYPE_... constant
*
* @var string
* @var Mage_Api2_Model_Resource::ACTION_TYPE_*
*/
protected $_actionType;

/**
* One of Mage_Api2_Model_Resource::OPERATION_... constant
*
* @var string
* @var Mage_Api2_Model_Resource::OPERATION_*
*/
protected $_operation;

Expand Down Expand Up @@ -524,7 +520,7 @@ public function setApiUser(Mage_Api2_Model_Auth_User_Abstract $apiUser)
* Get action type
* If not exists get from Request
*
* @return string One of Mage_Api2_Model_Resource::ACTION_TYPE_... constant
* @return self::ACTION_TYPE_*
*/
public function getActionType()
{
Expand All @@ -537,7 +533,7 @@ public function getActionType()
/**
* Set route type
*
* @param string $actionType One of Mage_Api2_Model_Resource::ACTION_TYPE_... constant
* @param self::ACTION_TYPE_* $actionType
* @return $this
*/
public function setActionType($actionType)
Expand All @@ -550,7 +546,7 @@ public function setActionType($actionType)
* Get operation
* If not exists get from Request
*
* @return string One of Mage_Api2_Model_Resource::OPERATION_... constant
* @return self::OPERATION_*
*/
public function getOperation()
{
Expand All @@ -563,7 +559,7 @@ public function getOperation()
/**
* Set operation
*
* @param string $operation One of Mage_Api2_Model_Resource::OPERATION_... constant
* @param self::OPERATION_* $operation
* @return $this
*/
public function setOperation($operation)
Expand Down Expand Up @@ -942,7 +938,7 @@ public function getForcedAttributes()
* Retrieve list of included attributes
*
* @param string $userType API user type
* @param string $operationType Type of operation: one of Mage_Api2_Model_Resource::OPERATION_ATTRIBUTE_... constant
* @param Mage_Api2_Model_Resource::OPERATION_ATTRIBUTE_* $operationType
* @return array
*/
public function getIncludedAttributes($userType, $operationType)
Expand All @@ -954,7 +950,7 @@ public function getIncludedAttributes($userType, $operationType)
* Retrieve list of entity only attributes
*
* @param string $userType API user type
* @param string $operationType Type of operation: one of Mage_Api2_Model_Resource::OPERATION_ATTRIBUTE_... constant
* @param Mage_Api2_Model_Resource::OPERATION_ATTRIBUTE_* $operationType
* @return array
*/
public function getEntityOnlyAttributes($userType, $operationType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected function _construct()
/**
* @param string $userType
* @param string $resourceId
* @param string $operation One of Mage_Api2_Model_Resource::OPERATION_ATTRIBUTE_... constant
* @param Mage_Api2_Model_Resource::OPERATION_ATTRIBUTE_* $operation
* @return string|bool|null
*/
public function getAllowedAttributes($userType, $resourceId, $operation)
Expand Down
152 changes: 119 additions & 33 deletions app/code/core/Mage/Catalog/Model/Api2/Product/Rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ abstract class Mage_Catalog_Model_Api2_Product_Rest extends Mage_Catalog_Model_A
*/
protected $_product;

protected ?array $allowedAttributes = null;

/**
* Retrieve product data
*
Expand Down Expand Up @@ -100,46 +102,22 @@ protected function _applyCategoryFilter(Mage_Catalog_Model_Resource_Product_Coll
*/
protected function _prepareProductForResponse(Mage_Catalog_Model_Product $product)
{
/** @var Mage_Catalog_Helper_Product $productHelper */
$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['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');
$this->addAttribute('image_url', $productData, $product);
$this->addAttribute('is_saleable', $productData, $product);
$this->addPrices($productData, $product);

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

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

/** @var Mage_Review_Model_Review $reviewModel */
$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;
$this->addAttribute('buy_now_url', $productData, $product);
$this->addAttribute('has_custom_options', $productData, $product);
$this->addAttribute('is_in_stock', $productData, $product);
$this->addAttribute('tier_price', $productData, $product);
$this->addAttribute('total_reviews_count', $productData, $product);
$this->addAttribute('url', $productData, $product);
} else {
// remove tier price from response
$product->unsetData('tier_price');
Expand All @@ -148,6 +126,114 @@ protected function _prepareProductForResponse(Mage_Catalog_Model_Product $produc
$product->addData($productData);
}

Hanmac marked this conversation as resolved.
Show resolved Hide resolved
/**
* Add custom attributes to product data
*
* Apply custom logig before add data to product.
*/
protected function addAttribute(string $attribute, array &$productData, Mage_Catalog_Model_Product $product): void
{
if (!$this->isAllowedAttribute($attribute)) {
return;
}

switch ($attribute) {
case 'buy_now_url':
/** @var Mage_Checkout_Helper_Cart $cartHelper */
$cartHelper = Mage::helper('checkout/cart');
$productData[$attribute] = $cartHelper->getAddUrl($product);
break;
case 'has_custom_options':
$productData[$attribute] = count($product->getOptions()) > 0;
break;
case 'image_url':
$productData[$attribute] = (string) Mage::helper('catalog/image')->init($product, 'image');
break;
case 'is_in_stock':
$stockItem = $product->getStockItem();
if (!$stockItem) {
$stockItem = Mage::getModel('cataloginventory/stock_item');
$stockItem->loadByProduct($product);
}
$productData[$attribute] = $stockItem->getIsInStock();
break;
case 'is_saleable':
$productData[$attribute] = $product->getIsSalable();
break;
case 'tier_price':
$productData[$attribute] = $this->_getTierPrices();
break;
case 'total_reviews_count':
/** @var Mage_Review_Model_Review $reviewModel */
$reviewModel = Mage::getModel('review/review');
$productData[$attribute] = $reviewModel->getTotalReviews(
$product->getId(),
true,
$this->_getStore()->getId(),
);
break;
case 'url':
$productData[$attribute] = $product->getProductUrl();
break;
}
}

/**
* Add price attributes to product data
*/
protected function addPrices(array &$productData, Mage_Catalog_Model_Product $product): void
Hanmac marked this conversation as resolved.
Show resolved Hide resolved
{
$isPriceRequired = false;
if ($this->isAllowedAttribute('regular_price_with_tax') ||
$this->isAllowedAttribute('regular_price_without_tax') ||
$this->isAllowedAttribute('final_price_with_tax') ||
$this->isAllowedAttribute('final_price_without_tax')
) {
$isPriceRequired = true;
}

// calculate prices
if ($isPriceRequired) {
$finalPrice = $product->getFinalPrice();
if ($this->isAllowedAttribute('regular_price_with_tax')) {
$productData['regular_price_with_tax'] = $this->_applyTaxToPrice($product->getPrice(), true);
}
if ($this->isAllowedAttribute('regular_price_without_tax')) {
$productData['regular_price_without_tax'] = $this->_applyTaxToPrice($product->getPrice(), false);
}
if ($this->isAllowedAttribute('final_price_with_tax')) {
$productData['final_price_with_tax'] = $this->_applyTaxToPrice($finalPrice, true);
}
if ($this->isAllowedAttribute('final_price_without_tax')) {
$productData['final_price_without_tax'] = $this->_applyTaxToPrice($finalPrice, false);
}
}
}

/**
* Get allowed attributes for output
*/
/**
* @return string[]
*/
protected function getAllowedAttributes(): array
sreichel marked this conversation as resolved.
Show resolved Hide resolved
sreichel marked this conversation as resolved.
Show resolved Hide resolved
{
if (is_null($this->allowedAttributes)) {
$attributes = Mage::helper('api2')->getAllowedAttributes(
$this->getUserType(),
'product',
Mage_Api2_Model_Resource::OPERATION_ATTRIBUTE_READ,
);
$this->allowedAttributes = $attributes;
}
return $this->allowedAttributes;
}

protected function isAllowedAttribute(string $attribute): bool
{
return in_array($attribute, $this->getAllowedAttributes());
}

/**
* Product create only available for admin
*/
Expand Down
Loading