Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsolonenko committed Feb 7, 2023
1 parent 3c33196 commit a64bf57
Show file tree
Hide file tree
Showing 18 changed files with 440 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Api/CategoryManagementInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
namespace Commerce365\MagentoApiExtensions\Api;

/**
* Category Management Interface.
*
* Additional admin methods to manage your Magento categories.
*
* @api
* @since 0.0.1
*/
interface CategoryManagementInterface
{
/**
* Put category position (within tree)
*
* @api
* @param int $categoryId
* @param int $new
* @return boolean
*/
public function updatePosition($categoryId, $new);

}
24 changes: 24 additions & 0 deletions Api/MagentoManagementInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
namespace Commerce365\MagentoApiExtensions\Api;

/**
* Magento Management Interface.
*
* Additional admin method to obtain the current Magento version.
*
* @api
* @since 0.0.4
*/
interface MagentoManagementInterface
{
/**
* Get current Magento version
*
* @api
* @return string
*/
public function getMagentoVersion();

}


22 changes: 22 additions & 0 deletions Api/PaymentMethodManagementInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
namespace Commerce365\MagentoApiExtensions\Api;

/**
* Payment Method Management Interface.
*
* Additional admin method to retrieve a list of installed payment methods.
*
* @api
* @since 0.0.3
*/
interface PaymentMethodManagementInterface
{
/**
* Get active payment methods
*
* @api
* @return string[]
*/
public function getActiveMethods();

}
24 changes: 24 additions & 0 deletions Api/SalesManagementInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
namespace Commerce365\MagentoApiExtensions\Api;

/**
* Sales Management Interface.
*
* Additional admin method to create a custom order status.
*
* @api
* @since 0.0.5
*/
interface SalesManagementInterface
{
/**
* Post a new order status to make sure Magento knows the status NAV/BC is using
*
* @api
* @param string $statusCode
* @param string $statusLabel
* @return boolean
*/
public function createOrderStatus($statusCode, $statusLabel);

}
22 changes: 22 additions & 0 deletions Api/ShippingMethodManagementInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
namespace Commerce365\MagentoApiExtensions\Api;

/**
* Shipping Method Management Interface.
*
* Additional admin method to retrieve a list of installed shipping methods.
*
* @api
* @since 0.0.3
*/
interface ShippingMethodManagementInterface
{
/**
* Get active carriers / shipping methods
*
* @api
* @return string[]
*/
public function getActiveMethods();

}
30 changes: 30 additions & 0 deletions Model/Category.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
namespace Commerce365\MagentoApiExtensions\Model;
use Commerce365\MagentoApiExtensions\Api\CategoryManagementInterface;

class Category implements CategoryManagementInterface
{

/**
* Put category position (within tree)
*
* @api
* @param int $categoryId
* @param int $new
* @return boolean
*/
public function updatePosition($categoryId, $new){
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category = $objectManager->create('Magento\Catalog\Model\Category')->load($categoryId);

if (!$category->getId())
throw new Exception("Category does not exist");

$category->setPosition($new);

$objectManager->get('\Magento\Catalog\Api\CategoryRepositoryInterface')->save($category);

return true;
}

}
24 changes: 24 additions & 0 deletions Model/Magento.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
namespace Commerce365\MagentoApiExtensions\Model;
use Commerce365\MagentoApiExtensions\Api\MagentoManagementInterface;
use Magento\Framework\App\ProductMetadataInterface;

class Magento implements MagentoManagementInterface
{

protected $productMetadata;

public function __construct(ProductMetadataInterface $productMetadata) {
$this->productMetadata = $productMetadata;
}

/**
* Get current Magento version
*
* @api
* @return string
*/
public function getMagentoVersion(){
return $this->productMetadata->getVersion();
}
}
43 changes: 43 additions & 0 deletions Model/PaymentMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
namespace Commerce365\MagentoApiExtensions\Model;
use Commerce365\MagentoApiExtensions\Api\PaymentMethodManagementInterface;

class PaymentMethod implements PaymentMethodManagementInterface
{

protected $scopeConfig;

protected $paymentConfig;

public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Payment\Model\Config $paymentConfig
) {
$this->paymentConfig= $paymentConfig;
$this->scopeConfig = $scopeConfig;
}

/**
* Get active payment methods
*
* @api
* @return object
*/
public function getActiveMethods(){

$activeMethods = $this->paymentConfig->getActiveMethods();
$storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
$methods = array();

foreach($activeMethods as $paymentCode => $paymentModel)
{
$paymentTitle = $this->scopeConfig->getValue('payment/'.$paymentCode.'/title');
$methods[$paymentCode] = array(
'label' => $paymentTitle,
'value' => $paymentCode
);
}

return $methods;
}
}
49 changes: 49 additions & 0 deletions Model/Sales.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
namespace Commerce365\MagentoApiExtensions\Model;

