0

I am trying to get product name, product SKU, Quantity in Url such that when I write 1 in Url 1-50 products should display, when writing 2 as page number 51-100 products should display. And data goes using JSON template. Also, I need to pass a key in Url.

I have retrieved required data as in Block using classes:

\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
\Magento\CatalogInventory\Model\Stock\StockItemRepository $stockItemRepository

As

public function getProductCollection()
{
    $collection = $this->_productCollectionFactory->create();
    $collection->addAttributeToSelect('*');
    $collection->setPageSize(10); // fetching only 3 products
    return $collection;
}
public function getStockItem($productId)
{
    return $this->_stockItemRepository->get($productId);
}

And in template for seeing data validness:

$productCollection = $block->getProductCollection();
foreach ($productCollection as $product) {
  //print_r($product->getData());
  echo 'Product ID:'.$product->getId();
  echo "<br>";
  echo 'Product Name:'.$product->getName();
  echo "<br>";
  echo 'Product SKU:'.$product->getSku();
  echo "<br>";
  //echo 'Product Qty:'.$product->getQty();
  $id = $product->getId();
  $_productStock = $block->getStockItem($id);
  echo 'Product Qty:'.$_productStock->getQty(); echo '<br />';
}

This is my JSON template:

{

“items”:

            [

                            {“itemID”: “1000”,”name”:”product name 1” },

                            {“itemID”: “1001”,”name”:”product name 2” },

                            etc

            ]



}

Like here is an example Url:

http://dummy.uk/pointsale?lastoid=15404

when I give page number 1, above mention data of products from 1-50 should be displayed.

Here is a tutorial that explains about Order data, but is not working for me. https://www.human-element.com/order-data-json-custom-route-magento-2/ what should be done to achieve the result?

  • Can you please explain more in clear way? – Rohan Hapani Oct 22 '18 at 6:42
  • I have added some more detail – Ajwad Taqvi Oct 22 '18 at 6:57
1

It's multi-dimentional array. If you want to access itemID, name etc., then you need to add foreach of $productColllection['items']. Like this below code :

foreach ($productCollection['items'] as $product) {
  //print_r($product->getData());
  echo 'Product ID:'.$product->getId();
  echo "<br>";
  echo 'Product Name:'.$product->getName();
  echo "<br>";
  echo 'Product SKU:'.$product->getSku();
  echo "<br>";
  //echo 'Product Qty:'.$product->getQty();
  $id = $product->getId();
  $_productStock = $block->getStockItem($id);
  echo 'Product Qty:'.$_productStock->getQty(); echo '<br />';
}

EDIT :

I don't know about from you access url. But, if you want to add query string into product url then you can add query string like this below way.

<?php echo $_product->getProductUrl().'?id=id&name=name' ?>
  • right, but my actual concern is about passing this gata into URL as query string, can you tell about it. – Ajwad Taqvi Oct 22 '18 at 7:07
  • I am accessing it via Controller like in example tutorial in question. – Ajwad Taqvi Oct 22 '18 at 7:45
0

Well, Thanks for help. I got my solution by this:

$cusCollection = array(); 
    $i=0;
    $productCollection = $this->order->getProductCollection();
    foreach ($productCollection as $product) {
        $cusCollection['items'][$i]['itemID'] = $product->getId();
        $id = $product->getId();
        $_productStock = $this->order->getStockItem($id); 
        $productQty = $_productStock->getQty();
        $cusCollection['items'][$i]['name '] = $product->getName().' '.$productQty; 
        $cusCollection['items'][$i]['sku '] = $product->getSku();
        $i++;
}
        echo json_encode($cusCollection);

and adding this in collection:

$page = $this->getRequest()->getParam('page');
$collection->setPageSize(50)->setCurPage($page);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.