1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: class Mage_Catalog_Model_Product_Link_Api_V2 extends Mage_Catalog_Model_Product_Link_Api
35: {
36: 37: 38: 39: 40: 41: 42: 43: 44:
45: public function assign($type, $productId, $linkedProductId, $data = array(), $identifierType = null)
46: {
47: $typeId = $this->_getTypeId($type);
48:
49: $product = $this->_initProduct($productId, $identifierType);
50:
51: $link = $product->getLinkInstance()
52: ->setLinkTypeId($typeId);
53:
54: $collection = $this->_initCollection($link, $product);
55: $idBySku = $product->getIdBySku($linkedProductId);
56: if ($idBySku) {
57: $linkedProductId = $idBySku;
58: }
59:
60: $links = $this->_collectionToEditableArray($collection);
61:
62: $links[(int)$linkedProductId] = array();
63: foreach ($collection->getLinkModel()->getAttributes() as $attribute) {
64: if (isset($data->$attribute['code'])) {
65: $links[(int)$linkedProductId][$attribute['code']] = $data->$attribute['code'];
66: }
67: }
68:
69: try {
70: if ($type == 'grouped') {
71: $link->getResource()->saveGroupedLinks($product, $links, $typeId);
72: } else {
73: $link->getResource()->saveProductLinks($product, $links, $typeId);
74: }
75:
76: $_linkInstance = Mage::getSingleton('catalog/product_link');
77: $_linkInstance->saveProductRelations($product);
78:
79: $indexerStock = Mage::getModel('cataloginventory/stock_status');
80: $indexerStock->updateStatus($productId);
81:
82: $indexerPrice = Mage::getResourceModel('catalog/product_indexer_price');
83: $indexerPrice->reindexProductIds($productId);
84:
85: } catch (Exception $e) {
86: $this->_fault('data_invalid', $e->getMessage());
87:
88: }
89:
90: return true;
91: }
92:
93: 94: 95: 96: 97: 98: 99: 100: 101:
102: public function update($type, $productId, $linkedProductId, $data = array(), $identifierType = null)
103: {
104: $typeId = $this->_getTypeId($type);
105:
106: $product = $this->_initProduct($productId, $identifierType);
107:
108: $link = $product->getLinkInstance()
109: ->setLinkTypeId($typeId);
110:
111: $collection = $this->_initCollection($link, $product);
112:
113: $links = $this->_collectionToEditableArray($collection);
114:
115: $idBySku = $product->getIdBySku($linkedProductId);
116: if ($idBySku) {
117: $linkedProductId = $idBySku;
118: }
119:
120: foreach ($collection->getLinkModel()->getAttributes() as $attribute) {
121: if (isset($data->$attribute['code'])) {
122: $links[(int)$linkedProductId][$attribute['code']] = $data->$attribute['code'];
123: }
124: }
125:
126: try {
127: if ($type == 'grouped') {
128: $link->getResource()->saveGroupedLinks($product, $links, $typeId);
129: } else {
130: $link->getResource()->saveProductLinks($product, $links, $typeId);
131: }
132:
133: $_linkInstance = Mage::getSingleton('catalog/product_link');
134: $_linkInstance->saveProductRelations($product);
135:
136: $indexerStock = Mage::getModel('cataloginventory/stock_status');
137: $indexerStock->updateStatus($productId);
138:
139: $indexerPrice = Mage::getResourceModel('catalog/product_indexer_price');
140: $indexerPrice->reindexProductIds($productId);
141:
142: } catch (Exception $e) {
143: $this->_fault('data_invalid', Mage::helper('catalog')->__('Link product does not exist.'));
144: }
145:
146: return true;
147: }
148: }
149: