Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProductRepositoryMultiCurrencyTest.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Catalog\Api;
7 
9 
11 {
12  const PRODUCT_SERVICE_NAME = 'catalogProductRepositoryV1';
13  const WEBSITES_SERVICE_NAME = 'storeWebsiteRepositoryV1';
14  const WEBSITE_LINK_SERVICE_NAME = 'catalogProductWebsiteLinkRepositoryV1';
15  const SERVICE_VERSION = 'V1';
16  const PRODUCTS_RESOURCE_PATH = '/V1/products';
17  const WEBSITES_RESOURCE_PATH = '/V1/store/websites';
18 
23  public function testGet()
24  {
25  $sku = 'simple';
26  $this->assignProductToWebsite($sku, $this->getWebsiteId('test'));
27  $product = $this->getProduct($sku);
28  $this->assertEquals(10, $product['price']);
29 
30  $product['price'] = 20;
31  $this->saveProduct($product, 'fixture_second_store');
32 
33  $product = $this->getProduct($sku, 'fixture_third_store');
34  $this->assertEquals(20, $product['price']);
35  }
36 
37  private function saveProduct($product, $storeCode = null)
38  {
39  $serviceInfo = [
40  'rest' => [
41  'resourcePath' => '/V1/products/' . $product['sku'],
43  ],
44  'soap' => [
45  'service' => self::PRODUCT_SERVICE_NAME,
46  'serviceVersion' => self::SERVICE_VERSION,
47  'operation' => self::PRODUCT_SERVICE_NAME . 'Save'
48  ]
49  ];
50 
51  $requestData = ['product' => $product];
52  return $this->_webApiCall($serviceInfo, $requestData, null, $storeCode);
53  }
54 
55  private function getProduct($sku, $storeCode = null)
56  {
57  $serviceInfo = [
58  'rest' => [
59  'resourcePath' => self::PRODUCTS_RESOURCE_PATH . '/' . $sku,
61  ],
62  'soap' => [
63  'service' => self::PRODUCT_SERVICE_NAME,
64  'serviceVersion' => self::SERVICE_VERSION,
65  'operation' => self::PRODUCT_SERVICE_NAME . 'get'
66  ]
67  ];
68 
69  $requestData = ['sku' => $sku];
70  return $this->_webApiCall($serviceInfo, $requestData, null, $storeCode);
71  }
72 
73  private function getWebsiteId($websiteCode)
74  {
75  $serviceInfo = [
76  'rest' => [
77  'resourcePath' => self::WEBSITES_RESOURCE_PATH,
79  ],
80  'soap' => [
81  'service' => self::WEBSITES_SERVICE_NAME,
82  'serviceVersion' => self::SERVICE_VERSION,
83  'operation' => self::WEBSITES_SERVICE_NAME . 'GetList'
84  ]
85  ];
86 
87  $response = $this->_webApiCall($serviceInfo);
88  $websiteId = null;
89  foreach ($response as $website) {
90  if ($website['code'] == $websiteCode) {
91  $websiteId = $website['id'];
92  }
93  }
94  $this->assertNotNull($websiteId);
95  return $websiteId;
96  }
97 
98  private function assignProductToWebsite($sku, $websiteId)
99  {
100  $serviceInfo = [
101  'rest' => [
102  'resourcePath' => '/V1/products/' . $sku . '/websites',
104  ],
105  'soap' => [
106  'service' => self::WEBSITE_LINK_SERVICE_NAME,
107  'serviceVersion' => self::SERVICE_VERSION,
108  'operation' => self::WEBSITE_LINK_SERVICE_NAME . 'save'
109  ]
110  ];
111 
112  $requestData = [
113  "productWebsiteLink" => [
114  "websiteId" => $websiteId,
115  "sku" => $sku
116  ]
117  ];
118  $this->assertTrue($this->_webApiCall($serviceInfo, $requestData));
119  }
120 }
$response
Definition: 404.php:11
_webApiCall( $serviceInfo, $arguments=[], $webApiAdapterCode=null, $storeCode=null, $integration=null)
$storeCode
Definition: indexer.php:15
if(!isset($_GET['website_code'])) $websiteCode
Definition: website.php:11