use Commerce365\MagentoApiExtensions\Api\SalesManagementInterface;

use Magento\Framework\Exception\AlreadyExistsException;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\StatusFactory;
use Magento\Sales\Model\ResourceModel\Order\StatusFactory as StatusResourceFactory;

class Sales implements SalesManagementInterface
{
protected StatusFactory $statusFactory;
protected StatusResourceFactory $statusResourceFactory;

public function __construct(StatusFactory $statusFactory, StatusResourceFactory $statusResourceFactory)
{
$this->statusFactory = $statusFactory;
$this->statusResourceFactory = $statusResourceFactory;
}

/**
* Post a new order status to make sure Magento knows the status NAV/BC is using
*
* @api
* @param string $statusCode
* @param string $statusLabel
* @return boolean
*/
public function createOrderStatus($statusCode, $statusLabel)
{
$statusResource = $this->statusResourceFactory->create();
$status = $this->statusFactory->create();
$status->setData([
'status' => $statusCode,
'label' => $statusLabel,
]);

try {
$statusResource->save($status);
} catch (AlreadyExistsException $exception) {
return;
}

$status->assignState(Order::STATE_PROCESSING, false, true);

return true;
}
}
50 changes: 50 additions & 0 deletions Model/ShippingMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
namespace Commerce365\MagentoApiExtensions\Model;
use Commerce365\MagentoApiExtensions\Api\ShippingMethodManagementInterface;

class ShippingMethod implements ShippingMethodManagementInterface
{

protected $scopeConfig;

protected $shipmentConfig;

public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Shipping\Model\Config $shipmentConfig
) {
$this->shipmentConfig = $shipmentConfig;
$this->scopeConfig = $scopeConfig;
}

/**
* Get active carriers / shipping methods
*
* @api
* @return object
*/
public function getActiveMethods(){

$activeCarriers = $this->shipmentConfig->getActiveCarriers();
$methods = array();

foreach($activeCarriers as $carrierCode => $carrierModel)
{
$carrierTitle =$this->scopeConfig->getValue('carriers/'.$carrierCode.'/title');

if( $carrierMethods = $carrierModel->getAllowedMethods() )
{
foreach ($carrierMethods as $methodCode => $method)
{
$code= $carrierCode.'_'.$methodCode;
$methods[]=array(
'label'=>$carrierTitle,
'value'=>$code
);
}
}
}

return $methods;
}
}
25 changes: 25 additions & 0 deletions Plugin/ProductAttributeExtensionAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Commerce365\MagentoApiExtensions\Plugin;

use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Catalog\Model\Attribute as AttributeModel;

class ProductAttributeExtensionAttributes
{
public function afterGetAttributes(
\Magento\Catalog\Api\ProductAttributeManagementInterface $subject,
$searchResult
) {
foreach ($searchResult as &$attribute) {
$extensionAttributes = $attribute->getExtensionAttributes();
if($attribute->getAttributeGroupId()){
$extensionAttributes->setAttributeGroupId($attribute->getAttributeGroupId());
$extensionAttributes->setEntityAttributeId($attribute->getEntityAttributeId());
$extensionAttributes->setSortOrder($attribute->getSortOrder());
}
$attribute->setExtensionAttributes($extensionAttributes);
}
return $searchResult;
}
}
21 changes: 21 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "commerce365/module-apiextensions",
"version": "1.0.0",
"description": "N/A",
"type": "magento2-module",
"require": {
"magento/framework": "*",
"magento/module-sales": "*"
},
"license": [
""
],
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"Commerce365\\MagentoApiExtensions\\\\": ""
}
}
}
18 changes: 18 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

<preference for="Commerce365\MagentoApiExtensions\Api\CategoryManagementInterface"
type="Commerce365\MagentoApiExtensions\Model\Category" />

<preference for="Commerce365\MagentoApiExtensions\Api\ShippingMethodManagementInterface"
type="Commerce365\MagentoApiExtensions\Model\ShippingMethod" />

<preference for="Commerce365\MagentoApiExtensions\Api\PaymentMethodManagementInterface"
type="Commerce365\MagentoApiExtensions\Model\PaymentMethod" />

<preference for="Commerce365\MagentoApiExtensions\Api\MagentoManagementInterface"
type="Commerce365\MagentoApiExtensions\Model\Magento" />

<preference for="Commerce365\MagentoApiExtensions\Api\SalesManagementInterface"
type="Commerce365\MagentoApiExtensions\Model\Sales" />
</config>
Loading

0 comments on commit a64bf57

Please sign in to comment.