Forums › Forums › OroCRM › OroCRM – How do I? Questions › Import product category from Magento
This topic contains 2 replies, has 2 voices, and was last updated by Mike Kudelya 7 years, 3 months ago.
Starting from March 1, 2020 the forum has been switched to the read-only mode. Please head to StackOverflow for support.
- CreatorTopic
- November 23, 2016 at 2:15 am #29146
Hello,
We are successfully sync order data from Magento to ORO. We have the information of the product purchased BUT also we need the category of the product.
We wonder if there is any way to import the product category too
Thanks in advance!
- CreatorTopic
- AuthorReplies
- November 29, 2016 at 7:24 am #29147
Hi
At the moment orocrm doesn’t support product category import, but i have wrote example which allows you see category of quote item. Replace your app/code/community/Oro/Api/Model/Sales/Quote/Api.php with following code:
PHP123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205<?php/*** Oro Inc.** NOTICE OF LICENSE** This source file is subject to the Open Software License (OSL 3.0)* that is published at http://opensource.org/licenses/osl-3.0.php.* If you did not receive a copy of the license and are unable to* obtain it through the world-wide-web, please send an email* to license@magecore.com so we can send you a copy immediately** @category Oro* @package Api* @copyright Copyright 2013 Oro Inc. (http://oroinc.com/orocrm)* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)*/class Oro_Api_Model_Sales_Quote_Apiextends Mage_Checkout_Model_Api_Resource{/*** @var array*/protected $_knownAttributes = array();protected $_categoryCollection;/*** @var Oro_Api_Helper_Data*/protected $_apiHelper;public function __construct(){$this->_categoryCollection = Mage::getResourceModel('catalog/category_collection')->addAttributeToSelect('name');$this->_apiHelper = Mage::helper('oro_api');}/*** Retrieve list of quotes. Filtration could be applied** @param array|object $filters* @param \stdClass $pager** @return array*/public function items($filters, $pager){/** @var Mage_Sales_Model_Resource_Quote_Collection $quoteCollection */$quoteCollection = Mage::getResourceModel('sales/quote_collection');$filters = $this->_apiHelper->parseFilters($filters);try {foreach ($filters as $field => $value) {$quoteCollection->addFieldToFilter($field, $value);}} catch (Mage_Core_Exception $e) {$this->_fault('filters_invalid', $e->getMessage());}$quoteCollection->setOrder('entity_id', Varien_Data_Collection_Db::SORT_ORDER_ASC);if (!$this->_apiHelper->applyPager($quoteCollection, $pager)) {// there's no such page, so no results for itreturn array();}$this->_preparesGiftMessages($quoteCollection);$resultArray = array();/** @var Mage_Sales_Model_Quote $quote */foreach ($quoteCollection as $quote) {$row = $quote->__toArray();$attributes = $this->_apiHelper->getNotIncludedAttributes($quote, $row, $this->_getKnownQuoteAttributes());if ($attributes) {$row['attributes'] = $attributes;}$row = array_merge($row, $this->info($quote));$resultArray[] = $row;}return $resultArray;}/*** Retrieve full information about quote** @param Mage_Sales_Model_Quote $quote* @return array*/protected function info($quote){$result = $this->_getAttributes($quote, 'quote');$result['shipping_address'] = $this->_getAttributes($quote->getShippingAddress(), 'quote_address');$result['billing_address'] = $this->_getAttributes($quote->getBillingAddress(), 'quote_address');$result['items'] = array();/** @var Mage_Sales_Model_Quote_Item $item */foreach ($quote->getAllItems() as $item) {$quoteItem = $this->_getAttributes($item, 'quote_item');$productAttributes = $this->_getProductAttributes($item);$quoteItem = array_merge($quoteItem, $productAttributes);$result['items'][] = $quoteItem;}$result['payment'] = $this->_getAttributes($quote->getPayment(), 'quote_payment');if (isset($result['payment'], $result['payment']['additional_information'])&& is_array($result['payment']['additional_information'])) {$result['payment']['additional_information'] = serialize($result['payment']['additional_information']);}return $result;}/*** @param Mage_Sales_Model_Quote_Item $item* @return array*/protected function _getProductAttributes($item){$categories = [];$result = array();$product = $item->getProduct();if ($product) {$productImage = $product->getData('image');if ($productImage) {$result['product_image_url'] = Mage::getSingleton('catalog/product_media_config')->getMediaUrl($productImage);}$result['product_url'] = $product->getProductUrl(false);$productCategoryIds = $product->getCategoryIds();foreach ($productCategoryIds as $categoryId) {$categories[] = $this->_categoryCollection->getItemById($categoryId)->getName();}if ($categories) {$result['categories'] = implode(', ', $categories);}}return $result;}/*** Get list of attributes exposed to API.** @return array*/protected function _getKnownQuoteAttributes(){if (!$this->_knownAttributes) {$this->_knownAttributes = array_merge($this->_apiHelper->getComplexTypeScalarAttributes('salesQuoteEntity'),array('entity_id'));}return $this->_knownAttributes;}/*** Set gift_message key to quote and quote item** @param Mage_Sales_Model_Resource_Quote_Collection $quoteCollection*/protected function _preparesGiftMessages($quoteCollection){$messageIds = array();/* @var Mage_Sales_Model_Quote $quote */foreach ($quoteCollection as $quote) {if ($quote->getGiftMessageId()) {$messageIds[] = $quote->getGiftMessageId();}foreach ($quote->getAllItems() as $quoteItem) {if ($quoteItem->getGiftMessageId()) {$messageIds[] = $quoteItem->getGiftMessageId();}}}if (!$messageIds) {return;}$messageIds = array_unique($messageIds);$giftCollection = Mage::getResourceModel('giftmessage/message_collection');$giftCollection->addFieldToFilter('gift_message_id', array('in' => $messageIds));/* @var Mage_Sales_Model_Quote $quote */foreach ($quoteCollection as $quote) {if ($quote->getGiftMessageId()) {$quote->setGiftMessage($giftCollection->getItemById($quote->getGiftMessageId())->getMessage());}foreach ($quote->getAllItems() as $quoteItem) {if ($quoteItem->getGiftMessageId()) {$quoteItem->setGiftMessage($giftCollection->getItemById($quoteItem->getGiftMessageId())->getMessage());}}}}}Add to “<complexType name=”shoppingCartItemEntity”>” section of app/code/community/Oro/Api/etc/wsdl.xml file new categories element:
XHTML1<element name="categories" type="xsd:string" minOccurs="0"/>After that clear magento cache. Add to orocrm ‘Magento Shopping Cart Item’ entity ‘categories’ text field, add your new field to grid and try to sync.
December 13, 2016 at 4:06 am #29148Master branch of OroCRM Bridge Magento Extension already contains this feature.
- AuthorReplies
The forum ‘OroCRM – How do I? Questions’ is closed to new topics and replies.