Integrate Multi-Channel Sales With Magento API - magento

Looking for advice on the sequence of Magento API calls necessary to implement this business process:
An inventory item (either physical or virtual/digital) is made available by the seller via an external channel (not the regular web storefront).
A customer initiates a payment directly to me without going through the Magento cart / checkout flow (can I lookup sales tax at this point?)
After the payment has been made, I want to trigger Magento post-processing logic to record the sale, manage inventory, etc.
For physical goods, I want to trigger Magento fulfillment logic to occur to create the shipment, etc.
I'm aware of the SOAP API, I'm looking for help to understand which actions need to be taken along the way to enact this process.

Here is very basic example how Magento API can be used for your case:
Connect to Magento via API
$user = 'apiUser'; $password = 'apiKey';
$proxy = new SoapClient('http://your_magento_host.com/api/v2_soap/?wsdl');
$sessionId = $proxy->login($user, $password);
Create or select customer
// Create customer
$customerList = $proxy->customerCustomerCreate($sessionId, array( 'email' => 'customer#gmail.com', 'firstname' => 'Will', 'lastname' => 'Smith', 'password' => 'qwerty', 'website_id' => 1, 'store_id' => 1, 'group_id' => 1 ));
$customer = (array) $customerList[0];
$customer['mode'] = 'customer';
// Or select existing customer (by email)
$filter = array(
'complex_filter' => array(
array(
'key' => 'email',
'value' => array('key' => 'in', 'value' => 'customer#gmail.com')
)
) );
$customerList = $proxy->customerCustomerList($sessionId, $filter);
$customer = (array) $customerList[0];
$customer['mode'] = 'customer';
Create cart
$cartId = $proxy->shoppingCartCreate($sessionId, 1);
$proxy->shoppingCartCustomerSet($sessionId, $cartId, $customer);
Select product (by sku)
$filter = array(
'complex_filter' => array(
array(
'key' => 'sku',
'value' => array('key' => 'in', 'value' => 'T-SHIRT001')
)
) );
$productList = $proxy->catalogProductList($sessionId, $filter);
$product = (array) $productList[0];
$product['qty'] = 1;
Add product to cart
$proxy->shoppingCartProductAdd($sessionId, $cartId, array($product));
Set billing/shipping address. You should add this addresses to customer if you just create it before.
$address = array(
array(
'mode' => 'shipping',
'firstname' => $customer['firstname'],
'lastname' => $customer['lastname'],
'street' => 'street address',
'city' => 'city',
'region' => 'region',
'telephone' => 'phone number',
'postcode' => 'postcode',
'country_id' => 'country ID',
'is_default_shipping' => 0,
'is_default_billing' => 0
),
array(
'mode' => 'billing',
'firstname' => $customer['firstname'],
'lastname' => $customer['lastname'],
'street' => 'street address',
'city' => 'city',
'region' => 'region',
'telephone' => 'phone number',
'postcode' => 'postcode',
'country_id' => 'country ID',
'is_default_shipping' => 0,
'is_default_billing' => 0
),
);
$proxy->shoppingCartCustomerAddresses($sessionId, $cartId, $address);
Set shipping mathod
$proxy->shoppingCartShippingMethod($sessionId, $cartId, 'flatrate_flatrate');
Set payment method.
$paymentMethod = array(
'po_number' => null,
'method' => 'checkmo',
'cc_cid' => null,
'cc_owner' => null,
'cc_number' => null,
'cc_type' => null,
'cc_exp_year' => null,
'cc_exp_month' => null
);
$proxy->shoppingCartPaymentMethod($sessionId, $cartId, $paymentMethod);
Place order
$orderId = $proxy->shoppingCartOrder($sessionId, $cartId, null, null);
Now check Sales->Orders in Magento admin area and you'll see new order.
More details here: http://www.magentocommerce.com/api/soap/introduction.html

