209 lines
11 KiB
PHP
Executable File
209 lines
11 KiB
PHP
Executable File
<?php
|
|
|
|
namespace IpSupply\ProductApiSyncErp\Api\Controller;
|
|
|
|
use IpSupply\ProductApiSyncErp\Api\Interface\ProductApiSyncErpInterface;
|
|
use Magento\Framework\App\Helper\Context;
|
|
use Magento\Framework\App\Helper\AbstractHelper;
|
|
use Magento\Framework\Webapi\Rest\Request;
|
|
use Magento\Framework\App\ObjectManager;
|
|
use Magento\Store\Model\StoreManagerInterface;
|
|
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
|
|
|
|
class ProductApiSyncErpController extends AbstractHelper implements ProductApiSyncErpInterface {
|
|
protected $_request;
|
|
protected $_objectManager;
|
|
protected $_collection;
|
|
protected $_storeManager;
|
|
protected $_baseURL;
|
|
|
|
public function __construct(Context $context, Request $request, CollectionFactory $collection, StoreManagerInterface $storeManager) {
|
|
$this->_request = $request;
|
|
$this->_objectManager = ObjectManager::getInstance();
|
|
$this->_collection = $collection;
|
|
$this->_storeManager = $storeManager;
|
|
$this->_baseURL = $this->_storeManager->getStore()->getBaseUrl();
|
|
parent::__construct($context);
|
|
}
|
|
|
|
public function getProduct() {
|
|
try {
|
|
$requestParams = $this->_request->getParams();
|
|
$currentPage = isset($requestParams["current_page"]) ? $requestParams["current_page"] : 0;
|
|
$limit = isset($requestParams["limit"]) ? $requestParams["limit"] : 100;
|
|
$response = [];
|
|
|
|
$productCollection = $this->_collection->create();
|
|
$productCollection->addAttributeToSelect('*');
|
|
$productCollection->addFieldToFilter('type_id', 'configurable');
|
|
$productCollection->setPageSize($limit);
|
|
$productCollection->setCurPage($currentPage);
|
|
$productCollection->load();
|
|
foreach($productCollection as $product) {
|
|
$dataResponse = [];
|
|
$dataResponse = [];
|
|
$dataResponse["sku"] = $product->getSku();
|
|
$dataResponse["name"] = $product->getSku();
|
|
$categoryIds = $product->getCategoryIds();
|
|
$dataResponse["categories"] = !empty($categoryIds) ? $categoryIds : [];
|
|
$dataResponse["description"] = $product->getDescription() != null ? $product->getDescription() : "";
|
|
$dataResponse["shortDescription"] = $product->getShortDescription() != null ? $product->getShortDescription() : "";
|
|
$dataResponse["imageUrls"] = $this->getProductImageURL($product);
|
|
$dataResponse["brands"] = "";
|
|
if($product->getResource()->getAttribute("brands") != false) {
|
|
if($product->getResource()->getAttribute("brands")->getFrontend()->getValue($product) != false) {
|
|
$dataResponse["brands"] = $product->getResource()->getAttribute("brands")->getFrontend()->getValue($product);
|
|
}
|
|
}
|
|
$dataResponse["types"] = "";
|
|
if($product->getResource()->getAttribute("types") != false) {
|
|
if($product->getResource()->getAttribute("types")->getFrontend()->getValue($product) != false) {
|
|
$dataResponse["types"] = $product->getResource()->getAttribute("types")->getFrontend()->getValue($product);
|
|
}
|
|
}
|
|
$dataResponse["condition"] = $this->getProductVariants($product);
|
|
$dataResponse["relatedProduct"] = $this->getRelatedProduct($product);
|
|
$dataResponse["upsellProduct"] = $this->getUpSellProduct($product);
|
|
$dataResponse["crosssellProduct"] = $this->getCrossSellProduct($product);
|
|
$response[] = $dataResponse;
|
|
}
|
|
|
|
echo json_encode(array(
|
|
"current_page" => (int)$currentPage,
|
|
"total" => count($productCollection),
|
|
"data" => $response
|
|
));
|
|
die;
|
|
}
|
|
catch (Exception $e) {
|
|
echo json_encode(["code" => 500, "message" => "Error".$e]);
|
|
die;
|
|
}
|
|
}
|
|
|
|
public function getProductImageURL($product) {
|
|
return $product["thumbnail"] != "" ? [$this->_baseURL."media/catalog/product".$product["thumbnail"]] : [];
|
|
}
|
|
|
|
public function getProductVariants($product) {
|
|
$productVariants = $product->getTypeInstance()->getUsedProducts($product);
|
|
if(!empty($productVariants)) {
|
|
$response = [];
|
|
foreach($productVariants as $variant) {
|
|
$optionText = $variant->getResource()->getAttribute('choose_condition')->getFrontend()->getValue($variant);
|
|
$response[strtolower($optionText)] = array(
|
|
"qty" => $variant->getQty(),
|
|
"price" => $variant->getPrice(),
|
|
"imageUrls" => $this->getProductImageURL($variant)
|
|
);
|
|
}
|
|
return $response;
|
|
}
|
|
else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public function getRelatedProduct($rootProduct) {
|
|
$response = [];
|
|
$relatedProduct = $rootProduct->getRelatedProducts();
|
|
$productRepo = $this->_objectManager->get('\Magento\Catalog\Model\ProductRepository');
|
|
if(!empty($relatedProduct)) {
|
|
foreach($relatedProduct as $product) {
|
|
$dataResponse = [];
|
|
$productData = $productRepo->getById($product->getID());
|
|
$dataResponse["sku"] = $productData->getSku();
|
|
$dataResponse["name"] = $product->getSku();
|
|
$categoryIds = $product->getCategoryIds();
|
|
$dataResponse["categories"] = !empty($categoryIds) ? $categoryIds : [];
|
|
$dataResponse["description"] = $product->getDescription() != null ? $product->getDescription() : "";
|
|
$dataResponse["shortDescription"] = $product->getShortDescription() != null ? $product->getShortDescription() : "";
|
|
$dataResponse["imageUrls"] = $this->getProductImageURL($productData);
|
|
$dataResponse["brands"] = "";
|
|
if($productData->getResource()->getAttribute("brands") != false) {
|
|
if($productData->getResource()->getAttribute("brands")->getFrontend()->getValue($productData) != false) {
|
|
$dataResponse["brands"] = $productData->getResource()->getAttribute("brands")->getFrontend()->getValue($productData);
|
|
}
|
|
}
|
|
$dataResponse["types"] = "";
|
|
if($productData->getResource()->getAttribute("types") != false) {
|
|
if($productData->getResource()->getAttribute("types")->getFrontend()->getValue($productData) != false) {
|
|
$dataResponse["types"] = $productData->getResource()->getAttribute("types")->getFrontend()->getValue($productData);
|
|
}
|
|
}
|
|
$dataResponse["condition"] = $this->getProductVariants($productData);
|
|
$response[] = $dataResponse;
|
|
}
|
|
}
|
|
return $response;
|
|
}
|
|
|
|
public function getUpSellProduct($rootProduct) {
|
|
$response = [];
|
|
$upSellProduct = $rootProduct->getUpSellProducts();
|
|
$productRepo = $this->_objectManager->get('\Magento\Catalog\Model\ProductRepository');
|
|
if(!empty($upSellProduct)) {
|
|
foreach($upSellProduct as $product) {
|
|
$dataResponse = [];
|
|
$productData = $productRepo->getById($product->getID());
|
|
$dataResponse["sku"] = $productData->getSku();
|
|
$dataResponse["name"] = $product->getSku();
|
|
$categoryIds = $product->getCategoryIds();
|
|
$dataResponse["categories"] = !empty($categoryIds) ? $categoryIds : [];
|
|
$dataResponse["description"] = $product->getDescription() != null ? $product->getDescription() : "";
|
|
$dataResponse["shortDescription"] = $product->getShortDescription() != null ? $product->getShortDescription() : "";
|
|
$dataResponse["imageUrls"] = $this->getProductImageURL($productData);
|
|
$dataResponse["brands"] = "";
|
|
if($productData->getResource()->getAttribute("brands") != false) {
|
|
if($productData->getResource()->getAttribute("brands")->getFrontend()->getValue($productData) != false) {
|
|
$dataResponse["brands"] = $productData->getResource()->getAttribute("brands")->getFrontend()->getValue($productData);
|
|
}
|
|
}
|
|
$dataResponse["types"] = "";
|
|
if($productData->getResource()->getAttribute("types") != false) {
|
|
if($productData->getResource()->getAttribute("types")->getFrontend()->getValue($productData) != false) {
|
|
$dataResponse["types"] = $productData->getResource()->getAttribute("types")->getFrontend()->getValue($productData);
|
|
}
|
|
}
|
|
$dataResponse["condition"] = $this->getProductVariants($productData);
|
|
$response[] = $dataResponse;
|
|
}
|
|
}
|
|
return $response;
|
|
}
|
|
|
|
public function getCrossSellProduct($rootProduct) {
|
|
$response = [];
|
|
$crossSellProduct = $rootProduct->getCrossSellProducts();
|
|
$productRepo = $this->_objectManager->get('\Magento\Catalog\Model\ProductRepository');
|
|
if(!empty($crossSellProduct)) {
|
|
foreach($crossSellProduct as $product) {
|
|
$dataResponse = [];
|
|
$productData = $productRepo->getById($product->getID());
|
|
$dataResponse["sku"] = $productData->getSku();
|
|
$dataResponse["name"] = $product->getSku();
|
|
$categoryIds = $product->getCategoryIds();
|
|
$dataResponse["categories"] = !empty($categoryIds) ? $categoryIds : [];
|
|
$dataResponse["description"] = $product->getDescription() != null ? $product->getDescription() : "";
|
|
$dataResponse["shortDescription"] = $product->getShortDescription() != null ? $product->getShortDescription() : "";
|
|
$dataResponse["imageUrls"] = $this->getProductImageURL($productData);
|
|
$dataResponse["brands"] = "";
|
|
if($productData->getResource()->getAttribute("brands") != false) {
|
|
if($productData->getResource()->getAttribute("brands")->getFrontend()->getValue($productData) != false) {
|
|
$dataResponse["brands"] = $productData->getResource()->getAttribute("brands")->getFrontend()->getValue($productData);
|
|
}
|
|
}
|
|
$dataResponse["types"] = "";
|
|
if($productData->getResource()->getAttribute("types") != false) {
|
|
if($productData->getResource()->getAttribute("types")->getFrontend()->getValue($productData) != false) {
|
|
$dataResponse["types"] = $productData->getResource()->getAttribute("types")->getFrontend()->getValue($productData);
|
|
}
|
|
}
|
|
$dataResponse["condition"] = $this->getProductVariants($productData);
|
|
$response[] = $dataResponse;
|
|
}
|
|
}
|
|
return $response;
|
|
}
|
|
}
|