Skip to content

Commit

Permalink
2.2.14
Browse files Browse the repository at this point in the history
* API get only enabled products option
* get vendor and manufacturer fields from product attributes
* get product sale price on store
  • Loading branch information
bnayalivne committed Feb 13, 2018
1 parent 85ead80 commit 0f2d925
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 21 deletions.
6 changes: 4 additions & 2 deletions Api/DataInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ interface DataInterface
* @param string|null $created_at_min
* @param string|null $created_at_max
* @param int|null $product_id
* @param bool $enabled_only
* @return \Remarkety\Mgconnector\Api\Data\ProductCollectionInterface
*/
*/
public function getProducts(
$mage_store_id,
$updated_at_min = null,
Expand All @@ -29,7 +30,8 @@ public function getProducts(
$since_id = null,
$created_at_min = null,
$created_at_max = null,
$product_id = null
$product_id = null,
$enabled_only = false
);
/**
* Get All customers from catalog
Expand Down
47 changes: 41 additions & 6 deletions Model/Api/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ class Data implements DataInterface
],
"created_at" => 'created_at',
"id" => 'entity_id',
'price' => 'price',
"price" => "price",
"sale_price_with_tax" => "sale_price_with_tax",
"image" => [
"id",
"product_id",
Expand Down Expand Up @@ -113,6 +114,7 @@ class Data implements DataInterface
"image",
"inventory_quantity",
"price",
"sale_price_with_tax",
"product_id",
"sku",
"taxable",
Expand Down Expand Up @@ -367,7 +369,8 @@ public function __construct(ProductFactory $productFactory,
* @param string|null $created_at_min
* @param string|null $created_at_max
* @param int|null $product_id
* @return array $collection
* @param bool $enabled_only
* @return DataObject $collection
*/
public function getProducts(
$mage_store_id,
Expand All @@ -378,9 +381,11 @@ public function getProducts(
$since_id = null,
$created_at_min = null,
$created_at_max = null,
$product_id = null
$product_id = null,
$enabled_only = false
)
{
$this->_storeManagerInterface->setCurrentStore($mage_store_id);

$pageNumber = null;
$pageSize = null;
Expand Down Expand Up @@ -416,6 +421,11 @@ public function getProducts(
$collection->addAttributeToFilter('entity_id', $product_id);
}

if($enabled_only){
$collection->addAttributeToFilter('status', Status::STATUS_ENABLED);
$collection->addAttributeToFilter('visibility', ['neq' => Visibility::VISIBILITY_NOT_VISIBLE]);
}

if ($limit != null) {
$pageNumber = 1; // Note that page numbers begin at 1
$pageSize = $limit;
Expand All @@ -430,6 +440,12 @@ public function getProducts(
if (!is_null($pageSize)) $collection->setPage($pageNumber, $pageSize);


$vendorAttr = $collection->getResource()->getAttribute('vendor');
if(!$vendorAttr){
$vendorAttr = $collection->getResource()->getAttribute('brand');
}
$manufacturerAttr = $collection->getResource()->getAttribute('manufacturer');

$map = $this->response_mask;
$productsArray = [];
foreach ($collection AS $row) {
Expand Down Expand Up @@ -465,6 +481,7 @@ public function getProducts(

$prod['body_html'] = $row->getDescription();
$prod['id'] = $row->getId();
$prod['sale_price_with_tax'] = $row->getFinalPrice();

$parent_id = $this->dataHelper->getParentId($row->getId());
if ($row->getTypeId() == 'simple' && $parent_id) {
Expand Down Expand Up @@ -499,18 +516,36 @@ public function getProducts(
'created_at' => $created_at_child->format(\DateTime::ATOM),
'updated_at' => $updated_at_child->format(\DateTime::ATOM),
'inventory_quantity' => $stock->getQty(),
'price' => (float)$childProd->getPrice()
'price' => (float)$childProd->getPrice(),
'sale_price_with_tax' => (float)$childProd->getFinalPrice()
];
}
}
} else {
$stock = $this->stockRegistry->getStockItem($row->getId());
$variants[] = [
'inventory_quantity' => $stock->getQty(),
'price' => (float)$row->getPrice()
'price' => (float)$row->getPrice(),
'sale_price_with_tax' => (float)$row->getFinalPrice()
];
}
$prod['variants'] = $variants;
if($vendorAttr){
if(!empty($row->getData($vendorAttr->getAttributeCode()))){
$vendor = $vendorAttr->getFrontend()->getValue($row);
$prod['vendor'] = $vendor;
} else {
$prod['vendor'] = null;
}
}
if($manufacturerAttr){
if(!empty($row->getData($manufacturerAttr->getAttributeCode()))){
$manufacturer = $manufacturerAttr->getFrontend()->getValue($row);
$prod['manufacturer'] = $manufacturer;
} else {
$prod['manufacturer'] = null;
}
}

$productsArray[] = $prod;
}
Expand Down Expand Up @@ -1206,7 +1241,7 @@ public function setConfig($mage_store_id, $configName, $scope, $newValue)
*/
public function getVersion()
{
return '2.2.13';
return '2.2.14';
}

/**
Expand Down
10 changes: 9 additions & 1 deletion Serializer/OrderSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,24 @@ public function serialize(\Magento\Sales\Model\Order $order){
$line_items = [];
foreach($items as $item){
$product = $item->getProduct();
$lineTax = (float)$item->getTaxAmount();
$lineQty = (float)$item->getQtyOrdered();
if($lineQty > 0 && $lineTax > 0){
$itemTax = $lineTax / $lineQty;
} else {
$itemTax = 0;
}
$itemArr = [
//'product_parent_id' => $rmCore->getProductParentId($item->getProduct()),
'product_id' => $item->getProductId(),
'sku' => $item->getSku(),
'quantity' => (float)$item->getQtyOrdered(),
'quantity' => $lineQty,
'quantity_refunded' => $item->getQtyRefunded(),
'quantity_shipped' => $item->getQtyShipped(),
'name' => $item->getName(),
'title' => empty($product) ? $item->getName() : $product->getName(),
'price' => (float)$item->getPrice(),
'tax_amount' => $itemTax,
'url' => empty($product) ? null : $product->getProductUrl(),
'images' => empty($product) ? [] : $this->remarketyHelper->getMediaGalleryImages($product)
];
Expand Down
59 changes: 49 additions & 10 deletions Serializer/ProductSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Magento\Catalog\Model\ProductRepository;
use Magento\CatalogInventory\Api\StockRegistryInterface;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
use Magento\Store\Model\StoreManagerInterface;
use Remarkety\Mgconnector\Helper\Data;

class ProductSerializer
Expand All @@ -26,13 +27,15 @@ class ProductSerializer
protected $dataHelper;
protected $urlModel;
protected $stockRegistry;
protected $storeManager;
public function __construct(
\Magento\Catalog\Model\CategoryFactory $categoryFactory,
\Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable $catalogProductTypeConfigurable,
ProductRepository $productRepository,
Data $dataHelper,
Url $urlModel,
StockRegistryInterface $stockRegistry
StockRegistryInterface $stockRegistry,
StoreManagerInterface $storeManager
)
{
$this->categoryFactory = $categoryFactory;
Expand All @@ -41,10 +44,11 @@ public function __construct(
$this->dataHelper = $dataHelper;
$this->urlModel = $urlModel;
$this->stockRegistry = $stockRegistry;
$this->storeManager = $storeManager;
}

public function loadProduct($product_id){
return $this->productRepository->getById($product_id);
public function loadProduct($product_id, $store_id = null){
return $this->productRepository->getById($product_id, false, $store_id);
}

public function serialize(ProductInterface $product, $storeId){
Expand All @@ -53,10 +57,18 @@ public function serialize(ProductInterface $product, $storeId){
if($product->getTypeId() == 'simple'){
$parent_id = $this->dataHelper->getParentId($product->getId());
if(!empty($parent_id)) {
$parentProduct = $this->loadProduct($parent_id);
$parentProduct = $this->loadProduct($parent_id, $storeId);
}
}

$store = $this->storeManager->getStore($storeId);
$product->setStoreId($storeId);
$product->setWebsiteId($store->getWebsiteId());
$product->setCustomerGroupId(0);

//makes sure the final price is re-calculated based on the current store
$product->setFinalPrice(null);

$created_at = new \DateTime($product->getCreatedAt());
$updated_at = new \DateTime($product->getUpdatedAt());

Expand All @@ -74,11 +86,11 @@ public function serialize(ProductInterface $product, $storeId){
$stock = $this->stockRegistry->getStockItem($product->getId());
$variants[] = [
'inventory_quantity' => $stock->getQty(),
'price' => (float)$product->getPrice()
'price' => (float)$product->getPrice(),
'salePrice' => (float)$product->getFinalPrice()
];

} else {
$product->setStoreId($storeId);
$categoryIds = $product->getCategoryIds();
$url = $product->getProductUrl(false);
$images = $this->dataHelper->getMediaGalleryImages($product);
Expand All @@ -89,7 +101,11 @@ public function serialize(ProductInterface $product, $storeId){
if(isset($childrenIdsGroups[0])) {
$childrenIds = $childrenIdsGroups[0];
foreach ($childrenIds as $childId) {
$childProd = $this->loadProduct($childId);
$childProd = $this->loadProduct($childId, $storeId);
$childProd->setStoreId($storeId);
$childProd->setWebsiteId($store->getWebsiteId());
$childProd->setCustomerGroupId(0);

$stock = $this->stockRegistry->getStockItem($childId);

$created_at_child = new \DateTime($childProd->getCreatedAt());
Expand All @@ -102,15 +118,17 @@ public function serialize(ProductInterface $product, $storeId){
'created_at' => $created_at_child->format(\DateTime::ATOM),
'updated_at' => $updated_at_child->format(\DateTime::ATOM),
'inventory_quantity' => $stock->getQty(),
'price' => (float)$childProd->getPrice()
'price' => (float)$childProd->getPrice(),
'salePrice' => (float)$childProd->getFinalPrice()
];
}
}
} else {
$stock = $this->stockRegistry->getStockItem($product->getId());
$variants[] = [
'inventory_quantity' => $stock->getQty(),
'price' => (float)$product->getPrice()
'price' => (float)$product->getPrice(),
'salePrice' => (float)$product->getFinalPrice()
];
}

Expand All @@ -126,6 +144,24 @@ public function serialize(ProductInterface $product, $storeId){

$options = [];

$vendor = null;
$manufacturer = null;

$vendorAttr = $product->getResource()->getAttribute('vendor');
if(!$vendorAttr){
$vendorAttr = $product->getResource()->getAttribute('brand');
}
$manufacturerAttr = $product->getResource()->getAttribute('manufacturer');
if($manufacturerAttr){
if(!empty($product->getData($manufacturerAttr->getAttributeCode()))){
$manufacturer = $manufacturerAttr->getFrontend()->getValue($product);
}
}
if($vendorAttr){
if(!empty($product->getData($vendorAttr->getAttributeCode()))){
$vendor = $vendorAttr->getFrontend()->getValue($product);
}
}
$data = [
'id' => $product->getId(),
'sku' => $product->getSku(),
Expand All @@ -136,9 +172,12 @@ public function serialize(ProductInterface $product, $storeId){
'images' => $images,
'enabled' => $enabled,
'price' => (float)$product->getPrice(),
'salePrice' => (float)$product->getFinalPrice(),
'url' => $url,
'variants' => $variants,
'options' => $options
'options' => $options,
'vendor' => $vendor,
'manufacturer' => $manufacturer
];
if(!empty($parent_id)){
$data['parent_id'] = $parent_id;
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"lib-libxml": "*"
},
"type": "magento2-module",
"version": "2.2.13",
"version": "2.2.14",
"license": [
"OSL-3.0",
"AFL-3.0"
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Remarkety_Mgconnector" setup_version="2.2.13">
<module name="Remarkety_Mgconnector" setup_version="2.2.14">
</module>
</config>

0 comments on commit 0f2d925

Please sign in to comment.