Yes, you can catch information about Tax:
1) Without order saving. Step 9:
$result = $proxy->shoppingCartTotals($sessionId, $cartId);<br>
var_dump($result);
You'll see array of subtotal, taxes, discounts and total.
2) With order saving. Step 10:
$result = $proxy->salesOrderInfo($sessionId, $orderId);<br>
var_dump($result);
// cancel order<br>
$result = $proxy->salesOrderCancel($sessionId, $orderId);
More information about used API calls here:
http://www.magentocommerce.com/api/soap/checkout/cart/cart.totals.html
http://www.magentocommerce.com/api/soap/sales/salesOrder/sales_order.info.html
http://www.magentocommerce.com/api/soap/sales/salesOrder/sales_order.cancel.html

Related

I cant get lastInsertId on laravel 7 project

$sql = DB::table('laravel_products')
->insert(array(
'name' => $name,
'price' => $price,
'qty' => $qty,
'description' => $description,
'uruu' => $uruu,
'garage' => $garage,
'duureg' => $duureg,
'tagt' => $tagt,
'talbai' => $talbai,
'haalga' => $haalga,
'tsonh' => $tsonh,
'shal' => $shal,
'tsonhtoo' => $ttsonh,
'hdawhar' => $bdawhar,
'lizing' => $lizing,
'utas' => $utas,
'email' => $email,
'hereg' => $hereg,
'bairshil' => $bairshil,
'bairlal' => $bairlal,
'ashig' => $ashigon,
'zahi' => $zahi,
'image' => $data
));
$lastInsertedID = $sql->lastInsertId();
When I try to insert its responses:
"Call to a member function lastInsertId() on bool"
I used insertGetId but its cant save multiple rows of pictures on mysql.
If you want to get the last inserted ID like that you can call that method on the PDO instance directly:
$id = DB::getPdo()->lastInsertId();
If the table has an auto-incrementing id, use the insertGetId method to insert a record and then retrieve the ID:
$id = DB::table('users')->insertGetId(
['email' => 'john#example.com', 'votes' => 0]
);
from : https://laravel.com/docs/5.8/queries#inserts
$data = new LaravelProducts(); //LaravelProducts is your Model Name
$data->name= $name; //here 'name' is your column name
$data->price= $price; //here 'price' is your column name
$data->qty= $qty; //here 'qty' is your column name
$data->description= $description; //here 'description' is your column name
..........
..........
$data->image= $image; //here 'image' is your column name
$data->save();
$lastInsertedId = $data->id;
You don't have to write a new query to collect last inserted id from database.
$laravel_product = DB::table('laravel_products')
->insertGetId( array(
'name' => $name,
'price' => $price,
'qty' => $qty,
'description' => $description,
'uruu' => $uruu,
'garage' => $garage,
'duureg' => $duureg,
'tagt' => $tagt,
'talbai' => $talbai,
'haalga' => $haalga,
'tsonh' => $tsonh,
'shal' => $shal,
'tsonhtoo' => $ttsonh,
'hdawhar' => $bdawhar,
'lizing' => $lizing,
'utas' => $utas,
'email' => $email,
'hereg' => $hereg,
'bairshil' => $bairshil,
'bairlal' => $bairlal,
'ashig' => $ashigon,
'zahi' => $zahi,
'image' => $data
)
);
foreach ($filenamesToSave as $filename) {
DB::insert('INSERT INTO laravel_products_images ( product_id, filename ) VALUES ( ?, ? )',[$laravel_product->id, $filename]);
return view('createproduct');
} // Foreach Closing
// Echo your inserted ID Like Below
echo $laravel_product->id;
It should be 100% working for you.

How to Add configurable product in cart using product id in magento api

