I want to send info to third party services about the views on pages like cart, products, checkout, etc. This info is sent whenever someone visits the respective page or view. For example, when a client sees a product on my store, I want to send the info about the product, time, etc. Does anynone have a clue of how to do this?

有帮助吗?

解决方案

You can do it by event observer, here and here for how to create custom observer. And you want to send data to your services (for example if you want to send about checked out item, you can use sales_model_service_quote_submit_success event), and then just create the json and send it with file_get_contents function like below :

$order = $observer->getOrder(); //get the order details
$quoteRepository = $this->_objectManager->create('Magento\Quote\Model\QuoteRepository'); //create repository
$quote = $quoteRepository->get($order->getQuoteId()); //get cart information by quote ID

$cartId = $order->getQuoteId(); //get cart ID
$allItems = $quote->getItemsCollection(); //get all checked out items

foreach ($allItems as $item) {
   $data[] = array (
      "itemSKU" => $item->getSku(), //save item SKU
      "itemQuantity" => $item->getQty() //save item Quantity 
   )
}

$json = json_encode($data); //save the arrays to json
$serviceUrl = //your service URL;
$service = json_decode(file_get_contents($serviceUrl.$json)); //send the json to your service and get the feed back
许可以下: CC-BY-SA归因
scroll top