Hello you totally awesome developer you.
I need some direction from you amazing developers on the best way to add a product / order to Magento via an external website.
Here is the scenario...
So, I have two thoughts on how to make this happen...
The problems I'm having...
Any and all help is greatly appreciated and I'm kinda on a time crunch (surprise, surprise). While I would love any free direction and help, I'm also willing to pay some consulting time if that is an option as well.
Thanks. Hope to hear from you cool kids soon!
Jon
Solved! Go to Solution.
Hi zuno,
In this ways you will have 2 steps.
1 - Create a new product(it is product that customer created)
2 - Add this product to Cart
Step 1 you can use Magento 2 REST API add product. This is code to create new product in Magento 2
$productData = array( 'sku' => 'SKU-' . uniqid(), 'name' => 'Create New Product ' . uniqid(), 'visibility' => 4, /*'catalog',*/ 'type_id' => 'simple', 'price' => 99.95, 'status' => 1, 'attribute_set_id' => 4, 'weight' => 1, 'custom_attributes' => array( array( 'attribute_code' => 'category_ids', 'value' => ["category ID that you want assign in"] ), array( 'attribute_code' => 'description', 'value' => 'Simple Description' ), array( 'attribute_code' => 'short_description', 'value' => 'Simple Short Description' ), ) ); $productData = json_encode(array('product' => $productData)); $requestURL = "http://yourhost/rest/V1/products"; $ch = curl_init(); curl_setopt($ch,CURLOPT_URL, $requestURL); curl_setopt($ch,CURLOPT_POSTFIELDS, $productData); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_HTTPHEADER, $setHaders); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); if(curl_exec($ch)===false){ echo "Curl error: " . curl_error($ch)."\n"; }else{ $response = curl_exec($ch) ?: ""; } curl_close($ch); echo $response;
Now we will move to step 2 to add project that you just create to cart. I often use this code to add a product to cart.
<?php namespace Your\Module\Controller\Index; use Magento\Framework\App\Action\Context; use Magento\Framework\View\Result\PageFactory; class AddProducttocart extends \Magento\Customer\Controller\AbstractAccount { /** * @var PageFactory */ protected $resultPageFactory; /** * @var \Magento\Framework\Data\Form\FormKey */ protected $formKey; /** * @param Context $context * @param PageFactory $resultPageFactory */ public function __construct( Context $context, \Magento\Framework\Data\Form\FormKey $formKey, PageFactory $resultPageFactory ) { parent::__construct($context); $this->formKey = $formKey; $this->resultPageFactory = $resultPageFactory; } /** * * @return \Magento\Framework\View\Result\Page */ public function execute() { $resultPage = $this->resultPageFactory->create(); $params = array( 'form_key' => $this->formKey->getFormKey(), 'product' =>12,//product Id you can get product that you just create 'qty' =>1,//quantity of product 'price' =>100 //product price ); $this->_redirect("checkout/cart/add/form_key/", $params); /** @var \Magento\Framework\View\Result\Page $resultPage */ return $resultPage; } }
Also you must get product ID that you just create it is easy.
Please let me know if you have any question. I will support you if any.
Thanks,
Tuan
Migrate Magento 1 to Magento 2 Service
Hi zuno,
In this ways you will have 2 steps.
1 - Create a new product(it is product that customer created)
2 - Add this product to Cart
Step 1 you can use Magento 2 REST API add product. This is code to create new product in Magento 2
$productData = array( 'sku' => 'SKU-' . uniqid(), 'name' => 'Create New Product ' . uniqid(), 'visibility' => 4, /*'catalog',*/ 'type_id' => 'simple', 'price' => 99.95, 'status' => 1, 'attribute_set_id' => 4, 'weight' => 1, 'custom_attributes' => array( array( 'attribute_code' => 'category_ids', 'value' => ["category ID that you want assign in"] ), array( 'attribute_code' => 'description', 'value' => 'Simple Description' ), array( 'attribute_code' => 'short_description', 'value' => 'Simple Short Description' ), ) ); $productData = json_encode(array('product' => $productData)); $requestURL = "http://yourhost/rest/V1/products"; $ch = curl_init(); curl_setopt($ch,CURLOPT_URL, $requestURL); curl_setopt($ch,CURLOPT_POSTFIELDS, $productData); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_HTTPHEADER, $setHaders); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); if(curl_exec($ch)===false){ echo "Curl error: " . curl_error($ch)."\n"; }else{ $response = curl_exec($ch) ?: ""; } curl_close($ch); echo $response;
Now we will move to step 2 to add project that you just create to cart. I often use this code to add a product to cart.
<?php namespace Your\Module\Controller\Index; use Magento\Framework\App\Action\Context; use Magento\Framework\View\Result\PageFactory; class AddProducttocart extends \Magento\Customer\Controller\AbstractAccount { /** * @var PageFactory */ protected $resultPageFactory; /** * @var \Magento\Framework\Data\Form\FormKey */ protected $formKey; /** * @param Context $context * @param PageFactory $resultPageFactory */ public function __construct( Context $context, \Magento\Framework\Data\Form\FormKey $formKey, PageFactory $resultPageFactory ) { parent::__construct($context); $this->formKey = $formKey; $this->resultPageFactory = $resultPageFactory; } /** * * @return \Magento\Framework\View\Result\Page */ public function execute() { $resultPage = $this->resultPageFactory->create(); $params = array( 'form_key' => $this->formKey->getFormKey(), 'product' =>12,//product Id you can get product that you just create 'qty' =>1,//quantity of product 'price' =>100 //product price ); $this->_redirect("checkout/cart/add/form_key/", $params); /** @var \Magento\Framework\View\Result\Page $resultPage */ return $resultPage; } }
Also you must get product ID that you just create it is easy.
Please let me know if you have any question. I will support you if any.
Thanks,
Tuan
Migrate Magento 1 to Magento 2 Service
Hello Tuan.
Thank you so much for this! It worked perfectly. You are awesome.
Do you happen to do freelance development? I may need some help in the future.
-Jon