I have creating webservices for my app but the problem id I am not been able to add configurable product in cart.Please find my code.Any help would be appreciated.
$customer_id = 'id_of_customer';
$cart_id = 'cart_id';
$quantity = 'quantity';
$store_id = 1;
if ($cart_id != '' AND $customer_id != '') {
$arrProducts = array(array(
"product_id" => '1887',
"sku" => 'sku_of_product',
"super_attribute" => array(151 => 3),
"qty" => 2,
));
$result = $proxy->shoppingCartProductAdd($sessionId, $cart_id, $arrProducts);
But I am getting the error Please specify the product's option(s).
Please use this code:
$product = array(
'product_id' => 19, // config product id
'sku' => 'H001',
'qty' => '1',
'super_attribute' => array(
0 => array(
'key' => 92, //attribute id
'value' => 10 //value
),
1 => array(
'key' => 134,
'value' => 3
)
)
);
$result = $proxy->shoppingCartProductAdd($session,$cartId, array($product));

Adding item to quote for getting product price in magento

I am using the below code for adding a product to quote object for getting the product total price. The getGrandTotal does not give any result. What could be the issue? How can i get total product price WITHOUT ADDING ITEM TO THE CART(as doing that would create issues in my application).
$data = $this->getRequest()->getParams();
$product = Mage::getModel('catalog/product')->load($data['product']);
$customer = Mage::getSingleton('customer/session')->getCustomer();
$bill_address_id = $customer->getDefaultBilling();
$ship_address_id = $customer->getDefaultShipping();
$bill_address = Mage::getModel('customer/address')->load($bill_address_id);
$ship_address = Mage::getModel('customer/address')->load($ship_address_id);
$store = Mage::app()->getStore();
$quote = Mage::getModel('sales/quote');
$quoteItem = Mage::getModel('sales/quote_item')->setProduct($product)->setQty($data['qty']);
$quote->addItem($quoteItem);
$quote->setStore($store);
$quote->getShippingAddress()->setCountryId($ship_address->getCountryId())
->setRegion($ship_address->getRegion())
->setPostcode($ship_address->getPostcode());
$quote->getShippingAddress()->setCollectShippingRates(true);
$quote->getShippingAddress()->collectShippingRates();
$quote->getShippingAddress()->setShippingMethod('flatrate_flatrate');
$quote->getShippingAddress()->collectTotals()->save();
Mage::log("Get Data for Quote:" . print_r($quote->getData(), true));
$totals = $quote->getGrandTotal();
Use below code:
$data = $this->getRequest()->getParams();
$product = Mage::getModel('catalog/product')->load($data['product']);
$customer = Mage::getSingleton('customer/session')->getCustomer();
$store = Mage::app()->getStore();
$quote = Mage::getModel('sales/quote');
$quoteItem = Mage::getModel('sales/quote_item')->setProduct($product)->setQty($data['qty']);
$quote->addItem($quoteItem);
$quote->setStore($store);
$billingAddressData = array(
'firstname' => 'Test',
'lastname' => 'Test',
'street' => 'Sample Street 10',
'city' => 'Somewhere',
'postcode' => '123456',
'telephone' => '123456',
'country_id' => 'US',
'region_id' => 12, // id from directory_country_region table
); // billing address
$shippingAddressData = array(
'firstname' => 'Test',
'lastname' => 'Test',
'street' => 'Sample Street 10',
'city' => 'Somewhere',
'postcode' => '123456',
'telephone' => '123456',
'country_id' => 'US',
'region_id' => 12, // id from directory_country_region table
);// shipping address
$billingAddress = $quote->getBillingAddress()->addData($billingAddressData);
$shippingAddress = $quote->getShippingAddress()->addData($shippingAddressData);
$shippingAddress->setCollectShippingRates(true)->collectShippingRates()
->setShippingMethod('flatrate_flatrate')
->setPaymentMethod('checkmo');
$quote->getPayment()->importData(array('method' => 'checkmo'));
$quote->collectTotals()->save();
$quote->save();
echo "Grand Total is = ". $totals = $quote->getGrandTotal();

Can't save multiselect attribute on Magento

I created a custom multiselect product attribute through installer. It works and I can save the product if I only select one option from the multiselect values. But if I select 2 values, the product still can be saved but came back up with 1 selected value again. In short, I can't save the attribute with 2 selected values.
$installer = $this;
$installer->startSetup();
$installer->addAttribute('catalog_product', 'attr_id',array(
'label' => 'Frontend Name',
'type' => 'int',
'input' => 'multiselect',
'backend' => 'eav/entity_attribute_backend_array',
'frontend' => '',
'source' => '',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => true,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'option' => array (
'value' => array(
'0' => array('First Option'),
'1' => array('Second Option'),
'2' => array('Third Option'),
)
),
'visible_on_front' => false,
'visible_in_advanced_search' => false,
'unique' => false
));
$installer->endSetup();
The problem comes from the type of your attribute.
'type'=> 'int',
The values from multiselect attributes are saved concatenated by comma 1,4,6. For this you need the attribute to be varchar or text. I recommend varchar if you are not going to have hundreds of options for the attribute.
The way is configured now, when it's saved, the value 1,4,6 is converted to int and it ends up being 1.
Modify you option array from
'option' => array (
'value' => array(
'0' => array('First Option'),
'1' => array('Second Option'),
'2' => array('Third Option'),
)
),
to
'option' => array (
'value' => array(
'first_option' => array('First Option'),
'second_option' => array('Second Option'),
'third_option' => array('Third Option'),
)
),
Multiselect will accept associated array.
I have found the solution myself .
open app/code/core/Mage/Adminhtml/controllers/Catalog/CategoryController.php
on save action after this
$category->setAttributeSetId($category->getDefaultAttributeSetId());
Please change language is your attribute name . you can change attribute name accordingly
$ga = "";
if($data['general']['language']){
foreach($data['general']['language'] as $a){
$ga .= $a.",";
}
$category->setLanguage(substr_replace($ga, "", -1));
}
Please replace language to your attribute name and it works...**
I am using SOAP API for entering products in magento shops. here is the full code
In the case of multiselect custom attribute.
$arrProductTime = explode(',', '136,139');
$result = $client->catalogProductCreate($session, 'simple', $attributeSet->set_id, 'product_sku1234', array(
'categories' => array(36),
'websites' => array(1),
'name' => 'my_pdt1008',
'description' => 'my_pdt1',
'short_description' => 'my_pdt1000',
'weight' => '11',
'status' => '1',
'url_key' => 'product-url-key1',
'url_path' => 'product-url-path1',
'visibility' => '4',
'price' => '100',
'tax_class_id' => 1,
'meta_title' => 'Product meta title1',
'meta_keyword' => 'Product meta keyword1',
'meta_description' => 'Product meta description1',
'stock_data' => array('qty'=>'100','is_in_stock'=>1,'manage_stock'=>1),
'additional_attributes' => array('multi_data' => array(array('key' => 'product_time', 'value' => $arrProductTime)))
));
I have faced a problem in my custom category attribute. It does not save the multiselect value in data base and does not show multiselect values on the category backend admin.
<?php
require_once("app/Mage.php");
Mage::app('default');
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$entityTypeId = $installer->getEntityTypeId('catalog_category');
$attributeSetId = $installer->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $installer->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);
$installer->addAttribute('catalog_category', 'cutomcity', array(
'label' => 'Test Select',
'type' => 'varchar',
'input' => 'multiselect',
'visible' => true,
'user_defined' => true,
'required' => false,
'position' => 80,
'visible_on_front' => false,
'group' => 'General Information',
'input' => 'multiselect',
'backend_model'=>'eav/entity_attribute_backend_array'
'source' => 'GA_Multiattribute_Helper_Testsource'
// eav/entity_attribute_source_table Even if i use this untill it does not show selected value on the Multiselect
));
?>
This is my helper file code
<?php
class GA_Multiattribute_Helper_Multiattributesource extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
{
protected $_optionsDefault = array();
public function getAllOptions($withEmpty = true, $defaultValues = false)
{
$collection = Mage::getModel('customer/customer')->getCollection()->addAttributeToSelect('*');;
$customers = array();
foreach($collection as $cust)
{
$fname = $cust->getFirstname();
$lname = $cust->getLastname();
$id = $cust->getId();
$customers[] = array('value'=>"$id$fname", 'label'=>"$fname $lname");
}
return $customers;
}
} ?>

