00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class Mage_Catalog_Model_Product_Link_Api_V2 extends Mage_Catalog_Model_Product_Link_Api
00035 {
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 public function assign($type, $productId, $linkedProductId, $data = array())
00046 {
00047 $typeId = $this->_getTypeId($type);
00048
00049 $product = $this->_initProduct($productId);
00050
00051 $link = $product->getLinkInstance()
00052 ->setLinkTypeId($typeId);
00053
00054 $collection = $this->_initCollection($link, $product);
00055 $idBySku = $product->getIdBySku($linkedProductId);
00056 if ($idBySku) {
00057 $linkedProductId = $idBySku;
00058 }
00059
00060 $links = $this->_collectionToEditableArray($collection);
00061
00062 $links[(int)$linkedProductId] = array();
00063 foreach ($collection->getLinkModel()->getAttributes() as $attribute) {
00064 if (isset($data->$attribute['code'])) {
00065 $links[(int)$linkedProductId][$attribute['code']] = $data->$attribute['code'];
00066 }
00067 }
00068
00069 try {
00070 $link->getResource()->saveProductLinks($product, $links, $typeId);
00071 } catch (Exception $e) {
00072 $this->_fault('data_invalid', Mage::helper('catalog')->__('Link product not exists.'));
00073 }
00074
00075 return true;
00076 }
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087 public function update($type, $productId, $linkedProductId, $data = array())
00088 {
00089 $typeId = $this->_getTypeId($type);
00090
00091 $product = $this->_initProduct($productId);
00092
00093 $link = $product->getLinkInstance()
00094 ->setLinkTypeId($typeId);
00095
00096 $collection = $this->_initCollection($link, $product);
00097
00098 $links = $this->_collectionToEditableArray($collection);
00099
00100 $idBySku = $product->getIdBySku($linkedProductId);
00101 if ($idBySku) {
00102 $linkedProductId = $idBySku;
00103 }
00104
00105 foreach ($collection->getLinkModel()->getAttributes() as $attribute) {
00106 if (isset($data->$attribute['code'])) {
00107 $links[(int)$linkedProductId][$attribute['code']] = $data->$attribute['code'];
00108 }
00109 }
00110
00111 try {
00112 $link->getResource()->saveProductLinks($product, $links, $typeId);
00113 } catch (Exception $e) {
00114 $this->_fault('data_invalid', Mage::helper('catalog')->__('Link product not exists.'));
00115 }
00116
00117 return true;
00118 }
00119 }