I am working on web-services of Magento. I need to show a sold product by the pos application in Magento backend.
Magento – Create sale in magento using pos application
magento-1.7magento-enterprise
Related Solutions
Include Mage.php from Magento in your external application
// Include Magento application
require_once ( "/your-magento-directory/app/Mage.php" );
umask(0);
// Initialize Magento
Mage::app("default");
You have two options then, for the session name, "frontend" for frontend session or "adminhtml" for admin session:
Mage::getSingleton("core/session", array("name" => "frontend"));
$session = Mage::getSingleton("customer/session");
This will get the session of the current logged in user.
And this will login to Magento from outside:
$session = Mage::getSingleton('customer/session', array('name' => 'frontend'));
$session->login($row['mail'], $row['field_magentopass_value']);
$session->setCustomerAsLoggedIn($session->getCustomer());
$row['mail']
is the email with user is registered
$row['field_magentopass_value']
is the password
Source: http://louisjohn.wordpress.com/category/magento/magento-functions-outside-magento-folder/
You cannot sort the products by position
field.
This field has meaning only in relation with a category. There is no position
attribute for the product (unless you added one by yourself).
You can get all the data you need with one (not so simple) single select. Here goes:
$res = Mage::getSingleton('core/resource');
$eav = Mage::getModel('eav/config');
$nameattr = $eav->getAttribute('catalog_category', 'name');
$nametable = $res->getTableName('catalog/category') . '_' . $nameattr->getBackendType();
$nameattrid = $nameattr->getAttributeId();
//you could add here to select only the attributes you need but I'm lazy and I added all of them (*)
$collection = Mage::getModel('catalog/product')
->getCollection()->addAttributeToSelect('*');
$collection->joinTable('catalog/category_product',
'product_id=entity_id', array('single_category_id' => 'category_id', 'position_in_category'=>'position'),
null, 'left');
$collection->joinTable(
$nametable,
'entity_id=single_category_id',
array('single_category_name' => 'value'),
"attribute_id=$nameattrid",
'left'
);
$collection->groupByAttribute('entity_id');
$collection->getSelect()
->columns(array('category_ids_values' => new Zend_Db_Expr("IFNULL(GROUP_CONCAT(`catalog_category_product`.`category_id` SEPARATOR ','), '')")))
->columns(array('position_values' => new Zend_Db_Expr("IFNULL(GROUP_CONCAT(`catalog_category_product`.`position` SEPARATOR ','), '')")))
->columns(array('category_names' => new Zend_Db_Expr("IFNULL(GROUP_CONCAT(`{$nametable}`.`value` SEPARATOR ','), '')")));
echo $collection->getSelect();
$final['prodCat_list'] = array();
$i = 0;
foreach ($collection as $product){
$item = array();
$item['sku']= $product->getSku();
$item['product_id']= $product->getId();
$item['name']= $product->getName();
$item['image']=$product->getThumbnailUrl();
$item['description']=$product->getDescription();
$item['short_description']=$product->getShortDescription();
$item['weight']=$product->getWeight();
$item['created_at']=$product->getCreatedAt();
$item['updated_at']=$product->getUpdatedAt();
$item['price']=$product->getPrice();
$item['special_price']=$product->getSpecialPrice();
$item['tax_class_id']=$product->getTaxClassId();
//this can be retrieved directly with $product->getCategoryIds, but they might be in a different order.
$item['cat_id'] = $product->getCategoryIdsValues();
$item['positions'] = $product->getPositionValues();
$item['category_names'] = $product->getCategoryNames();
$final['prodCat_list'][] = $item;
}
This code returns and array with product data. An element looks something like this:
[5] => Array
(
[sku] => MA464LL/A
[product_id] => 25
[name] => Product name here
[image] => image/link here
[description] => Description here.
[short_description] => Short description here
[weight] => 10.6000
[created_at] => 2007-08-24 14:28:50
[updated_at] => 2008-07-28 21:27:34
[price] => 2299.9900
[special_price] =>
[tax_class_id] => 2
[cat_id] => 15,28
[positions] => 0,0
[category_names] => Computers,Laptops
)
Best Answer
The best method will be to use the Magento SOAP API v2 with WS-I compliance enabled as this version is compatible with .NET, Java and the like.
What you need to do:
I'll include code from an example PHP class so that you get an idea how it works. Your programming language may vary but the basics and calls should be the same.
Start an API session
If you didn't do it yet you need to create an API user (see
System > Web Services > SOAP/XML-RPC - Roles
andSystem > Web Services > SOAP/XML-RPC - Users
).Use the credentials you entered to authenticate and obtain a session key in your script.
MagentoSoapClient is an example class I wrote for this question. I published it on GitHub so it's easier to follow the code: Magento SOAP v2 WS-I Examples. In the following sections I'll show the main calls (like above) and explain the called methods (like beneath).
When using Magento SOAP API v2 with WS-I compliance enabled, you have to pass the arguments as an object (or associative array in PHP).
$this->_client
is the SOAP client instance.login
is the API method you have to call.The response you get from Magento is an object.
result
contains the actual data.Log in using the credentials you entered in the backend.
The
login
method returns a session ID. Keep it. From now on you'll need it for every call to identify yourself.Documentation: http://www.magentocommerce.com/api/soap/introduction.html
Create a cart
Create a cart (or "quote"). The cart will be transformed into an order in the last step.
The response is a cart id (or "quote id"). Keep it. You will need it for the following steps.
shoppingCartCreate
is the API method you want to call. Please note the session ID that I pass with every call.Please follow the documentation links for all arguments that you can pass on to Magento. For example I'm omitting the store codes but you may want to create the order in a particular store.
Documentation: http://www.magentocommerce.com/api/soap/checkout/cart/cart.create.html
Add the products to the cart
Let's add a simple product to the cart.
Yes, it's pretty simple (as long as you don't have options or more complex products). You can add several products in one call.
The called API method is
shoppingCartProductAdd
. Please note that from now on always we're not only passing the session ID but also the cart ID ("quote ID").Documentation: http://www.magentocommerce.com/api/soap/checkout/cartProduct/cart_product.add.html
Set the customer for the cart
Now you need to set a customer for the cart.
This is as simple as it gets. Depending on your business needs you may want to use an existing Magento customer or register a new one. For simplicity I checkout as a guest.
The method is
shoppingCartCustomerSet
.Documentation: http://www.magentocommerce.com/api/soap/checkout/cartCustomer/cart_customer.set.html
Add the billing and shipping address to the customer
In the next step, you have to set the billing and shipping address.
Note that you have to provide special "regions" and "country IDs". You may have to retrieve the available countries and available regions from Magento and build a mapping to translate your codes into Magentos codes. Or you provide fixed data if you don't have to care about the addresses.
This one and the following calls return a boolean value which tells you if the action was successful.
Again, not much to see here.
shoppingCartCustomerAddresses
is the API method to go.Documentation: http://www.magentocommerce.com/api/soap/checkout/cartCustomer/cart_customer.addresses.html
Set the shipping method
Now you have to define the shipping method.
I used the default shipping method but you may want to retrieve a list of available shipping methods using shoppingCartShippingList.
The shipping method is set using the API method
shoppingCartShippingMethod
.Documentation: http://www.magentocommerce.com/api/soap/checkout/cartShipping/cart_shipping.method.html
Set the payment method
Now we're doing the same for the payment method.
Again, this is a default payment method ("check money order"). You can retrieve the list of available payment methods using shoppingCartPaymentList.
As you can see the API method is
shoppingCartPaymentMethod
. Have a look at the documentation for additional data you can pass here.Documentation: http://www.magentocommerce.com/api/soap/checkout/cartPayment/cart_payment.method.html
Create the order
Finally! We can create the order.
The response is the "increment order ID" by Magento. If everything went well you will see an order in your Magento backend now. You can use the other calls mentioned in the documentation to get more information on the order.
Our last API call is
shoppingCartOrder
.Documentation: http://www.magentocommerce.com/api/soap/checkout/cart/cart.order.html
Conclusion
As long as you don't become a victim of bugs, the Magento API is not too difficult to handle (once you get a grip on it).
Be sure to activate the WSDL cache in the backend as calls can take a long time otherwise. With all caches enabled, the calls should be fast (excluding the last one to create the order, this may take a few seconds).
If you find that Magento is blocking your POS application you may have to implement a queue and create the orders asynchronously.