Magento 2.3 получает max_sales_qty при добавлении товара в корзину с помощью API клиента - PullRequest
0 голосов
/ 14 октября 2019

Я добавил товары в корзину. Но когда один товар превышает instock_qty, он не добавляется в корзину. Поэтому я добавлю максимально доступное кол-во в корзину. Как получить максимальное кол-во до добавления товара в корзину?

Вот мой запрос:

<code>    <?php

    $userData = array("username" => "email@customer.com", "password" => "CUSTOMERPASSWORD");
    $ch = curl_init("https://www.mymagentostore.com/index.php/rest/V1/integration/customer/token");
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($userData));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Lenght: " . strlen(json_encode($userData))));

    $token = curl_exec($ch);

    $ch = curl_init("https://www.mymagentostore.com/index.php/rest/V1/carts/mine");
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));

    $quoteId = curl_exec($ch);


    $cartItems = array(
    array('sku' => '1234-6005','qty' => '800'), // I have just 450 in stock.
    array('sku' => '1234-4005','qty' => '2'),
    array('sku' => '1234-6003','qty' => '11'),
    array('sku' => '1234-4003','qty' => '1')
    );


    foreach ($cartItems as $cartItem) {
        $cartItemArray = array("cartItem" => array('sku' => $cartItem["sku"],'qty' => $cartItem["qty"], 'quote_id' => $quoteId));
        $ch = curl_init("https://www.mymagentostore.com/index.php/rest/V1/carts/mine/items");
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($cartItemArray));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));

        curl_exec($ch);
    }


    $ch = curl_init("https://www.mymagentostore.com/index.php/rest/V1/carts/mine");
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));

    $result = curl_exec($ch);

    $result = json_decode($result, 1);
    echo '<pre>';
    var_dump($result);
    echo '
';
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...