cancel
Showing results for 
Search instead for 
Did you mean: 

Adding an order to Magento via an external site/service

SOLVED

Adding an order to Magento via an external site/service

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...

  1. User creates a custom sign on an external website (was not by my choice. I would have made the entire thing a Magento module)
  2. The sign is priced based on the design they create. The more complex, the more expensive
  3. User clicks "buy now" button
  4. Order is pushed into the Magento cart (user may or may not be logged in)
  5. User completes checkout on Magento site
    1. All customer, payment, shipping, etc is handled in Magento

So, I have two thoughts on how to make this happen...

  1. Use the Magento REST API to push the order from the external app into Magento
    1. Send a product ID and price into whatever cart is active for the user's session
  2. Create a custom Magento module with a WebApi side that takes the price from the external app and pushes the product into the cart via Magento core/local functions

The problems I'm having...

  1. It's been a while since I've done Magento development. It was v1 which was quite different
  2. I can make basic API calls, but I'm just not sure on the exact steps I need to take in order to get a product—with a custom price—pushed into the currently active (by session) cart

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

2 REPLIES

Re: Adding an order to Magento via an external site/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

 

 

 

-------
Crystal-Techs Solution: http://crystal-techs.com/

Re: Adding an order to Magento via an external site/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