ltsecurityinc安防产品在线商城代码
This commit is contained in:
@@ -0,0 +1,206 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Bundle\Api;
|
||||
|
||||
use Magento\TestFramework\TestCase\WebapiAbstract;
|
||||
|
||||
class CartItemRepositoryTest extends WebapiAbstract
|
||||
{
|
||||
const SERVICE_VERSION = 'V1';
|
||||
const SERVICE_NAME = 'quoteCartItemRepositoryV1';
|
||||
const RESOURCE_PATH = '/V1/carts/';
|
||||
|
||||
/**
|
||||
* @var \Magento\TestFramework\ObjectManager
|
||||
*/
|
||||
protected $objectManager;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_bundle_and_options.php
|
||||
*/
|
||||
public function testGetAll()
|
||||
{
|
||||
/** @var $quote \Magento\Quote\Model\Quote */
|
||||
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class)->load(
|
||||
'test_order_bundle',
|
||||
'reserved_order_id'
|
||||
);
|
||||
$quoteId = $quote->getId();
|
||||
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH . $quoteId . '/items',
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_NAME,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => self::SERVICE_NAME . 'GetList',
|
||||
],
|
||||
];
|
||||
$response = $this->_webApiCall($serviceInfo, ['cartId' => $quoteId]);
|
||||
$this->assertCount(1, $response);
|
||||
$response = $response[0];
|
||||
$bundleOption = $quote->getItemById($response['item_id'])->getBuyRequest()->getBundleOption();
|
||||
$bundleOptionQty = $quote->getItemById($response['item_id'])->getBuyRequest()->getBundleOptionQty();
|
||||
$actualOptions = $response['product_option']['extension_attributes']['bundle_options'];
|
||||
|
||||
$this->assertEquals(array_keys($bundleOption), array_column($actualOptions, 'option_id'));
|
||||
$this->assertEquals($bundleOptionQty, array_column($actualOptions, 'option_qty', 'option_id'));
|
||||
foreach ($actualOptions as $option) {
|
||||
$id = $option['option_id'];
|
||||
$expectedSelections = is_array($bundleOption[$id]) ? $bundleOption[$id] : [$bundleOption[$id]];
|
||||
$this->assertEquals($expectedSelections, $option['option_selections']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_saved.php
|
||||
* @magentoApiDataFixture Magento/Bundle/_files/product.php
|
||||
*/
|
||||
public function testAddItem()
|
||||
{
|
||||
/** @var \Magento\Catalog\Model\Product $product */
|
||||
$product = $this->objectManager->create(\Magento\Catalog\Model\Product::class)->load(3);
|
||||
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class)->load(
|
||||
'test_order_item_with_items',
|
||||
'reserved_order_id'
|
||||
);
|
||||
|
||||
$itemQty = 1;
|
||||
$bundleProductOptions = $product->getExtensionAttributes()->getBundleProductOptions();
|
||||
$bundleOptionId = $bundleProductOptions[0]->getId();
|
||||
$optionSelections = $bundleProductOptions[0]->getProductLinks()[0]->getId();
|
||||
$buyRequest = [
|
||||
'bundle_option' => [$bundleOptionId => [$optionSelections]],
|
||||
'bundle_option_qty' => [$bundleOptionId => 1],
|
||||
'qty' => $itemQty,
|
||||
'original_qty' => $itemQty
|
||||
];
|
||||
|
||||
$productSku = $product->getSku();
|
||||
$productId = $product->getId();
|
||||
/** @var \Magento\Quote\Model\Quote $quote */
|
||||
|
||||
$cartId = $quote->getId();
|
||||
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH . $cartId . '/items',
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_NAME,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => self::SERVICE_NAME . 'Save',
|
||||
],
|
||||
];
|
||||
|
||||
$requestData = [
|
||||
"cartItem" => [
|
||||
"sku" => $productSku,
|
||||
"qty" => $itemQty,
|
||||
"quote_id" => $cartId,
|
||||
"product_option" => [
|
||||
"extension_attributes" => [
|
||||
"bundle_options" => [
|
||||
[
|
||||
"option_id" => (int)$bundleOptionId,
|
||||
"option_qty" => $itemQty,
|
||||
"option_selections" => [(int)$optionSelections]
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
];
|
||||
$response = $this->_webApiCall($serviceInfo, $requestData);
|
||||
$this->assertTrue($quote->hasProductId($productId));
|
||||
$this->assertEquals($buyRequest, $quote->getItemById($response['item_id'])->getBuyRequest()->getData());
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_bundle_and_options.php
|
||||
*/
|
||||
public function testUpdate()
|
||||
{
|
||||
/** @var \Magento\Quote\Model\Quote $quote */
|
||||
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class)->load(
|
||||
'test_order_bundle',
|
||||
'reserved_order_id'
|
||||
);
|
||||
$cartId = $quote->getId();
|
||||
$cartItem = $quote->getAllVisibleItems()[0];
|
||||
$itemSku = $cartItem->getSku();
|
||||
$itemId = $cartItem->getId();
|
||||
|
||||
$product = $cartItem->getProduct();
|
||||
/** @var $typeInstance \Magento\Bundle\Model\Product\Type */
|
||||
$typeInstance = $product->getTypeInstance();
|
||||
$typeInstance->setStoreFilter($product->getStoreId(), $product);
|
||||
$optionCollection = $typeInstance->getOptionsCollection($product);
|
||||
$bundleOptions = [];
|
||||
/** @var $option \Magento\Bundle\Model\Option */
|
||||
foreach ($optionCollection as $option) {
|
||||
if (!$option->getRequired()) {
|
||||
continue;
|
||||
}
|
||||
$selectionsCollection = $typeInstance->getSelectionsCollection([$option->getId()], $product);
|
||||
$option = ['option_id' => $option->getId(), 'option_qty' => 1];
|
||||
$option['option_selections'] = [$selectionsCollection->getLastItem()->getSelectionId()];
|
||||
$bundleOptions[] = $option;
|
||||
}
|
||||
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH . $cartId . '/items/' . $itemId,
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_NAME,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => self::SERVICE_NAME . 'Save',
|
||||
],
|
||||
];
|
||||
$requestData = [
|
||||
"cartItem" => [
|
||||
"sku" => $itemSku,
|
||||
"qty" => 2,
|
||||
"quote_id" => $cartId,
|
||||
"item_id" => $itemId,
|
||||
"product_option" => [
|
||||
"extension_attributes" => [
|
||||
"bundle_options" => $bundleOptions
|
||||
]
|
||||
]
|
||||
]
|
||||
];
|
||||
$this->_webApiCall($serviceInfo, $requestData);
|
||||
|
||||
$quoteUpdated = $this->objectManager->create(\Magento\Quote\Model\Quote::class)->load(
|
||||
'test_order_bundle',
|
||||
'reserved_order_id'
|
||||
);
|
||||
$cartItems = $quoteUpdated->getAllVisibleItems();
|
||||
$buyRequest = $cartItems[0]->getBuyRequest()->toArray();
|
||||
|
||||
$this->assertCount(1, $cartItems);
|
||||
$this->assertEquals(count($buyRequest['bundle_option']), count($bundleOptions));
|
||||
foreach ($bundleOptions as $option) {
|
||||
$optionId = $option['option_id'];
|
||||
$optionQty = $option['option_qty'];
|
||||
$optionSelections = $option['option_selections'];
|
||||
$this->assertArrayHasKey($optionId, $buyRequest['bundle_option']);
|
||||
$this->assertEquals($optionQty, $buyRequest['bundle_option_qty'][$optionId]);
|
||||
$this->assertEquals($optionSelections, $buyRequest['bundle_option'][$optionId]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Bundle\Api;
|
||||
|
||||
/**
|
||||
* API test for creation of Invoice for order with bundle product.
|
||||
*/
|
||||
class OrderInvoiceCreateTest extends \Magento\TestFramework\TestCase\WebapiAbstract
|
||||
{
|
||||
const SERVICE_READ_NAME = 'salesInvoiceOrderV1';
|
||||
const SERVICE_VERSION = 'V1';
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\ObjectManagerInterface
|
||||
*/
|
||||
private $objectManager;
|
||||
|
||||
/**
|
||||
* @var \Magento\Sales\Api\InvoiceRepositoryInterface
|
||||
*/
|
||||
private $invoiceRepository;
|
||||
|
||||
/**
|
||||
* Set up.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$this->invoiceRepository = $this->objectManager->get(
|
||||
\Magento\Sales\Api\InvoiceRepositoryInterface::class
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test create a partial invoice for order with bundle and Simple products.
|
||||
*
|
||||
* @return void
|
||||
* @magentoApiDataFixture Magento/Bundle/_files/order_items_simple_and_bundle.php
|
||||
*/
|
||||
public function testInvoiceWithSimpleAndBundleCreate()
|
||||
{
|
||||
/** @var \Magento\Sales\Api\Data\OrderInterface $existingOrder*/
|
||||
$existingOrder = $this->objectManager->create(\Magento\Sales\Api\Data\OrderInterface::class)
|
||||
->loadByIncrementId('100000001');
|
||||
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => '/V1/order/' . $existingOrder->getId() . '/invoice',
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_READ_NAME,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => self::SERVICE_READ_NAME . 'execute',
|
||||
],
|
||||
];
|
||||
|
||||
$requestData = [
|
||||
'orderId' => $existingOrder->getId(),
|
||||
'items' => [],
|
||||
'comment' => [
|
||||
'comment' => 'Test Comment',
|
||||
'is_visible_on_front' => 1,
|
||||
],
|
||||
];
|
||||
$grantTotal = 0;
|
||||
foreach ($existingOrder->getAllItems() as $item) {
|
||||
$requestData['items'] = [];
|
||||
$requestData['items'][] = [
|
||||
'order_item_id' => $item->getItemId(),
|
||||
'qty' => $item->getQtyOrdered(),
|
||||
];
|
||||
$result = $this->_webApiCall($serviceInfo, $requestData);
|
||||
$this->assertNotEmpty($result);
|
||||
try {
|
||||
$invoice = $this->invoiceRepository->get($result);
|
||||
$grantTotal += $invoice->getGrandTotal();
|
||||
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
|
||||
$this->fail('Failed asserting that Invoice was created');
|
||||
}
|
||||
}
|
||||
$this->assertNotEquals(
|
||||
$existingOrder->getGrandTotal(),
|
||||
$grantTotal,
|
||||
'Failed asserting that invoice is correct.'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test create invoice with Bundle product.
|
||||
*
|
||||
* @return void
|
||||
* @magentoApiDataFixture Magento/Bundle/_files/order_item_with_bundle_and_options.php
|
||||
*/
|
||||
public function testInvoiceWithBundleCreate()
|
||||
{
|
||||
/** @var \Magento\Sales\Api\Data\OrderInterface $existingOrder*/
|
||||
$existingOrder = $this->objectManager->create(\Magento\Sales\Api\Data\OrderInterface::class)
|
||||
->loadByIncrementId('100000001');
|
||||
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => '/V1/order/' . $existingOrder->getId() . '/invoice',
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_READ_NAME,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => self::SERVICE_READ_NAME . 'execute',
|
||||
],
|
||||
];
|
||||
|
||||
$requestData = [
|
||||
'orderId' => $existingOrder->getId(),
|
||||
'items' => [],
|
||||
'comment' => [
|
||||
'comment' => 'Test Comment',
|
||||
'is_visible_on_front' => 1,
|
||||
],
|
||||
];
|
||||
|
||||
/** @var \Magento\Sales\Api\Data\OrderItemInterface $item */
|
||||
foreach ($existingOrder->getAllItems() as $item) {
|
||||
$requestData['items'][] = [
|
||||
'order_item_id' => $item->getItemId(),
|
||||
'qty' => $item->getQtyOrdered(),
|
||||
];
|
||||
}
|
||||
$result = $this->_webApiCall($serviceInfo, $requestData);
|
||||
$this->assertNotEmpty($result);
|
||||
$invoice = $this->invoiceRepository->get($result);
|
||||
$this->assertNotEquals(
|
||||
$existingOrder->getGrandTotal(),
|
||||
$invoice->getGrandTotal(),
|
||||
'Failed asserting that invoice is correct.'
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Bundle\Api;
|
||||
|
||||
use Magento\TestFramework\TestCase\WebapiAbstract;
|
||||
|
||||
class OrderItemRepositoryTest extends WebapiAbstract
|
||||
{
|
||||
const RESOURCE_PATH = '/V1/orders/items';
|
||||
|
||||
const SERVICE_VERSION = 'V1';
|
||||
const SERVICE_NAME = 'salesOrderItemRepositoryV1';
|
||||
|
||||
const ORDER_INCREMENT_ID = '100000001';
|
||||
|
||||
/**
|
||||
* @var \Magento\TestFramework\ObjectManager
|
||||
*/
|
||||
protected $objectManager;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Bundle/_files/order_item_with_bundle_and_options.php
|
||||
*/
|
||||
public function testGet()
|
||||
{
|
||||
/** @var \Magento\Sales\Model\Order $order */
|
||||
$order = $this->objectManager->create(\Magento\Sales\Model\Order::class);
|
||||
$order->loadByIncrementId(self::ORDER_INCREMENT_ID);
|
||||
$orderItem = current($order->getItems());
|
||||
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH . '/' . $orderItem->getId(),
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_NAME,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => self::SERVICE_NAME . 'get',
|
||||
],
|
||||
];
|
||||
|
||||
$response = $this->_webApiCall($serviceInfo, ['id' => $orderItem->getId()]);
|
||||
|
||||
$this->assertIsArray($response);
|
||||
$this->assertOrderItem($orderItem, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Bundle/_files/order_item_with_bundle_and_options.php
|
||||
*/
|
||||
public function testGetList()
|
||||
{
|
||||
/** @var \Magento\Sales\Model\Order $order */
|
||||
$order = $this->objectManager->create(\Magento\Sales\Model\Order::class);
|
||||
$order->loadByIncrementId(self::ORDER_INCREMENT_ID);
|
||||
|
||||
/** @var $searchCriteriaBuilder \Magento\Framework\Api\SearchCriteriaBuilder */
|
||||
$searchCriteriaBuilder = $this->objectManager->create(\Magento\Framework\Api\SearchCriteriaBuilder::class);
|
||||
/** @var $filterBuilder \Magento\Framework\Api\FilterBuilder */
|
||||
$filterBuilder = $this->objectManager->create(\Magento\Framework\Api\FilterBuilder::class);
|
||||
|
||||
$searchCriteriaBuilder->addFilters(
|
||||
[
|
||||
$filterBuilder->setField('order_id')
|
||||
->setValue($order->getId())
|
||||
->create(),
|
||||
]
|
||||
);
|
||||
|
||||
$requestData = ['searchCriteria' => $searchCriteriaBuilder->create()->__toArray()];
|
||||
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH . '?' . http_build_query($requestData),
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_NAME,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => self::SERVICE_NAME . 'getList',
|
||||
],
|
||||
];
|
||||
|
||||
$response = $this->_webApiCall($serviceInfo, $requestData);
|
||||
|
||||
$this->assertIsArray($response);
|
||||
$this->assertArrayHasKey('items', $response);
|
||||
$this->assertCount(1, $response['items']);
|
||||
$this->assertIsArray($response['items'][0]);
|
||||
$this->assertOrderItem(current($order->getItems()), $response['items'][0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Magento\Sales\Model\Order\Item $orderItem
|
||||
* @param array $response
|
||||
* @return void
|
||||
*/
|
||||
protected function assertOrderItem(\Magento\Sales\Model\Order\Item $orderItem, array $response)
|
||||
{
|
||||
$bundleOption = $orderItem->getBuyRequest()->getBundleOption();
|
||||
$bundleOptionQty = $orderItem->getBuyRequest()->getBundleOptionQty();
|
||||
|
||||
$this->assertArrayHasKey('product_option', $response);
|
||||
$this->assertArrayHasKey('extension_attributes', $response['product_option']);
|
||||
$this->assertArrayHasKey('bundle_options', $response['product_option']['extension_attributes']);
|
||||
|
||||
$actualOptions = $response['product_option']['extension_attributes']['bundle_options'];
|
||||
|
||||
$this->assertEquals(array_keys($bundleOption), array_column($actualOptions, 'option_id'));
|
||||
$this->assertEquals($bundleOptionQty, array_column($actualOptions, 'option_qty', 'option_id'));
|
||||
|
||||
foreach ($actualOptions as $option) {
|
||||
$expectedSelections = is_array($bundleOption[$option['option_id']])
|
||||
? $bundleOption[$option['option_id']]
|
||||
: [$bundleOption[$option['option_id']]];
|
||||
$this->assertEquals($expectedSelections, $option['option_selections']);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,315 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Magento\Bundle\Api;
|
||||
|
||||
use Magento\Framework\Webapi\Rest\Request;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\TestCase\WebapiAbstract;
|
||||
|
||||
class ProductLinkManagementTest extends WebapiAbstract
|
||||
{
|
||||
const SERVICE_NAME = 'bundleProductLinkManagementV1';
|
||||
const SERVICE_VERSION = 'V1';
|
||||
const RESOURCE_PATH = '/V1/bundle-products';
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Bundle/_files/product.php
|
||||
*/
|
||||
public function testGetChildren()
|
||||
{
|
||||
$productSku = 'bundle-product';
|
||||
$expected = [
|
||||
[
|
||||
'sku' => 'simple',
|
||||
'position' => 0,
|
||||
'qty' => 1,
|
||||
],
|
||||
];
|
||||
|
||||
$result = $this->getChildren($productSku);
|
||||
|
||||
$this->assertArrayHasKey(0, $result);
|
||||
$this->assertArrayHasKey('option_id', $result[0]);
|
||||
$this->assertArrayHasKey('is_default', $result[0]);
|
||||
$this->assertArrayHasKey('can_change_quantity', $result[0]);
|
||||
$this->assertArrayHasKey('price', $result[0]);
|
||||
$this->assertArrayHasKey('price_type', $result[0]);
|
||||
$this->assertNotNull($result[0]['id']);
|
||||
|
||||
unset($result[0]['option_id'], $result[0]['is_default'], $result[0]['can_change_quantity']);
|
||||
unset($result[0]['price'], $result[0]['price_type'], $result[0]['id']);
|
||||
|
||||
ksort($result[0]);
|
||||
ksort($expected[0]);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Bundle/_files/product.php
|
||||
*/
|
||||
public function testRemoveChild()
|
||||
{
|
||||
$productSku = 'bundle-product';
|
||||
$childSku = 'simple';
|
||||
$optionIds = $this->getProductOptions(3);
|
||||
$optionId = array_shift($optionIds);
|
||||
$this->assertTrue($this->removeChild($productSku, $optionId, $childSku));
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Bundle/_files/product.php
|
||||
* @magentoApiDataFixture Magento/Catalog/_files/product_virtual.php
|
||||
*/
|
||||
public function testAddChild()
|
||||
{
|
||||
$productSku = 'bundle-product';
|
||||
$children = $this->getChildren($productSku);
|
||||
|
||||
$optionId = $children[0]['option_id'];
|
||||
|
||||
$linkedProduct = [
|
||||
'sku' => 'virtual-product',
|
||||
'option_id' => $optionId,
|
||||
'position' => '1',
|
||||
'is_default' => 1,
|
||||
'priceType' => 2,
|
||||
'price' => 151.34,
|
||||
'qty' => 8,
|
||||
'can_change_quantity' => 1,
|
||||
];
|
||||
|
||||
$childId = $this->addChild($productSku, $optionId, $linkedProduct);
|
||||
$this->assertGreaterThan(0, $childId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify empty out of stock bundle product is in stock after child has been added.
|
||||
*
|
||||
* @magentoApiDataFixture Magento/Bundle/_files/empty_bundle_product.php
|
||||
* @magentoApiDataFixture Magento/Catalog/_files/product_virtual.php
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testBundleProductIsInStockAfterAddChild(): void
|
||||
{
|
||||
$productSku = 'bundle-product';
|
||||
$option = [
|
||||
'required' => 1,
|
||||
'position' => 0,
|
||||
'type' => 'select',
|
||||
'title' => 'option 1',
|
||||
'sku' => $productSku,
|
||||
];
|
||||
self::assertFalse($this->isProductInStock($productSku));
|
||||
$optionId = $this->addOption($option);
|
||||
$linkedProduct = [
|
||||
'sku' => 'virtual-product',
|
||||
'option_id' => $optionId,
|
||||
'position' => '1',
|
||||
'is_default' => 1,
|
||||
'priceType' => 2,
|
||||
'price' => 151.34,
|
||||
'qty' => 8,
|
||||
'can_change_quantity' => 1,
|
||||
];
|
||||
|
||||
$this->addChild($productSku, $optionId, $linkedProduct);
|
||||
self::assertTrue($this->isProductInStock($productSku));
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify in stock bundle product is out stock after child has been removed.
|
||||
*
|
||||
* @magentoApiDataFixture Magento/Bundle/_files/product.php
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testBundleProductIsOutOfStockAfterRemoveChild(): void
|
||||
{
|
||||
$productSku = 'bundle-product';
|
||||
$childSku = 'simple';
|
||||
$optionIds = $this->getProductOptions(3);
|
||||
$optionId = array_shift($optionIds);
|
||||
self::assertTrue($this->isProductInStock($productSku));
|
||||
$this->removeChild($productSku, $optionId, $childSku);
|
||||
self::assertFalse($this->isProductInStock($productSku));
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Bundle/_files/product.php
|
||||
* @magentoApiDataFixture Magento/Catalog/_files/product_virtual.php
|
||||
*/
|
||||
public function testSaveChild()
|
||||
{
|
||||
$productSku = 'bundle-product';
|
||||
$children = $this->getChildren($productSku);
|
||||
|
||||
$linkedProduct = $children[0];
|
||||
|
||||
//Modify a few fields
|
||||
$linkedProduct['is_default'] = true;
|
||||
$linkedProduct['qty'] = 2;
|
||||
|
||||
$this->assertTrue($this->saveChild($productSku, $linkedProduct));
|
||||
$children = $this->getChildren($productSku);
|
||||
$this->assertEquals($linkedProduct, $children[0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $productSku
|
||||
* @param array $linkedProduct
|
||||
* @return string
|
||||
*/
|
||||
private function saveChild($productSku, $linkedProduct)
|
||||
{
|
||||
$resourcePath = self::RESOURCE_PATH . '/:sku/links/:id';
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => str_replace(
|
||||
[':sku', ':id'],
|
||||
[$productSku, $linkedProduct['id']],
|
||||
$resourcePath
|
||||
),
|
||||
'httpMethod' => Request::HTTP_METHOD_PUT,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_NAME,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => self::SERVICE_NAME . 'SaveChild',
|
||||
],
|
||||
];
|
||||
return $this->_webApiCall(
|
||||
$serviceInfo,
|
||||
['sku' => $productSku, 'linkedProduct' => $linkedProduct]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $productSku
|
||||
* @param int $optionId
|
||||
* @param array $linkedProduct
|
||||
* @return string
|
||||
*/
|
||||
private function addChild($productSku, $optionId, $linkedProduct)
|
||||
{
|
||||
$resourcePath = self::RESOURCE_PATH . '/:sku/links/:optionId';
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => str_replace(
|
||||
[':sku', ':optionId'],
|
||||
[$productSku, $optionId],
|
||||
$resourcePath
|
||||
),
|
||||
'httpMethod' => Request::HTTP_METHOD_POST,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_NAME,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => self::SERVICE_NAME . 'AddChildByProductSku',
|
||||
],
|
||||
];
|
||||
return $this->_webApiCall(
|
||||
$serviceInfo,
|
||||
['sku' => $productSku, 'optionId' => $optionId, 'linkedProduct' => $linkedProduct]
|
||||
);
|
||||
}
|
||||
|
||||
protected function getProductOptions($productId)
|
||||
{
|
||||
/** @var \Magento\Catalog\Model\Product $product */
|
||||
$product = Bootstrap::getObjectManager()->get(\Magento\Catalog\Model\Product::class);
|
||||
$product->load($productId);
|
||||
/** @var \Magento\Bundle\Model\Product\Type $type */
|
||||
$type = Bootstrap::getObjectManager()->get(\Magento\Bundle\Model\Product\Type::class);
|
||||
return $type->getOptionsIds($product);
|
||||
}
|
||||
|
||||
protected function removeChild($productSku, $optionId, $childSku)
|
||||
{
|
||||
$resourcePath = self::RESOURCE_PATH . '/%s/options/%s/children/%s';
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => sprintf($resourcePath, $productSku, $optionId, $childSku),
|
||||
'httpMethod' => Request::HTTP_METHOD_DELETE,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_NAME,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => self::SERVICE_NAME . 'removeChild',
|
||||
],
|
||||
];
|
||||
$requestData = ['sku' => $productSku, 'optionId' => $optionId, 'childSku' => $childSku];
|
||||
return $this->_webApiCall($serviceInfo, $requestData);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $productSku
|
||||
* @return string
|
||||
*/
|
||||
protected function getChildren($productSku)
|
||||
{
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH . '/' . $productSku . '/children',
|
||||
'httpMethod' => Request::HTTP_METHOD_GET,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_NAME,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => self::SERVICE_NAME . 'getChildren',
|
||||
],
|
||||
];
|
||||
return $this->_webApiCall($serviceInfo, ['productSku' => $productSku]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check product stock status.
|
||||
*
|
||||
* @param string $productSku
|
||||
* @return bool
|
||||
*/
|
||||
private function isProductInStock(string $productSku): bool
|
||||
{
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => '/V1/stockStatuses/' . $productSku,
|
||||
'httpMethod' => Request::HTTP_METHOD_GET,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => 'catalogInventoryStockRegistryV1',
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => 'catalogInventoryStockRegistryV1getStockStatusBySku',
|
||||
],
|
||||
];
|
||||
$result = $this->_webApiCall($serviceInfo, ['productSku' => $productSku]);
|
||||
|
||||
return (bool)$result['stock_status'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Add option to bundle product.
|
||||
*
|
||||
* @param array $option
|
||||
* @return int
|
||||
*/
|
||||
private function addOption(array $option): int
|
||||
{
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => '/V1/bundle-products/options/add',
|
||||
'httpMethod' => Request::HTTP_METHOD_POST,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => 'bundleProductOptionManagementV1',
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => 'bundleProductOptionManagementV1Save',
|
||||
],
|
||||
];
|
||||
return $this->_webApiCall($serviceInfo, ['option' => $option]);
|
||||
}
|
||||
}
|
||||
+280
@@ -0,0 +1,280 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Bundle\Api;
|
||||
|
||||
class ProductOptionRepositoryTest extends \Magento\TestFramework\TestCase\WebapiAbstract
|
||||
{
|
||||
const SERVICE_NAME = 'bundleProductOptionRepositoryV1';
|
||||
const SERVICE_VERSION = 'V1';
|
||||
const RESOURCE_PATH = '/V1/bundle-products/:sku/options';
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Bundle/_files/product.php
|
||||
*/
|
||||
public function testGet()
|
||||
{
|
||||
$productSku = 'bundle-product';
|
||||
$expected = [
|
||||
'required' => true,
|
||||
'position' => 0,
|
||||
'type' => 'select',
|
||||
'title' => 'Bundle Product Items',
|
||||
'sku' => $productSku,
|
||||
'product_links' => [
|
||||
[
|
||||
'sku' => 'simple',
|
||||
'qty' => 1,
|
||||
'position' => 0,
|
||||
'can_change_quantity' => 1,
|
||||
'is_default' => false,
|
||||
'price' => 2.75,
|
||||
'price_type' => 0,
|
||||
],
|
||||
],
|
||||
];
|
||||
$optionId = $this->getList($productSku)[0]['option_id'];
|
||||
$result = $this->get($productSku, $optionId);
|
||||
|
||||
$this->assertArrayHasKey('option_id', $result);
|
||||
$expected['product_links'][0]['option_id'] = $result['option_id'];
|
||||
unset($result['option_id']);
|
||||
$this->assertNotNull($result['product_links'][0]['id']);
|
||||
unset($result['product_links'][0]['id']);
|
||||
|
||||
ksort($expected);
|
||||
ksort($result);
|
||||
ksort($expected['product_links'][0]);
|
||||
ksort($result['product_links'][0]);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Bundle/_files/product.php
|
||||
*/
|
||||
public function testGetList()
|
||||
{
|
||||
$productSku = 'bundle-product';
|
||||
$expected = [
|
||||
[
|
||||
'required' => true,
|
||||
'position' => 0,
|
||||
'type' => 'select',
|
||||
'title' => 'Bundle Product Items',
|
||||
'sku' => $productSku,
|
||||
'product_links' => [
|
||||
[
|
||||
'sku' => 'simple',
|
||||
'qty' => 1,
|
||||
'position' => 0,
|
||||
'can_change_quantity' => 1,
|
||||
'is_default' => false,
|
||||
'price' => 2.75,
|
||||
'price_type' => 0,
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
$result = $this->getList($productSku);
|
||||
|
||||
$this->assertArrayHasKey(0, $result);
|
||||
$this->assertArrayHasKey('option_id', $result[0]);
|
||||
$expected[0]['product_links'][0]['option_id'] = $result[0]['option_id'];
|
||||
unset($result[0]['option_id']);
|
||||
$this->assertNotNull($result[0]['product_links'][0]['id']);
|
||||
unset($result[0]['product_links'][0]['id']);
|
||||
|
||||
ksort($expected[0]);
|
||||
ksort($result[0]);
|
||||
ksort($expected[0]['product_links'][0]);
|
||||
ksort($result[0]['product_links'][0]);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Bundle/_files/product.php
|
||||
*/
|
||||
public function testRemove()
|
||||
{
|
||||
$this->expectException(\Magento\Framework\Exception\NoSuchEntityException::class);
|
||||
|
||||
$productSku = 'bundle-product';
|
||||
|
||||
$optionId = $this->getList($productSku)[0]['option_id'];
|
||||
$result = $this->remove($productSku, $optionId);
|
||||
|
||||
$this->assertTrue($result);
|
||||
|
||||
try {
|
||||
$this->get($productSku, $optionId);
|
||||
} catch (\Exception $e) {
|
||||
throw new \Magento\Framework\Exception\NoSuchEntityException();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Bundle/_files/product.php
|
||||
*/
|
||||
public function testAdd()
|
||||
{
|
||||
$productSku = 'bundle-product';
|
||||
$request = [
|
||||
'required' => true,
|
||||
'position' => 0,
|
||||
'type' => 'select',
|
||||
'title' => 'test product',
|
||||
'product_links' => [],
|
||||
'sku' => $productSku,
|
||||
];
|
||||
|
||||
$optionId = $this->add($request);
|
||||
$this->assertGreaterThan(0, $optionId);
|
||||
$result = $this->get($productSku, $optionId);
|
||||
|
||||
$this->assertArrayHasKey('option_id', $result);
|
||||
$this->assertArrayHasKey('sku', $result);
|
||||
unset($result['option_id']);
|
||||
|
||||
ksort($result);
|
||||
ksort($request);
|
||||
$this->assertEquals($request, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Bundle/_files/product.php
|
||||
*/
|
||||
public function testUpdate()
|
||||
{
|
||||
$productSku = 'bundle-product';
|
||||
$request = [
|
||||
'title' => 'someTitle',
|
||||
'type' => 'select',
|
||||
'required' => 1,
|
||||
'sku' => $productSku,
|
||||
];
|
||||
|
||||
$options = $this->getList($productSku);
|
||||
|
||||
$this->assertGreaterThan(0, count($options));
|
||||
$this->assertArrayHasKey('option_id', $options[0]);
|
||||
|
||||
$optionId = $options[0]['option_id'];
|
||||
$this->update($optionId, $request);
|
||||
|
||||
$result = $this->get($productSku, $optionId);
|
||||
|
||||
$this->assertCount(7, $result);
|
||||
$this->assertArrayHasKey('title', $result);
|
||||
$this->assertEquals($request['title'], $result['title']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $optionId
|
||||
* @param array $option
|
||||
* @return string
|
||||
*/
|
||||
protected function update($optionId, $option)
|
||||
{
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => '/V1/bundle-products/options/' . $optionId,
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => 'bundleProductOptionManagementV1',
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => 'bundleProductOptionManagementV1Save',
|
||||
],
|
||||
];
|
||||
|
||||
if (TESTS_WEB_API_ADAPTER == self::ADAPTER_SOAP) {
|
||||
$option['optionId'] = $optionId;
|
||||
}
|
||||
return $this->_webApiCall($serviceInfo, ['option' => $option]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $option
|
||||
* @return string
|
||||
*/
|
||||
protected function add($option)
|
||||
{
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => '/V1/bundle-products/options/add',
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => 'bundleProductOptionManagementV1',
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => 'bundleProductOptionManagementV1Save',
|
||||
],
|
||||
];
|
||||
return $this->_webApiCall($serviceInfo, ['option' => $option]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $productSku
|
||||
* @param int $optionId
|
||||
* @return string
|
||||
*/
|
||||
protected function remove($productSku, $optionId)
|
||||
{
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => str_replace(':sku', $productSku, self::RESOURCE_PATH) . '/' . $optionId,
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_NAME,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => self::SERVICE_NAME . 'DeleteById',
|
||||
],
|
||||
];
|
||||
return $this->_webApiCall($serviceInfo, ['sku' => $productSku, 'optionId' => $optionId]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $productSku
|
||||
* @return string
|
||||
*/
|
||||
protected function getList($productSku)
|
||||
{
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => str_replace(':sku', $productSku, self::RESOURCE_PATH) . '/all',
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_NAME,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => self::SERVICE_NAME . 'GetList',
|
||||
],
|
||||
];
|
||||
return $this->_webApiCall($serviceInfo, ['sku' => $productSku]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $productSku
|
||||
* @param int $optionId
|
||||
* @return string
|
||||
*/
|
||||
protected function get($productSku, $optionId)
|
||||
{
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => str_replace(':sku', $productSku, self::RESOURCE_PATH) . '/' . $optionId,
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_NAME,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => self::SERVICE_NAME . 'Get',
|
||||
],
|
||||
];
|
||||
return $this->_webApiCall($serviceInfo, ['sku' => $productSku, 'optionId' => $optionId]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Bundle\Api;
|
||||
|
||||
class ProductOptionTypeListTest extends \Magento\TestFramework\TestCase\WebapiAbstract
|
||||
{
|
||||
const SERVICE_READ_NAME = 'bundleProductOptionTypeListV1';
|
||||
const SERVICE_VERSION = 'V1';
|
||||
const RESOURCE_PATH = '/V1/bundle-products/options/types';
|
||||
|
||||
public function testGetTypes()
|
||||
{
|
||||
$expected = [
|
||||
['label' => 'Drop-down', 'code' => 'select'],
|
||||
['label' => 'Radio Buttons', 'code' => 'radio'],
|
||||
['label' => 'Checkbox', 'code' => 'checkbox'],
|
||||
['label' => 'Multiple Select', 'code' => 'multi'],
|
||||
];
|
||||
$result = $this->getTypes();
|
||||
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected function getTypes()
|
||||
{
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH,
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_READ_NAME,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => self::SERVICE_READ_NAME . 'getItems',
|
||||
],
|
||||
];
|
||||
return $this->_webApiCall($serviceInfo);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,622 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Magento\Bundle\Api;
|
||||
|
||||
use Magento\Bundle\Test\Fixture\Option as BundleOptionFixture;
|
||||
use Magento\Bundle\Test\Fixture\Product as BundleProductFixture;
|
||||
use Magento\Catalog\Api\Data\ProductInterface;
|
||||
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
|
||||
use Magento\Framework\Api\ExtensibleDataInterface;
|
||||
use Magento\Store\Test\Fixture\Store as StoreFixture;
|
||||
use Magento\TestFramework\Fixture\DataFixture;
|
||||
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\TestCase\WebapiAbstract;
|
||||
use Magento\Bundle\Api\Data\LinkInterface;
|
||||
|
||||
/**
|
||||
* Class ProductServiceTest for testing Bundle Product API
|
||||
*/
|
||||
class ProductServiceTest extends WebapiAbstract
|
||||
{
|
||||
private const SERVICE_NAME = 'catalogProductRepositoryV1';
|
||||
private const SERVICE_VERSION = 'V1';
|
||||
private const RESOURCE_PATH = '/V1/products';
|
||||
private const BUNDLE_PRODUCT_ID = 'sku-test-product-bundle';
|
||||
|
||||
/**
|
||||
* @var \Magento\Catalog\Model\ResourceModel\Product\Collection
|
||||
*/
|
||||
protected $productCollection;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $cleanUpOnTearDown = true;
|
||||
|
||||
/**
|
||||
* Execute per test initialization
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$objectManager = Bootstrap::getObjectManager();
|
||||
$this->productCollection = $objectManager->get(\Magento\Catalog\Model\ResourceModel\Product\Collection::class);
|
||||
$this->cleanUpOnTearDown = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute per test cleanup
|
||||
*/
|
||||
protected function tearDown(): void
|
||||
{
|
||||
if ($this->cleanUpOnTearDown) {
|
||||
$this->deleteProductBySku(self::BUNDLE_PRODUCT_ID);
|
||||
}
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Catalog/_files/products_new.php
|
||||
*/
|
||||
public function testCreateBundle()
|
||||
{
|
||||
$bundleProductOptions = [
|
||||
[
|
||||
"title" => "test option",
|
||||
"type" => "checkbox",
|
||||
"required" => true,
|
||||
"product_links" => [
|
||||
[
|
||||
"sku" => 'simple',
|
||||
"qty" => 1,
|
||||
'is_default' => false,
|
||||
'price' => 1.0,
|
||||
'price_type' => LinkInterface::PRICE_TYPE_FIXED,
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$product = [
|
||||
"sku" => self::BUNDLE_PRODUCT_ID,
|
||||
"name" => self::BUNDLE_PRODUCT_ID,
|
||||
"type_id" => "bundle",
|
||||
"price" => 50,
|
||||
'attribute_set_id' => 4,
|
||||
"custom_attributes" => [
|
||||
[
|
||||
"attribute_code" => "price_type",
|
||||
"value" => \Magento\Bundle\Model\Product\Price::PRICE_TYPE_FIXED,
|
||||
],
|
||||
[
|
||||
"attribute_code" => "price_view",
|
||||
"value" => 1,
|
||||
],
|
||||
],
|
||||
"extension_attributes" => [
|
||||
"bundle_product_options" => $bundleProductOptions,
|
||||
],
|
||||
];
|
||||
|
||||
$response = $this->createProduct($product);
|
||||
|
||||
$this->assertEquals(self::BUNDLE_PRODUCT_ID, $response[ProductInterface::SKU]);
|
||||
$this->assertEquals(50, $response['price']);
|
||||
$this->assertTrue(
|
||||
isset($response[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]["bundle_product_options"])
|
||||
);
|
||||
$resultBundleProductOptions
|
||||
= $response[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]["bundle_product_options"];
|
||||
$this->assertTrue(isset($resultBundleProductOptions[0]["product_links"][0]["sku"]));
|
||||
$this->assertEquals('simple', $resultBundleProductOptions[0]["product_links"][0]["sku"]);
|
||||
|
||||
$response = $this->getProduct(self::BUNDLE_PRODUCT_ID);
|
||||
$this->assertTrue(
|
||||
isset($response[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]["bundle_product_options"])
|
||||
);
|
||||
$resultBundleProductOptions
|
||||
= $response[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]["bundle_product_options"];
|
||||
$this->assertTrue(isset($resultBundleProductOptions[0]["product_links"][0]["sku"]));
|
||||
$this->assertEquals('simple', $resultBundleProductOptions[0]["product_links"][0]["sku"]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Catalog/_files/products_new.php
|
||||
* @magentoApiDataFixture Magento/Catalog/_files/second_product_simple.php
|
||||
*/
|
||||
public function testUpdateBundleModifyExistingSelection()
|
||||
{
|
||||
$bundleProduct = $this->createFixedPriceBundleProduct();
|
||||
$bundleProductOptions = $this->getBundleProductOptions($bundleProduct);
|
||||
|
||||
//Change the type of existing option
|
||||
$bundleProductOptions[0]['type'] = 'select';
|
||||
//Change the sku of existing link and qty
|
||||
$bundleProductOptions[0]['product_links'][0]['sku'] = 'simple2';
|
||||
$bundleProductOptions[0]['product_links'][0]['qty'] = 2;
|
||||
$bundleProductOptions[0]['product_links'][0]['price'] = 10;
|
||||
$bundleProductOptions[0]['product_links'][0]['price_type'] = 1;
|
||||
$this->setBundleProductOptions($bundleProduct, $bundleProductOptions);
|
||||
|
||||
$updatedProduct = $this->saveProduct($bundleProduct);
|
||||
|
||||
$bundleOptions = $this->getBundleProductOptions($updatedProduct);
|
||||
$this->assertEquals('select', $bundleOptions[0]['type']);
|
||||
$this->assertEquals('simple2', $bundleOptions[0]['product_links'][0]['sku']);
|
||||
$this->assertEquals(2, $bundleOptions[0]['product_links'][0]['qty']);
|
||||
$this->assertEquals(10, $bundleOptions[0]['product_links'][0]['price']);
|
||||
$this->assertEquals(1, $bundleOptions[0]['product_links'][0]['price_type']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Catalog/_files/products_new.php
|
||||
* @magentoApiDataFixture Magento/Catalog/_files/second_product_simple.php
|
||||
*/
|
||||
public function testUpdateBundleModifyExistingOptionOnly()
|
||||
{
|
||||
$bundleProduct = $this->createFixedPriceBundleProduct();
|
||||
$bundleProductOptions = $this->getBundleProductOptions($bundleProduct);
|
||||
|
||||
//Change the type of existing option
|
||||
$bundleProductOptions[0]['type'] = 'select';
|
||||
|
||||
$this->setBundleProductOptions($bundleProduct, $bundleProductOptions);
|
||||
|
||||
$updatedProduct = $this->saveProduct($bundleProduct);
|
||||
|
||||
$bundleOptions = $this->getBundleProductOptions($updatedProduct);
|
||||
$this->assertEquals('select', $bundleOptions[0]['type']);
|
||||
$this->assertEquals('simple', $bundleOptions[0]['product_links'][0]['sku']);
|
||||
$this->assertEquals(1, $bundleOptions[0]['product_links'][0]['qty']);
|
||||
$this->assertEquals(20, $bundleOptions[0]['product_links'][0]['price']);
|
||||
$this->assertEquals(1, $bundleOptions[0]['product_links'][0]['price_type']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Catalog/_files/products_new.php
|
||||
* @magentoApiDataFixture Magento/Catalog/_files/second_product_simple.php
|
||||
*/
|
||||
public function testUpdateProductWithoutBundleOptions()
|
||||
{
|
||||
$bundleProduct = $this->createFixedPriceBundleProduct();
|
||||
$bundleProductOptions = $this->getBundleProductOptions($bundleProduct);
|
||||
|
||||
$existingSelectionId = $bundleProductOptions[0]['product_links'][0]['id'];
|
||||
|
||||
//unset bundle_product_options
|
||||
unset($bundleProductOptions[0]['product_links']);
|
||||
$this->setBundleProductOptions($bundleProduct, null);
|
||||
|
||||
$updatedProduct = $this->saveProduct($bundleProduct);
|
||||
|
||||
$bundleOptions = $this->getBundleProductOptions($updatedProduct);
|
||||
$this->assertEquals('checkbox', $bundleOptions[0]['type']);
|
||||
$this->assertEquals('simple', $bundleOptions[0]['product_links'][0]['sku']);
|
||||
$this->assertEquals(1, $bundleOptions[0]['product_links'][0]['qty']);
|
||||
$this->assertEquals($existingSelectionId, $bundleOptions[0]['product_links'][0]['id']);
|
||||
$this->assertEquals(20, $bundleOptions[0]['product_links'][0]['price']);
|
||||
$this->assertEquals(1, $bundleOptions[0]['product_links'][0]['price_type']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Catalog/_files/products_new.php
|
||||
* @magentoApiDataFixture Magento/Catalog/_files/second_product_simple.php
|
||||
*/
|
||||
public function testUpdateBundleAddSelection()
|
||||
{
|
||||
$bundleProduct = $this->createDynamicBundleProduct();
|
||||
$bundleProductOptions = $this->getBundleProductOptions($bundleProduct);
|
||||
|
||||
//Add a selection to existing option
|
||||
$bundleProductOptions[0]['product_links'][] = [
|
||||
'sku' => 'simple2',
|
||||
'qty' => 2,
|
||||
"price" => 20,
|
||||
"price_type" => 1,
|
||||
"is_default" => false,
|
||||
];
|
||||
$this->setBundleProductOptions($bundleProduct, $bundleProductOptions);
|
||||
$updatedProduct = $this->saveProduct($bundleProduct);
|
||||
|
||||
$bundleOptions = $this->getBundleProductOptions($updatedProduct);
|
||||
$this->assertEquals('simple', $bundleOptions[0]['product_links'][0]['sku']);
|
||||
$this->assertEquals('simple2', $bundleOptions[0]['product_links'][1]['sku']);
|
||||
$this->assertEquals(2, $bundleOptions[0]['product_links'][1]['qty']);
|
||||
$this->assertGreaterThan(
|
||||
$bundleOptions[0]['product_links'][0]['id'],
|
||||
$bundleOptions[0]['product_links'][1]['id']
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Catalog/_files/products_new.php
|
||||
* @magentoApiDataFixture Magento/Catalog/_files/second_product_simple.php
|
||||
*/
|
||||
public function testUpdateBundleAddAndDeleteOption()
|
||||
{
|
||||
$bundleProduct = $this->createDynamicBundleProduct();
|
||||
$bundleProductOptions = $this->getBundleProductOptions($bundleProduct);
|
||||
|
||||
$oldOptionId = $bundleProductOptions[0]['option_id'];
|
||||
//replace current option with a new option
|
||||
$bundleProductOptions[0] = [
|
||||
'title' => 'new option',
|
||||
'required' => true,
|
||||
'type' => 'select',
|
||||
'product_links' => [
|
||||
[
|
||||
'sku' => 'simple2',
|
||||
'qty' => 2,
|
||||
"price" => 20,
|
||||
"price_type" => 1,
|
||||
"is_default" => false,
|
||||
],
|
||||
],
|
||||
];
|
||||
$this->setBundleProductOptions($bundleProduct, $bundleProductOptions);
|
||||
$this->saveProduct($bundleProduct);
|
||||
|
||||
$updatedProduct = $this->getProduct(self::BUNDLE_PRODUCT_ID);
|
||||
$bundleOptions = $this->getBundleProductOptions($updatedProduct);
|
||||
$simpleProduct = $this->getProduct('simple2');
|
||||
$this->assertEquals('new option', $bundleOptions[0]['title']);
|
||||
$this->assertTrue($bundleOptions[0]['required']);
|
||||
$this->assertEquals('select', $bundleOptions[0]['type']);
|
||||
$this->assertGreaterThan($oldOptionId, $bundleOptions[0]['option_id']);
|
||||
$this->assertFalse(isset($bundleOptions[1]));
|
||||
$this->assertEquals('simple2', $bundleOptions[0]['product_links'][0]['sku']);
|
||||
$this->assertEquals(2, $bundleOptions[0]['product_links'][0]['qty']);
|
||||
$this->assertEquals($simpleProduct['price'], $bundleOptions[0]['product_links'][0]['price']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Catalog/_files/products_new.php
|
||||
* @magentoApiDataFixture Magento/Catalog/_files/second_product_simple.php
|
||||
*/
|
||||
public function testUpdateFixedPriceBundleProductOptionSelectionPrice()
|
||||
{
|
||||
$optionPrice = 20;
|
||||
$bundleProduct = $this->createFixedPriceBundleProduct();
|
||||
$bundleProductOptions = $this->getBundleProductOptions($bundleProduct);
|
||||
|
||||
//replace current option with a new option
|
||||
$bundleProductOptions[0] = [
|
||||
'title' => 'new option',
|
||||
'required' => true,
|
||||
'type' => 'select',
|
||||
'product_links' => [
|
||||
[
|
||||
'sku' => 'simple2',
|
||||
'qty' => 2,
|
||||
"price" => $optionPrice,
|
||||
"price_type" => 1,
|
||||
"is_default" => false,
|
||||
],
|
||||
],
|
||||
];
|
||||
$this->setBundleProductOptions($bundleProduct, $bundleProductOptions);
|
||||
$this->saveProduct($bundleProduct);
|
||||
|
||||
$updatedProduct = $this->getProduct(self::BUNDLE_PRODUCT_ID);
|
||||
$bundleOptions = $this->getBundleProductOptions($updatedProduct);
|
||||
$this->assertEquals('simple2', $bundleOptions[0]['product_links'][0]['sku']);
|
||||
$this->assertEquals(2, $bundleOptions[0]['product_links'][0]['qty']);
|
||||
$this->assertEquals($optionPrice, $bundleOptions[0]['product_links'][0]['price']);
|
||||
}
|
||||
|
||||
#[
|
||||
DataFixture(StoreFixture::class, as: 'store2'),
|
||||
DataFixture(ProductFixture::class, ['price' => 10], 'p1'),
|
||||
DataFixture(ProductFixture::class, ['price' => 20], 'p2'),
|
||||
DataFixture(BundleOptionFixture::class, ['product_links' => ['$p1$']], 'opt1'),
|
||||
DataFixture(BundleOptionFixture::class, ['product_links' => ['$p2$']], 'opt2'),
|
||||
DataFixture(
|
||||
BundleProductFixture::class,
|
||||
['_options' => ['$opt1$', '$opt2$']],
|
||||
'bundle1'
|
||||
),
|
||||
]
|
||||
|
||||
public function testUpdateBundleProductOptionsTitleOnStoreView(): void
|
||||
{
|
||||
$this->cleanUpOnTearDown = false;
|
||||
$fixtures = DataFixtureStorageManager::getStorage();
|
||||
$product = $fixtures->get('bundle1');
|
||||
$store2 = $fixtures->get('store2');
|
||||
$data = $this->getProduct($product->getSku());
|
||||
$defaultOptions = $this->getBundleProductOptions($data);
|
||||
$store2Options = $defaultOptions;
|
||||
$store2Options[0]['title'] .= ' - custom';
|
||||
$store2Options[1]['title'] .= ' - custom';
|
||||
$this->setBundleProductOptions($data, $store2Options);
|
||||
$this->saveProduct($data, $store2->getCode());
|
||||
|
||||
// check that option titles are updated on store 2
|
||||
$data = $this->getProduct($product->getSku(), $store2->getCode());
|
||||
$options = $this->getBundleProductOptions($data);
|
||||
$this->assertEquals($store2Options[0]['title'], $options[0]['title']);
|
||||
$this->assertEquals($store2Options[1]['title'], $options[1]['title']);
|
||||
|
||||
// check that option titles have not changed on default store
|
||||
$data = $this->getProduct($product->getSku(), 'default');
|
||||
$options = $this->getBundleProductOptions($data);
|
||||
$this->assertEquals($defaultOptions[0]['title'], $options[0]['title']);
|
||||
$this->assertEquals($defaultOptions[1]['title'], $options[1]['title']);
|
||||
|
||||
// check that option titles have not changed in global scope
|
||||
$data = $this->getProduct($product->getSku(), 'all');
|
||||
$options = $this->getBundleProductOptions($data);
|
||||
$this->assertEquals($defaultOptions[0]['title'], $options[0]['title']);
|
||||
$this->assertEquals($defaultOptions[1]['title'], $options[1]['title']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the bundle_product_options custom attribute from product, null if the attribute is not set
|
||||
*
|
||||
* @param array $product
|
||||
* @return array|null
|
||||
*/
|
||||
protected function getBundleProductOptions($product)
|
||||
{
|
||||
if (isset($product[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]["bundle_product_options"])) {
|
||||
return $product[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]["bundle_product_options"];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the bundle_product_options custom attribute, replace existing attribute if exists
|
||||
*
|
||||
* @param array $product
|
||||
* @param array $bundleProductOptions
|
||||
*/
|
||||
protected function setBundleProductOptions(&$product, $bundleProductOptions)
|
||||
{
|
||||
$product["extension_attributes"]["bundle_product_options"] = $bundleProductOptions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create dynamic bundle product with one option
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function createDynamicBundleProduct()
|
||||
{
|
||||
$bundleProductOptions = [
|
||||
[
|
||||
"title" => "test option",
|
||||
"type" => "checkbox",
|
||||
"required" => 1,
|
||||
"product_links" => [
|
||||
[
|
||||
"sku" => 'simple',
|
||||
"qty" => 1,
|
||||
"is_default" => true,
|
||||
"price" => 10,
|
||||
"price_type" => 1,
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$uniqueId = self::BUNDLE_PRODUCT_ID;
|
||||
$product = [
|
||||
"sku" => $uniqueId,
|
||||
"name" => $uniqueId,
|
||||
"type_id" => "bundle",
|
||||
'attribute_set_id' => 4,
|
||||
"custom_attributes" => [
|
||||
"price_type" => [
|
||||
'attribute_code' => 'price_type',
|
||||
'value' => \Magento\Bundle\Model\Product\Price::PRICE_TYPE_DYNAMIC
|
||||
],
|
||||
"price_view" => [
|
||||
"attribute_code" => "price_view",
|
||||
"value" => "1",
|
||||
],
|
||||
],
|
||||
"extension_attributes" => [
|
||||
"bundle_product_options" => $bundleProductOptions,
|
||||
],
|
||||
];
|
||||
|
||||
$response = $this->createProduct($product);
|
||||
$this->assertTrue(
|
||||
isset($response[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]["bundle_product_options"])
|
||||
);
|
||||
$resultBundleProductOptions
|
||||
= $response[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]["bundle_product_options"];
|
||||
$this->assertTrue(isset($resultBundleProductOptions[0]["product_links"][0]["sku"]));
|
||||
$this->assertEquals('simple', $resultBundleProductOptions[0]["product_links"][0]["sku"]);
|
||||
$this->assertTrue(isset($response['custom_attributes']));
|
||||
$customAttributes = $this->convertCustomAttributes($response['custom_attributes']);
|
||||
$this->assertTrue(isset($customAttributes['price_type']));
|
||||
$this->assertEquals(\Magento\Bundle\Model\Product\Price::PRICE_TYPE_DYNAMIC, $customAttributes['price_type']);
|
||||
$this->assertTrue(isset($customAttributes['price_view']));
|
||||
$this->assertEquals(1, $customAttributes['price_view']);
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create fixed price bundle product with one option
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function createFixedPriceBundleProduct()
|
||||
{
|
||||
$bundleProductOptions = [
|
||||
[
|
||||
"title" => "test option",
|
||||
"type" => "checkbox",
|
||||
"required" => 1,
|
||||
"product_links" => [
|
||||
[
|
||||
"sku" => 'simple',
|
||||
"qty" => 1,
|
||||
"price" => 20,
|
||||
"price_type" => 1,
|
||||
"is_default" => true,
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$uniqueId = self::BUNDLE_PRODUCT_ID;
|
||||
$product = [
|
||||
"sku" => $uniqueId,
|
||||
"name" => $uniqueId,
|
||||
"type_id" => "bundle",
|
||||
"price" => 50,
|
||||
'attribute_set_id' => 4,
|
||||
"custom_attributes" => [
|
||||
"price_type" => [
|
||||
'attribute_code' => 'price_type',
|
||||
'value' => \Magento\Bundle\Model\Product\Price::PRICE_TYPE_FIXED
|
||||
],
|
||||
"price_view" => [
|
||||
"attribute_code" => "price_view",
|
||||
"value" => "1",
|
||||
],
|
||||
],
|
||||
"extension_attributes" => [
|
||||
"bundle_product_options" => $bundleProductOptions,
|
||||
],
|
||||
];
|
||||
|
||||
$response = $this->createProduct($product);
|
||||
$resultBundleProductOptions
|
||||
= $response[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]["bundle_product_options"];
|
||||
$this->assertEquals('simple', $resultBundleProductOptions[0]["product_links"][0]["sku"]);
|
||||
$this->assertTrue(isset($response['custom_attributes']));
|
||||
$customAttributes = $this->convertCustomAttributes($response['custom_attributes']);
|
||||
$this->assertTrue(isset($customAttributes['price_type']));
|
||||
$this->assertEquals(\Magento\Bundle\Model\Product\Price::PRICE_TYPE_FIXED, $customAttributes['price_type']);
|
||||
$this->assertTrue(isset($customAttributes['price_view']));
|
||||
$this->assertEquals(1, $customAttributes['price_view']);
|
||||
return $response;
|
||||
}
|
||||
|
||||
protected function convertCustomAttributes($customAttributes)
|
||||
{
|
||||
$convertedCustomAttribute = [];
|
||||
foreach ($customAttributes as $customAttribute) {
|
||||
$convertedCustomAttribute[$customAttribute['attribute_code']] = $customAttribute['value'];
|
||||
}
|
||||
return $convertedCustomAttribute;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get product
|
||||
*
|
||||
* @param string $productSku
|
||||
* @return array the product data
|
||||
*/
|
||||
protected function getProduct($productSku, ?string $storeCode = null)
|
||||
{
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH . '/' . $productSku,
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_NAME,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => self::SERVICE_NAME . 'Get',
|
||||
],
|
||||
];
|
||||
|
||||
return TESTS_WEB_API_ADAPTER === self::ADAPTER_SOAP
|
||||
? $this->_webApiCall($serviceInfo, ['sku' => $productSku], storeCode: $storeCode)
|
||||
: $this->_webApiCall($serviceInfo, storeCode: $storeCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create product
|
||||
*
|
||||
* @param array $product
|
||||
* @return array the created product data
|
||||
*/
|
||||
protected function createProduct($product)
|
||||
{
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH,
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_NAME,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => self::SERVICE_NAME . 'Save',
|
||||
],
|
||||
];
|
||||
$requestData = ['product' => $product];
|
||||
$response = $this->_webApiCall($serviceInfo, $requestData);
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a product by sku
|
||||
*
|
||||
* @param $productSku
|
||||
* @return bool
|
||||
*/
|
||||
protected function deleteProductBySku($productSku)
|
||||
{
|
||||
$resourcePath = self::RESOURCE_PATH . '/' . $productSku;
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => $resourcePath,
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_NAME,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => self::SERVICE_NAME . 'deleteById',
|
||||
],
|
||||
];
|
||||
$requestData = ["sku" => $productSku];
|
||||
$response = $this->_webApiCall($serviceInfo, $requestData);
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save product
|
||||
*
|
||||
* @param array $product
|
||||
* @param string|null $storeCode
|
||||
* @return array the created product data
|
||||
*/
|
||||
protected function saveProduct($product, ?string $storeCode = null)
|
||||
{
|
||||
if (isset($product['custom_attributes'])) {
|
||||
$count = count($product['custom_attributes']);
|
||||
for ($i=0; $i < $count; $i++) {
|
||||
if ($product['custom_attributes'][$i]['attribute_code'] == 'category_ids'
|
||||
&& !is_array($product['custom_attributes'][$i]['value'])
|
||||
) {
|
||||
$product['custom_attributes'][$i]['value'] = [""];
|
||||
}
|
||||
}
|
||||
}
|
||||
$resourcePath = self::RESOURCE_PATH . '/' . $product['sku'];
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => $resourcePath,
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT
|
||||
],
|
||||
'soap' => [
|
||||
'service' => self::SERVICE_NAME,
|
||||
'serviceVersion' => self::SERVICE_VERSION,
|
||||
'operation' => self::SERVICE_NAME . 'Save',
|
||||
],
|
||||
];
|
||||
$requestData = ['product' => $product];
|
||||
$response = $this->_webApiCall($serviceInfo, $requestData, storeCode: $storeCode);
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user