I am trying to update group prices programmatically, my prices come from a CSV file, The error I am getting is
Fatal error: Call to a member function setData() on a non-object in
/chroot/home/radmodne/radmod.net/html/import_attribute.php on line 129
I have put together this script from others I found online, what it does it update all prices and qty based on an attribute. This part this working fine, what doesn't work is updating the group prices part I just added. I suspect the default magento functions are not included which is causing my issue, but i don't know for sure.
$mageFilename = 'app/Mage.php'; require_once $mageFilename; Mage::setIsDeveloperMode(true); ini_set('display_errors', 1); umask(0); Mage::app('admin'); Mage::register('isSecureArea', 1); Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); set_time_limit(0); ini_set('memory_limit','1024M'); /***************** UTILITY FUNCTIONS ********************/ function _getConnection($type = 'core_read'){ return Mage::getSingleton('core/resource')->getConnection($type); } function _getTableName($tableName){ return Mage::getSingleton('core/resource')->getTableName($tableName); } function _getAttributeId($attribute_code = 'price'){ $connection = _getConnection('core_read'); $sql = "SELECT attribute_id FROM " . _getTableName('eav_attribute') . " WHERE entity_type_id = ? AND attribute_code = ?"; $entity_type_id = _getEntityTypeId(); return $connection->fetchOne($sql, array($entity_type_id, $attribute_code)); } function _getEntityTypeId($entity_type_code = 'catalog_product'){ $connection = _getConnection('core_read'); $sql = "SELECT entity_type_id FROM " . _getTableName('eav_entity_type') . " WHERE entity_type_code = ?"; return $connection->fetchOne($sql, array($entity_type_code)); } function _checkIfSkuExists($sku){ $connection = _getConnection('core_read'); $sql = "SELECT COUNT(*) AS count_no FROM " . _getTableName('catalog_product_entity_varchar') . " WHERE value = ?"; $count = $connection->fetchOne($sql, array($sku)); if($count > 0){ return true; }else{ return false; } } function _getIdFromSku($sku){ $connection = _getConnection('core_read'); $sql = "SELECT entity_id FROM " . _getTableName('catalog_product_entity_varchar') . " WHERE value = ?"; return $connection->fetchOne($sql, array($sku)); } function _updateStocks($data){ $connection = _getConnection('core_write'); $sku = $data[0]; $newQty = $data[1]; //$productId = _getIdFromSku($sku); $attributeId = _getAttributeId(); $sql_prod = "SELECT entity_id FROM " . _getTableName('catalog_product_entity_varchar') . " WHERE value = $sku"; $sqlQuery = $connection->query($sql_prod); while ($row = $sqlQuery->fetch() ) { $entity_id = $row['entity_id']; $sql = "UPDATE " . _getTableName('cataloginventory_stock_item') . " csi, " . _getTableName('cataloginventory_stock_status') . " css SET csi.qty = ?, csi.is_in_stock = ?, css.qty = ?, css.stock_status = ? WHERE csi.product_id = ? AND csi.product_id = css.product_id"; $isInStock = $newQty > 0 ? 1 : 0; $stockStatus = $newQty > 0 ? 1 : 0; $connection->query($sql, array($newQty, $isInStock, $newQty, $stockStatus, $entity_id)); } } function _updatePrices($data){ $connection = _getConnection('core_write'); $sku = $data[0]; $newPrice = $data[5]; $newPrice1 = $data[2]; $newPrice2 = $data[3]; $newPrice4 = $data[4]; $newPrice5 = $data[6]; //$productId = _getIdFromSku($sku); $attributeId = _getAttributeId(); $sql_prod = "SELECT entity_id FROM " . _getTableName('catalog_product_entity_varchar') . " WHERE value = $sku"; $sqlQuery = $connection->query($sql_prod); while ($row = $sqlQuery->fetch() ) { $entity_id = $row['entity_id']; $sql = "UPDATE " . _getTableName('catalog_product_entity_decimal') . " cped SET cped.value = ? WHERE cped.attribute_id = ? AND cped.entity_id = ?"; $connection->query($sql, array($newPrice, $attributeId, $entity_id)); $entity_id->setData('group_price',array()); $entity_id->save(); $g_PricingData = array ( array ('website_id'=>0, 'cust_group'=>5, 'price'=>$newPrice1), array ('website_id'=>0, 'cust_group'=>6, 'price'=>$newPrice2), array ('website_id'=>0, 'cust_group'=>7, 'price'=>$newPrice4), array ('website_id'=>0, 'cust_group'=>8, 'price'=>$newPrice5) ); $entity_id->setData('group_price',$g_PricingData); $entity_id->save(); } }
The code that its causing the issues is in my _updatePrices function
$entity_id->setData('group_price',array()); $entity_id->save(); $g_PricingData = array ( array ('website_id'=>0, 'cust_group'=>5, 'price'=>$newPrice1), array ('website_id'=>0, 'cust_group'=>6, 'price'=>$newPrice2), array ('website_id'=>0, 'cust_group'=>7, 'price'=>$newPrice4), array ('website_id'=>0, 'cust_group'=>8, 'price'=>$newPrice5) ); $entity_id->setData('group_price',$g_PricingData); $entity_id->save();
Solved! Go to Solution.
Ok, so this is a very hacky way to do it, but it works (i have 4 group prices). If someone can provide a better answer I am open to it.
With some data mining I found the group prices were stored in 4 tables, so i update all 4, with all 4 of my group prices..
/* catalog_product_entity_group_price table */ $sql_price_one = "UPDATE " . _getTableName('catalog_product_entity_group_price') . " cpegp SET cpegp.value = ? WHERE cpegp.customer_group_id = ? AND cpegp.entity_id = ?"; $connection->query($sql_price_one, array($newPrice1, 5, $entity_id)); $sql_price_two = "UPDATE " . _getTableName('catalog_product_entity_group_price') . " cpegp SET cpegp.value = ? WHERE cpegp.customer_group_id = ? AND cpegp.entity_id = ?"; $connection->query($sql_price_two, array($newPrice2, 6, $entity_id)); $sql_price_four = "UPDATE " . _getTableName('catalog_product_entity_group_price') . " cpegp SET cpegp.value = ? WHERE cpegp.customer_group_id = ? AND cpegp.entity_id = ?"; $connection->query($sql_price_four, array($newPrice4, 7, $entity_id)); $sql_price_five = "UPDATE " . _getTableName('catalog_product_entity_group_price') . " cpegp SET cpegp.value = ? WHERE cpegp.customer_group_id = ? AND cpegp.entity_id = ?"; $connection->query($sql_price_five, array($newPrice5, 8, $entity_id)); /* catalog_product_index_group_price table */ $sql_price_one = "UPDATE " . _getTableName('catalog_product_index_group_price') . " cpigp SET cpigp.price = ? WHERE cpigp.customer_group_id = ? AND cpigp.entity_id = ?"; $connection->query($sql_price_one, array($newPrice1, 5, $entity_id)); $sql_price_two = "UPDATE " . _getTableName('catalog_product_index_group_price') . " cpigp SET cpigp.price = ? WHERE cpigp.customer_group_id = ? AND cpigp.entity_id = ?"; $connection->query($sql_price_two, array($newPrice2, 6, $entity_id)); $sql_price_four = "UPDATE " . _getTableName('catalog_product_index_group_price') . " cpigp SET cpigp.price = ? WHERE cpigp.customer_group_id = ? AND cpigp.entity_id = ?"; $connection->query($sql_price_four, array($newPrice4, 7, $entity_id)); $sql_price_five = "UPDATE " . _getTableName('catalog_product_index_group_price') . " cpigp SET cpigp.price = ? WHERE cpigp.customer_group_id = ? AND cpigp.entity_id = ?"; $connection->query($sql_price_five, array($newPrice5, 8, $entity_id)); /* catalog_product_index_price table */ $sql_price_one = "UPDATE " . _getTableName('catalog_product_index_price') . " cpip SET cpip.group_price = ? WHERE cpip.customer_group_id = ? AND cpip.entity_id = ?"; $connection->query($sql_price_one, array($newPrice1, 5, $entity_id)); $sql_price_two = "UPDATE " . _getTableName('catalog_product_index_price') . " cpip SET cpip.group_price = ? WHERE cpip.customer_group_id = ? AND cpip.entity_id = ?"; $connection->query($sql_price_two, array($newPrice2, 6, $entity_id)); $sql_price_four = "UPDATE " . _getTableName('catalog_product_index_price') . " cpip SET cpip.group_price = ? WHERE cpip.customer_group_id = ? AND cpip.entity_id = ?"; $connection->query($sql_price_four, array($newPrice4, 7, $entity_id)); $sql_price_five = "UPDATE " . _getTableName('catalog_product_index_price') . " cpip SET cpip.group_price = ? WHERE cpip.customer_group_id = ? AND cpip.entity_id = ?"; $connection->query($sql_price_five, array($newPrice5, 8, $entity_id)); /* catalog_product_index_price_tmp table */ $sql_price_one = "UPDATE " . _getTableName('catalog_product_index_price_tmp') . " cpipt SET cpipt.group_price = ? WHERE cpipt.customer_group_id = ? AND cpipt.entity_id = ?"; $connection->query($sql_price_one, array($newPrice1, 5, $entity_id)); $sql_price_two = "UPDATE " . _getTableName('catalog_product_index_price_tmp') . " cpipt SET cpipt.group_price = ? WHERE cpipt.customer_group_id = ? AND cpipt.entity_id = ?"; $connection->query($sql_price_two, array($newPrice2, 6, $entity_id)); $sql_price_four = "UPDATE " . _getTableName('catalog_product_index_price_tmp') . " cpipt SET cpipt.group_price = ? WHERE cpipt.customer_group_id = ? AND cpipt.entity_id = ?"; $connection->query($sql_price_four, array($newPrice4, 7, $entity_id)); $sql_price_five = "UPDATE " . _getTableName('catalog_product_index_price_tmp') . " cpipt SET cpipt.group_price = ? WHERE cpipt.customer_group_id = ? AND cpipt.entity_id = ?"; $connection->query($sql_price_five, array($newPrice5, 8, $entity_id));
Ok, so this is a very hacky way to do it, but it works (i have 4 group prices). If someone can provide a better answer I am open to it.
With some data mining I found the group prices were stored in 4 tables, so i update all 4, with all 4 of my group prices..
/* catalog_product_entity_group_price table */ $sql_price_one = "UPDATE " . _getTableName('catalog_product_entity_group_price') . " cpegp SET cpegp.value = ? WHERE cpegp.customer_group_id = ? AND cpegp.entity_id = ?"; $connection->query($sql_price_one, array($newPrice1, 5, $entity_id)); $sql_price_two = "UPDATE " . _getTableName('catalog_product_entity_group_price') . " cpegp SET cpegp.value = ? WHERE cpegp.customer_group_id = ? AND cpegp.entity_id = ?"; $connection->query($sql_price_two, array($newPrice2, 6, $entity_id)); $sql_price_four = "UPDATE " . _getTableName('catalog_product_entity_group_price') . " cpegp SET cpegp.value = ? WHERE cpegp.customer_group_id = ? AND cpegp.entity_id = ?"; $connection->query($sql_price_four, array($newPrice4, 7, $entity_id)); $sql_price_five = "UPDATE " . _getTableName('catalog_product_entity_group_price') . " cpegp SET cpegp.value = ? WHERE cpegp.customer_group_id = ? AND cpegp.entity_id = ?"; $connection->query($sql_price_five, array($newPrice5, 8, $entity_id)); /* catalog_product_index_group_price table */ $sql_price_one = "UPDATE " . _getTableName('catalog_product_index_group_price') . " cpigp SET cpigp.price = ? WHERE cpigp.customer_group_id = ? AND cpigp.entity_id = ?"; $connection->query($sql_price_one, array($newPrice1, 5, $entity_id)); $sql_price_two = "UPDATE " . _getTableName('catalog_product_index_group_price') . " cpigp SET cpigp.price = ? WHERE cpigp.customer_group_id = ? AND cpigp.entity_id = ?"; $connection->query($sql_price_two, array($newPrice2, 6, $entity_id)); $sql_price_four = "UPDATE " . _getTableName('catalog_product_index_group_price') . " cpigp SET cpigp.price = ? WHERE cpigp.customer_group_id = ? AND cpigp.entity_id = ?"; $connection->query($sql_price_four, array($newPrice4, 7, $entity_id)); $sql_price_five = "UPDATE " . _getTableName('catalog_product_index_group_price') . " cpigp SET cpigp.price = ? WHERE cpigp.customer_group_id = ? AND cpigp.entity_id = ?"; $connection->query($sql_price_five, array($newPrice5, 8, $entity_id)); /* catalog_product_index_price table */ $sql_price_one = "UPDATE " . _getTableName('catalog_product_index_price') . " cpip SET cpip.group_price = ? WHERE cpip.customer_group_id = ? AND cpip.entity_id = ?"; $connection->query($sql_price_one, array($newPrice1, 5, $entity_id)); $sql_price_two = "UPDATE " . _getTableName('catalog_product_index_price') . " cpip SET cpip.group_price = ? WHERE cpip.customer_group_id = ? AND cpip.entity_id = ?"; $connection->query($sql_price_two, array($newPrice2, 6, $entity_id)); $sql_price_four = "UPDATE " . _getTableName('catalog_product_index_price') . " cpip SET cpip.group_price = ? WHERE cpip.customer_group_id = ? AND cpip.entity_id = ?"; $connection->query($sql_price_four, array($newPrice4, 7, $entity_id)); $sql_price_five = "UPDATE " . _getTableName('catalog_product_index_price') . " cpip SET cpip.group_price = ? WHERE cpip.customer_group_id = ? AND cpip.entity_id = ?"; $connection->query($sql_price_five, array($newPrice5, 8, $entity_id)); /* catalog_product_index_price_tmp table */ $sql_price_one = "UPDATE " . _getTableName('catalog_product_index_price_tmp') . " cpipt SET cpipt.group_price = ? WHERE cpipt.customer_group_id = ? AND cpipt.entity_id = ?"; $connection->query($sql_price_one, array($newPrice1, 5, $entity_id)); $sql_price_two = "UPDATE " . _getTableName('catalog_product_index_price_tmp') . " cpipt SET cpipt.group_price = ? WHERE cpipt.customer_group_id = ? AND cpipt.entity_id = ?"; $connection->query($sql_price_two, array($newPrice2, 6, $entity_id)); $sql_price_four = "UPDATE " . _getTableName('catalog_product_index_price_tmp') . " cpipt SET cpipt.group_price = ? WHERE cpipt.customer_group_id = ? AND cpipt.entity_id = ?"; $connection->query($sql_price_four, array($newPrice4, 7, $entity_id)); $sql_price_five = "UPDATE " . _getTableName('catalog_product_index_price_tmp') . " cpipt SET cpipt.group_price = ? WHERE cpipt.customer_group_id = ? AND cpipt.entity_id = ?"; $connection->query($sql_price_five, array($newPrice5, 8, $entity_id));