Public Member Functions | |
__construct () | |
items ($type, $productId) | |
assign ($type, $productId, $linkedProductId, $data=array()) | |
update ($type, $productId, $linkedProductId, $data=array()) | |
remove ($type, $productId, $linkedProductId) | |
attributes ($type) | |
types () | |
Protected Member Functions | |
_getTypeId ($type) | |
_initProduct ($productId) | |
_initCollection ($link, $product) | |
_collectionToEditableArray ($collection) | |
Protected Attributes | |
$_typeMap |
Definition at line 34 of file Api.php.
__construct | ( | ) |
_collectionToEditableArray | ( | $ | collection | ) | [protected] |
Export collection to editable array
Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Link_Product_Collection | $collection |
Definition at line 310 of file Api.php.
00311 { 00312 $result = array(); 00313 00314 foreach ($collection as $linkedProduct) { 00315 $result[$linkedProduct->getId()] = array(); 00316 00317 foreach ($collection->getLinkModel()->getAttributes() as $attribute) { 00318 $result[$linkedProduct->getId()][$attribute['code']] = $linkedProduct->getData($attribute['code']); 00319 } 00320 } 00321 00322 return $result; 00323 }
_getTypeId | ( | $ | type | ) | [protected] |
_initCollection | ( | $ | link, | |
$ | product | |||
) | [protected] |
Initialize and return linked products collection
Mage_Catalog_Model_Product_Link | $link | |
Mage_Catalog_Model_Product | $product |
Definition at line 294 of file Api.php.
00295 { 00296 $collection = $link 00297 ->getProductCollection() 00298 ->setIsStrongMode() 00299 ->setProduct($product); 00300 00301 return $collection; 00302 }
_initProduct | ( | $ | productId | ) | [protected] |
Initilize and return product model
int | $productId |
Definition at line 266 of file Api.php.
00267 { 00268 00269 00270 $product = Mage::getModel('catalog/product') 00271 ->setStoreId($this->_getStoreId()); 00272 00273 $idBySku = $product->getIdBySku($productId); 00274 if ($idBySku) { 00275 $productId = $idBySku; 00276 } 00277 00278 $product->load($productId); 00279 00280 if (!$product->getId()) { 00281 $this->_fault('product_not_exists'); 00282 } 00283 00284 return $product; 00285 }
assign | ( | $ | type, | |
$ | productId, | |||
$ | linkedProductId, | |||
$ | data = array() | |||
) |
Add product link association
string | $type | |
int|string | $productId | |
int|string | $linkedProductId | |
array | $data |
Reimplemented in Mage_Catalog_Model_Product_Link_Api_V2.
Definition at line 95 of file Api.php.
00096 { 00097 $typeId = $this->_getTypeId($type); 00098 00099 $product = $this->_initProduct($productId); 00100 00101 $link = $product->getLinkInstance() 00102 ->setLinkTypeId($typeId); 00103 00104 $collection = $this->_initCollection($link, $product); 00105 $idBySku = $product->getIdBySku($linkedProductId); 00106 if ($idBySku) { 00107 $linkedProductId = $idBySku; 00108 } 00109 00110 $links = $this->_collectionToEditableArray($collection); 00111 00112 $links[(int)$linkedProductId] = array(); 00113 00114 foreach ($collection->getLinkModel()->getAttributes() as $attribute) { 00115 if (isset($data[$attribute['code']])) { 00116 $links[(int)$linkedProductId][$attribute['code']] = $data[$attribute['code']]; 00117 } 00118 } 00119 00120 try { 00121 $link->getResource()->saveProductLinks($product, $links, $typeId); 00122 } catch (Exception $e) { 00123 $this->_fault('data_invalid', Mage::helper('catalog')->__('Link product not exists.')); 00124 } 00125 00126 return true; 00127 }
attributes | ( | $ | type | ) |
Retrieve attribute list for specified type
string | $type |
Definition at line 216 of file Api.php.
00217 { 00218 $typeId = $this->_getTypeId($type); 00219 00220 $attributes = Mage::getModel('catalog/product_link') 00221 ->getAttributes($typeId); 00222 00223 $result = array(); 00224 00225 foreach ($attributes as $attribute) { 00226 $result[] = array( 00227 'code' => $attribute['code'], 00228 'type' => $attribute['type'] 00229 ); 00230 } 00231 00232 return $result; 00233 }
items | ( | $ | type, | |
$ | productId | |||
) |
Retrieve product link associations
string | $type | |
int|sku | $productId |
Definition at line 55 of file Api.php.
00056 { 00057 $typeId = $this->_getTypeId($type); 00058 00059 $product = $this->_initProduct($productId); 00060 00061 $link = $product->getLinkInstance() 00062 ->setLinkTypeId($typeId); 00063 00064 $collection = $this->_initCollection($link, $product); 00065 00066 $result = array(); 00067 00068 foreach ($collection as $linkedProduct) { 00069 $row = array( 00070 'product_id' => $linkedProduct->getId(), 00071 'type' => $linkedProduct->getTypeId(), 00072 'set' => $linkedProduct->getAttributeSetId(), 00073 'sku' => $linkedProduct->getSku() 00074 ); 00075 00076 foreach ($link->getAttributes() as $attribute) { 00077 $row[$attribute['code']] = $linkedProduct->getData($attribute['code']); 00078 } 00079 00080 $result[] = $row; 00081 } 00082 00083 return $result; 00084 }
remove | ( | $ | type, | |
$ | productId, | |||
$ | linkedProductId | |||
) |
Remove product link association
string | $type | |
int|string | $productId | |
int|string | $linkedProductId |
Definition at line 179 of file Api.php.
00180 { 00181 $typeId = $this->_getTypeId($type); 00182 00183 $product = $this->_initProduct($productId); 00184 00185 $link = $product->getLinkInstance() 00186 ->setLinkTypeId($typeId); 00187 00188 $collection = $this->_initCollection($link, $product); 00189 00190 $idBySku = $product->getIdBySku($linkedProductId); 00191 if ($idBySku) { 00192 $linkedProductId = $idBySku; 00193 } 00194 00195 $links = $this->_collectionToEditableArray($collection); 00196 00197 if (isset($links[$linkedProductId])) { 00198 unset($links[$linkedProductId]); 00199 } 00200 00201 try { 00202 $link->getResource()->saveProductLinks($product, $links, $typeId); 00203 } catch (Exception $e) { 00204 $this->_fault('not_removed'); 00205 } 00206 00207 return true; 00208 }
types | ( | ) |
update | ( | $ | type, | |
$ | productId, | |||
$ | linkedProductId, | |||
$ | data = array() | |||
) |
Update product link association info
string | $type | |
int|string | $productId | |
int|string | $linkedProductId | |
array | $data |
Reimplemented in Mage_Catalog_Model_Product_Link_Api_V2.
Definition at line 138 of file Api.php.
00139 { 00140 $typeId = $this->_getTypeId($type); 00141 00142 $product = $this->_initProduct($productId); 00143 00144 $link = $product->getLinkInstance() 00145 ->setLinkTypeId($typeId); 00146 00147 $collection = $this->_initCollection($link, $product); 00148 00149 $links = $this->_collectionToEditableArray($collection); 00150 00151 $idBySku = $product->getIdBySku($linkedProductId); 00152 if ($idBySku) { 00153 $linkedProductId = $idBySku; 00154 } 00155 00156 foreach ($collection->getLinkModel()->getAttributes() as $attribute) { 00157 if (isset($data[$attribute['code']])) { 00158 $links[(int)$linkedProductId][$attribute['code']] = $data[$attribute['code']]; 00159 } 00160 } 00161 00162 try { 00163 $link->getResource()->saveProductLinks($product, $links, $typeId); 00164 } catch (Exception $e) { 00165 $this->_fault('data_invalid', Mage::helper('catalog')->__('Link product not exists.')); 00166 } 00167 00168 return true; 00169 }
$_typeMap [protected] |
Initial value:
array( 'related' => Mage_Catalog_Model_Product_Link::LINK_TYPE_RELATED, 'up_sell' => Mage_Catalog_Model_Product_Link::LINK_TYPE_UPSELL, 'cross_sell' => Mage_Catalog_Model_Product_Link::LINK_TYPE_CROSSSELL, 'grouped' => Mage_Catalog_Model_Product_Link::LINK_TYPE_GROUPED )