Create Magento coupons progmatically with expiry dates of 30 days from now

I need to Create Coupons in Magento on Action of account Create.
I have created observer which is executing properly on account Create but i am Struggling with Coupons create. I have fixed coupon names on of fixed amount on applicable on cart subtotal.
I found many ans but none of them has anything about expiry of coupon
No replies yet :(
Though I found the solution.
$customer = $observer->getCustomer()->getData();
$email = $customer['email'];
$email100 = $email.'-100-1';
$datei=date('d/m/Y');
$datee = date('d/m/Y', strtotime('1 day'));
$firstname=$customer['firstname'];
$couponcode1=$email.'100-1';
$data = array(
'product_ids' => null,
'name' => sprintf('coupon-100-1', Mage::getSingleton('customer/session')->getCustomerId()),
'description' => null,
'is_active' => 1,
'website_ids' => array(1),
'customer_group_ids' => array(1),
'coupon_type' => 2,
'coupon_code' => $email100,
'uses_per_coupon' => 1,
'uses_per_customer' => 1,
'from_date' => $datei,
'to_date' => $datee,
'sort_order' => null,
'is_rss' => 1,
'test' => 'test',
'rule' => array(
'conditions' => array(
'1' =>array(
'type' => 'salesrule/rule_condition_combine',
'aggregator' => 'all',
'value' => 1,
),
'1--1' => array(
'type' => 'salesrule/rule_condition_combine',
'aggregator' => 'all',
'value' => 1,
),
'1--1--1' =>array(
'type' => 'salesrule/rule_condition_address',
'attribute' => 'base_subtotal',
'operator' => '>=',
'value' => '400'
),
'1--1--2' =>array(
'type' => 'salesrule/rule_condition_combine',
'aggregator' => 'all',
'value' => 0
),
'1--1--2--1' =>array(
'type' => 'salesrule/rule_condition_product_found',
'value' => 1,
'aggregator' => 'all'
),
'1--1--2--1--1' => array(
'type' => 'salesrule/rule_condition_product',
'attribute' => 'special_price',
'operator' => '>=',
'value' => 0
)
),
'actions' => array(
'1' =>array(
'type' => 'salesrule/rule_condition_combine',
'aggregator' => 'all',
'value' => 1,
'new_child' => null
),
)
),
'simple_action' => 'cart_fixed',
'discount_amount' => 100,
'discount_qty' => 0,
'discount_step' => null,
'apply_to_shipping' => 0,
'simple_free_shipping' => 0,
'stop_rules_processing' => 0,
'store_labels' => array()
);
$model = Mage::getModel('salesrule/rule');
$validateResult = $model->validateData(new Varien_Object($data));
if ($validateResult !== true) {
foreach($validateResult as $errorMessage) {
// print_r($errorMessage);
$session->addError($errorMessage);
}
}
if (isset($data['rule']['conditions'])) {
$data['conditions'] = $data['rule']['conditions'];
}
if (isset($data['rule']['actions'])) {
$data['actions'] = $data['rule']['actions'];
}
unset($data['rule']);
//print_r($data);
$model->loadPost($data);
$model->save();
Here the name of coupon code is email id followed by -100-1.
$datei and $datee are the initial date and the expiry date. I have customized the coupon code so that it is not being used if the cart contains special price item.
Fixed amount discount is applied on the complete cart subtotal.
In Case of query do respond.

Resources