Does anyone know a way to write and read the Customer Group Prices with API v2? We are using a tool to integrate our financial software with Magento. Each customer has a different agreed price for products in Dynamics GP and we need to match this price in Magento. This way, when a Customer logs into the Magento site, their agreed price will be displayed in all simple products.
1 Answer
I've done this for importing group prices with SOAPv2 for Magento CE 1.7.0.2 some time ago and wrote a a (german) blogpost about this (Google translate might help). I added the important parts with an english explanation here and hope this helps you getting started.
Basically you need to rewrite the catalog/product_api_v2
model and extend the _prepareDataForSave
method.
Create a new module
In my example it's Avoe_Catalog:
In Avoe/Catalog/etc/config.xml
rewrite the Product API:
<config>
<modules>
<Avoe_Catalog>
<version>0.0.1</version>
</Avoe_Catalog>
</modules>
<global>
<models>
<avoecatalog>
<class>Avoe_Catalog_Model</class>
</avoecatalog>
<catalog>
<rewrite>
<product_api_v2>Avoe_Catalog_Model_Product_Api_V2</product_api_v2>
</rewrite>
</catalog>
</models>
</global>
</config>
Avoe/Catalog/Model/Product/Api/V2.php:
copy the _prepareDataForSave
method from Mage_Catalog_Model_Product_Api_V2
to your new Api model:
class Avoe_Catalog_Model_Product_Api_V2 extends Mage_Catalog_Model_Product_Api_V2 {
protected function _prepareDataForSave ($product, $productData) {
...
if (property_exists($productData, 'tier_price')) {
...
}
//add the following code
if (property_exists($productData, 'group_price')) {
$groupPrices = Mage::getModel('avoecatalog/product_attribute_groupprice_api_V2')
->prepareGroupPrices($product, $productData->group_price);
$product->setData("group_price", $groupPrices);
}
}
}
Create the following file:
Avoe/Catalog/Model/Product/Attribute/Groupprice/Api/V2.php
copied from Mage/Catalog/Model/Product/Attribute/Tierprice/Api/V2.php
:
In this copied file, I've replaced all $tierPrices
with $groupPrices
and updated checks with needed options (cust_group, price, website_id).
class Avoe_Catalog_Model_Product_Attribute_Groupprice_Api_V2 extends Mage_Catalog_Model_Api_Resource
{
/**
* Prepare group prices for save
*
* @param Mage_Catalog_Model_Product $product
* @param array $groupPrices
* @return array
*/
public function prepareGroupPrices($product, $groupPrices = null)
{
if (!is_array($groupPrices)) {
return null;
}
$updateValue = array();
foreach ($groupPrices as $groupPrice1) {
$groupPrice = unserialize($groupPrice1);
if (!is_array($groupPrice)
|| !isset($groupPrice["cust_group"])
|| !isset($groupPrice["price"])) {
$this->_fault('data_invalid', Mage::helper('catalog')->__('Invalid Group Prices'));
}
if (!isset($groupPrice["website_id"]) || $groupPrice["website_id"] == 'all') {
$groupPrice["website_id"] = 0;
} else {
try {
$groupPrice["website_id"] = Mage::app()->getWebsite($groupPrice["website"])->getId();
} catch (Mage_Core_Exception $e) {
$groupPrice["website_id"] = 0;
}
}
if (intval($groupPrice["website_id"]) > 0 && !in_array($groupPrice["website_id"], $product->getWebsiteIds())) {
$this->_fault('data_invalid', Mage::helper('catalog')->__('Invalid group prices. The product is not associated to the requested website.'));
}
if (!isset($groupPrice["cust_group"])) {
$groupPrice["cust_group"] = '0';
}
if ($groupPrice["cust_group"] == 'all') {
$groupPrice["cust_group"] = '0';
}
$updateValue[] = array(
'website_id' => $groupPrice["website_id"],
'cust_group' => $groupPrice["cust_group"],
'price' => $groupPrice["price"],
'delete' => $groupPrice["delete"]
);
}
return $updateValue;
}
}
SOAPv2 API call
You can insert group prices via SOAPv2 then the following way:
$newProductData = new stdClass();
$additionalAttrs = array();
$gp = array(
"0" => array (
"website_id" => "0",
"cust_group" => "1",
"price" => "11.11",
"delete" => "1"
),
"1" => array (
"website_id" => "0",
"cust_group" => "0",
"price" => "12.12"
)
);
$gp = array("key" => "group_price", "value" => array(serialize($gp[0]),serialize($gp[1])));
$additionalAttrs['multi_data'][] = $gp;
$newProductData->additional_attributes = $additionalAttrs;
$result = $client->catalogProductUpdate($sessionId,$prodSku,$newProductData,"","sku");
-
-
I created the config.xml, create the new API model, and replaced "tierPrices" with "groupPrices" in the V2 file. I cannot write to the fields though. Should the groupPrices be displayed in the wsdl?– StevensFeb 23, 2015 at 6:22
-
I've updated my answer above with the code from Avoe_Catalog_Model_Product_Attribute_Groupprice_Api_V2. No, the group prices will not show up in the WSDL. They are part of the catalogProductUpdateRequestParam as productData option (typens:catalogProductCreateEntity). catalogProductCreateEntity contains the additional_attributes option where the group_price is set to. Feb 23, 2015 at 7:09
-
1Is it possible to add the group_price to WSDL as tier_price is done? I see that @massimilianoarceri did something similar here <stackoverflow.com/a/24576576/3005408>– StevensMar 3, 2015 at 15:09
-
1As "config.xml", I have to put "<global>" inside "<config>" as below to avoid errors like "Uncaught SoapFault exception: Duplicate website group price customer group."
<config> <modules> <Avoe_Catalog> <version>0.0.1</version> </Avoe_Catalog> </modules> <global> <models> <avoecatalog> <class>Avoe_Catalog_Model</class> </avoecatalog> <catalog> <rewrite> <product_api_v2>Avoe_Catalog_Model_Product_Api_V2</product_api_v2> </rewrite> </catalog> </models> </global> </config>
– Ted FengSep 20, 2015 at 4:05