Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Products added to cart with REST API give total prices equal to zero #2991

Closed
degaray opened this issue Jan 13, 2016 · 57 comments
Closed

Products added to cart with REST API give total prices equal to zero #2991

degaray opened this issue Jan 13, 2016 · 57 comments
Labels
Component: Quote Fixed in 2.3.x The issue has been fixed in 2.3 release line Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Reproduced on 2.1.x The issue has been reproduced on latest 2.1 release Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release

Comments

@degaray
Copy link

degaray commented Jan 13, 2016

Preconditions (*)

  1. Magento 2.3.0(release), 2.3-develop, 2.2-develop

Steps to reproduce (*)

1.Execute:

endpoint="http://m2ce.conf/rest"
shippingEmail="[email protected]"

admin_token=$(curl -X POST "$endpoint/V1/integration/admin/token" \
--header "Content-Type: application/json" \
-d '{"username":"admin","password":"123123q"}') && echo $admin_token && admin_token=$(echo $admin_token | tr -d '"')

curl -X POST "$endpoint/V1/categories" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $admin_token" \
-d '{"category":{"parent_id":2,"name":"Category 1","is_active":true,"level":2,"path":"1/2/3","include_in_menu":true,"custom_attributes":[{"attribute_code":"is_anchor","value":"1"},{"attribute_code":"url_key","value":"category-1"},{"attribute_code":"url_path","value":"category-1"}]}}' | json_pp

curl -X POST "$endpoint/V1/categories" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $admin_token" \
-d '{"category":{"parent_id":2,"name":"Category 2","is_active":true,"level":2,"path":"1/2/4","include_in_menu":true,"custom_attributes":[{"attribute_code":"is_anchor","value":"1"},{"attribute_code":"url_key","value":"category-2"},{"attribute_code":"url_path","value":"category-1"}]}}' | json_pp

category_id="3"; productSKU="Simple Product SSM 1" && curl -X POST "$endpoint/V1/products" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $admin_token" \
-d '{"product":{"sku":"'"$productSKU"'","name":"'"$productSKU"'","attribute_set_id":4,"price":10,"status":1,"visibility":4,"type_id":"simple","created_at":"2018-01-01 00:00:01","updated_at":"2018-01-01 00:01:00","weight":1,"extension_attributes":{"category_links":[{"position":0,"category_id":"'"$category_id"'"}],"stock_item":{"qty":1000,"is_in_stock":true,"is_qty_decimal":false}}}}' | json_pp

category_id="3"; productSKU="Simple Product SSM 2" && curl -X POST "$endpoint/V1/products" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $admin_token" \
-d '{"product":{"sku":"'"$productSKU"'","name":"'"$productSKU"'","attribute_set_id":4,"price":10,"status":1,"visibility":4,"type_id":"simple","created_at":"2018-01-01 00:00:01","updated_at":"2018-01-01 00:01:00","weight":1,"extension_attributes":{"category_links":[{"position":0,"category_id":"'"$category_id"'"}],"stock_item":{"qty":1000,"is_in_stock":true,"is_qty_decimal":false}}}}' | json_pp

category_id="3"; productSKU="Simple Product SSM 3" && curl -X POST "$endpoint/V1/products" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $admin_token" \
-d '{"product":{"sku":"'"$productSKU"'","name":"'"$productSKU"'","attribute_set_id":4,"price":10,"status":1,"visibility":4,"type_id":"simple","created_at":"2018-01-01 00:00:01","updated_at":"2018-01-01 00:01:00","weight":1,"extension_attributes":{"category_links":[{"position":0,"category_id":"'"$category_id"'"}],"stock_item":{"qty":1000,"is_in_stock":true,"is_qty_decimal":false}}}}' | json_pp

category_id="4"; productSKU="Simple Product SSM 4" && curl -X POST "$endpoint/V1/products" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $admin_token" \
-d '{"product":{"sku":"'"$productSKU"'","name":"'"$productSKU"'","attribute_set_id":4,"price":10,"status":1,"visibility":4,"type_id":"simple","created_at":"2018-01-01 00:00:01","updated_at":"2018-01-01 00:01:00","weight":1,"extension_attributes":{"category_links":[{"position":0,"category_id":"'"$category_id"'"}],"stock_item":{"qty":1000,"is_in_stock":true,"is_qty_decimal":false}}}}' | json_pp

category_id="4"; productSKU="Simple Product SSM 5" && curl -X POST "$endpoint/V1/products" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $admin_token" \
-d '{"product":{"sku":"'"$productSKU"'","name":"'"$productSKU"'","attribute_set_id":4,"price":10,"status":1,"visibility":4,"type_id":"simple","created_at":"2018-01-01 00:00:01","updated_at":"2018-01-01 00:01:00","weight":1,"extension_attributes":{"category_links":[{"position":0,"category_id":"'"$category_id"'"}],"stock_item":{"qty":1000,"is_in_stock":true,"is_qty_decimal":false}}}}' | json_pp

curl -X POST "$endpoint/V1/categories" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $admin_token" \
-d '{"category":{"parent_id":2,"name":"Category 3","is_active":true,"level":2,"path":"1/2/4","include_in_menu":true,"custom_attributes":[{"attribute_code":"is_anchor","value":"1"},{"attribute_code":"url_key","value":"category-3"},{"attribute_code":"url_path","value":"category-3"}]}}' | json_pp

category_id="5"; productSKU="Simple Product SSM 5" && curl -X POST "$endpoint/V1/products" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $admin_token" \
-d '{"product":{"sku":"'"$productSKU"'","name":"'"$productSKU"'","attribute_set_id":4,"price":10,"status":1,"visibility":4,"type_id":"simple","created_at":"2018-01-01 00:00:01","updated_at":"2018-01-01 00:01:00","weight":1,"extension_attributes":{"category_links":[{"position":0,"category_id":"'"$category_id"'"}],"stock_item":{"qty":1000,"is_in_stock":true,"is_qty_decimal":false}}}}' | json_pp

curl -X POST $endpoint/V1/customers \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $admin_token" \
-d '{"customer":{"default_billing":false,"default_shipping":true,"email":"'"$shippingEmail"'","firstname":"Jim","lastname":"Earthworm","store_id":1,"website_id":1,"addresses":[{"id":1,"region":{"region_code":"CA","region":"California","region_id":12},"region_id":12,"country_id":"US","street":["6161 West Centinela Avenue"],"company":"Magento","telephone":"555-55-555-55","postcode":"90230","city":"Culver City","firstname":"Jim","lastname":"Earthworm"}],"disable_auto_group_change":0,"extension_attributes":{"is_subscribed":false}},"password":"MetaFrame1"}' | json_pp

customer_token=$(curl -X POST "$endpoint/V1/integration/customer/token" \
 -H "Content-Type: application/json" \
 -d '{"username":"'"$shippingEmail"'","password":"MetaFrame1"}') && echo $customer_token && customer_token=$(echo $customer_token | tr -d '"')

 cart_id=$(curl -X POST "$endpoint/V1/carts/mine" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $customer_token") && echo $cart_id && cart_id=$(echo $cart_id | tr -d '"')

curl -X GET "$endpoint/V1/customers/me" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $customer_token" | grep -i customer_id

curl -X GET "$endpoint/V1/carts/mine/items" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $customer_token"

curl -X POST "$endpoint/V1/carts/mine/items" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $customer_token" \
-d '{"cartItem":{"sku":"'"$productSKU"'","qty": 1,"name": "'"$productSKU"'","price":10,"product_type":"simple","quote_id":"'"$cart_id"'"}}' | json_pp

curl -X POST "$endpoint/V1/carts/mine/items" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $customer_token" \
-d '{"cartItem":{"sku":"'"$productSKU"'","qty": 1,"name": "'"$productSKU"'","price":10,"product_type":"simple","quote_id":"'"$cart_id"'"}}' | json_pp

Expected result (*)

  1. Price is present in both last two responses and is not equal to zero or is equal to real price whatever price was specified in the request payload.
  2. No side-effects on Storefront

Actual result (*)

  1. Fresh cart always has no price property. Submit an order and you will be able to reproduce again.
  2. Zero price in the Cart on Storefront
    report

Information from reporter

I have created a Product with a prize of $24 using the backend of Magento2. When I check it in the backent it is fine.
However, when I add one unit of it to an existent cart using the REST API I get a price of zero. For that I call [POST] /carts/mine/items with body:

{
    "cart_item": {
        "quote_id": "2",
        "sku": "MY_SKU",
        "qty": 1
    }
}

I get the following response:

{
  "item_id": 2,
  "sku": "MY_SKU",
  "qty": 2,
  "name": "My Product",
  "price": 0,
  "product_type": "virtual",
  "quote_id": "2"
}

Also when I call [GET] /products I get the right price in the response, this is the response I get:

{
  "items": [
    {
      "id": 1,
      "sku": "MY_SKU",
      "name": "My Product",
      "attribute_set_id": 9,
      "price": 24,
      "status": 1,
      "visibility": 4,
      "type_id": "virtual",
      "created_at": "2016-01-11 19:17:10",
      "updated_at": "2016-01-13 17:49:11",
      "product_links": [],
      "options": [],
      "tier_prices": [],
      "custom_attributes": [
        {
          "attribute_code": "meta_title",
          "value": "My Product"
        },
        {
          "attribute_code": "meta_keyword",
          "value": "My Product"
        },
        {
          "attribute_code": "meta_description",
          "value": "My Product"
        },
        {
          "attribute_code": "options_container",
          "value": "container2"
        },
        {
          "attribute_code": "required_options",
          "value": "0"
        },
        {
          "attribute_code": "has_options",
          "value": "0"
        },
        {
          "attribute_code": "url_key",
          "value": "my-product"
        },
        {
          "attribute_code": "tax_class_id",
          "value": "0"
        },
        {
          "attribute_code": "custom_time",
          "value": "2016-01-11 00:00:00"
        }
      ]
    }
  ],
  "search_criteria": {
    "filter_groups": [
      {
        "filters": [
          {
            "field": "custom_time",
            "value": "2015-11-19",
            "condition_type": "gt"
          }
        ]
      }
    ]
  },
  "total_count": 1
}

Any guesses why this might be?

@degaray
Copy link
Author

degaray commented Jan 24, 2016

The problem comes here: Magento\Quote\Model\Quote\TotalsCollector::collect because all totals are set to zero and then all totals are calculated per address set. Since no address is set, then no totals are calculated, and as result, it does not calculate any total.

@ishakhsuvarov ishakhsuvarov self-assigned this Jan 25, 2016
@ishakhsuvarov
Copy link
Contributor

@degaray Could you please provide more detailed steps to reproduce the issue? How did you create your quote and customer?
A set of empty addresses should be created together with the quote, allowing normal totals calculation for this case.

@degaray
Copy link
Author

degaray commented Jan 25, 2016

1.- I create a new instance of Magento2 using git clone --recursive https://github.com/rgranadino/mage2_vagrant.git to get a fresh install. Then I follow all the steps to install it using composer, etc.
2.- I oath login as admin using /integration/admin/token:

    {
        "username": "admin",
        "password": "password123"
    }

I get an auth token.

3.- I create a product calling: [POST] /products:

    {
      "product": {
        "sku": "MY_SKU",
        "name": "My Product",
        "attributeSetId": "4",
        "price": 20,
        "status": 1,
        "visibility": 4,
        "typeId": "virtual",
        "weight": 0,
        "extensionAttributes": {
          "stockItem": {
            "stockId": 1,
            "qty": 20,
            "isInStock": true,
            "isQtyDecimal": false,
            "useConfigMinQty": true,
            "minQty": 0,
            "useConfigMinSaleQty": 0,
            "minSaleQty": 0,
            "useConfigMaxSaleQty": true,
            "maxSaleQty": 0,
            "useConfigBackorders": false,
            "backorders": 0,
            "useConfigNotifyStockQty": true,
            "notifyStockQty": 20,
            "useConfigQtyIncrements": false,
            "qtyIncrements": 0,
            "useConfigEnableQtyInc": false,
            "enableQtyIncrements": false,
            "useConfigManageStock": true,
            "manageStock": true,
            "lowStockDate": "string",
            "isDecimalDivided": true,
            "stockStatusChangedAuto": 0,
            "extensionAttributes": {}
          }
        },
        "options": [],
        "tierPrices": [],
        "customAttributes": [
        ]
      },
      "saveOptions": true
    }

4.- I create a user using [POST] /customers:

    {
        "customer": {
            "email": "[email protected]",
            "firstname": "My Name",
            "lastname": "Last Name",
            "storeId": 1,
            "websiteId": 1,
            "extension_attributes": {
            },
            "addresses": [
                {
                    "country_id": "MX",
                    "street": [
                        "Some address 23"
                    ],
                    "telephone": "5555555558",
                    "postcode": "10200",
                    "city": "México",
                    "firstname": "My Name",
                    "lastname": "Last Name",
                    "default_shipping": true,
                    "default_billing": true
                }
            ]
        },
        "password": "{{customer_password}}"
    }

4.1- I login using /integration/customer/token and get an oath token.

5.- I create a cart using: [POST] /carts/mine with a blank body

6.- I add an item to the cart using: [POST] /carts/mine/items

    {
        "cart_item": {
            "quote_id": "1",
            "sku": "MY_SKU",
            "qty": 1
        }
    }

7.- I finally call [GET] /carts/mine/payment-information
and I get this:

    {
      "payment_methods": [
        {
          "code": "checkmo",
          "title": "Check / Money order"
        },
        {
          "code": "free",
          "title": "No Payment Information Required"
        }
      ],
      "totals": {
        "grand_total": 0,
        "weee_tax_applied_amount": null,
        "base_currency_code": "USD",
        "quote_currency_code": "USD",
        "items_qty": 1,
        "items": [
          {
            "item_id": 1,
            "price": 0,
            "base_price": 0,
            "qty": 1,
            "row_total": 0,
            "base_row_total": 0,
            "row_total_with_discount": 0,
            "tax_amount": 0,
            "base_tax_amount": 0,
            "tax_percent": 0,
            "discount_amount": 0,
            "base_discount_amount": 0,
            "discount_percent": 0,
            "options": "[]",
            "weee_tax_applied_amount": null,
            "weee_tax_applied": null,
            "name": "My Product"
          }
        ],
        "total_segments": [
          {
            "code": "subtotal",
            "title": "Subtotal",
            "value": null
          },
          {
            "code": "shipping",
            "title": "Shipping & Handling",
            "value": null
          },
          {
            "code": "grand_total",
            "title": "Grand Total",
            "value": null,
            "area": "footer"
          }
        ]
      }
    }

@degaray
Copy link
Author

degaray commented Jan 25, 2016

You can find further reference to this issue in this question:

http://magento.stackexchange.com/questions/97373/items-added-to-cart-with-no-price-on-magento2

@degaray degaray changed the title Products added to cart with REST API give total prizes equal to zero Products added to cart with REST API give total prices equal to zero Jan 25, 2016
@sdzhepa
Copy link
Contributor

sdzhepa commented Jan 29, 2016

@degaray Thank you for reporting this issue. We have created an internal ticket MAGETWO-48570 to fix the problem.

@ishakhsuvarov ishakhsuvarov added Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development bug report labels Jan 29, 2016
magento-team pushed a commit that referenced this issue Apr 4, 2016
@ishakhsuvarov
Copy link
Contributor

@degaray Closing this ticket, since the fix is already delivered with the linked commits. Please feel free to reopen if you have any additional questions regarding this.

@degaray
Copy link
Author

degaray commented May 16, 2016

This issue is not fixed! I have tried 2.0.5 and it did not work. I added the extra line as in bdd8d3b and the bug still appears. This is a VERY serious bug. As I cannot collect the value of the cart, thus I cannot get the list of payment methods, use coupons, or update the cart information from the server.

@manuelDistefano
Copy link

Same here. I have tried 2.0.7 and it did not work.

@manuelDistefano
Copy link

manuelDistefano commented Jun 20, 2016

Update. You can user rest/V1/carts/mine/billing-address to add address to cart. Then add item to cart so you can have item with the correct price.

POST /index.php/rest/V1/carts/mine/billing-address HTTP/1.1
Host: 127.0.0.41:8082
Content-Type: application/json
Accept: application/json
Accept-Language: it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4
Authorization: Bearer 3ewcbeoxh1x52b9wf5aapaib0qm8hrbk
Cache-Control: no-cache
Postman-Token: 5ad87a49-68ec-6b0a-1002-88ed0f1bbbac

{
  "address": {
    "region": "Roma",
    "regionId": 0,
    "regionCode": "Roma",
    "countryId": "IT",
    "street": [
      "Via Toscana 16"
    ],
    "company": "Manuel Di Stefano",
    "telephone": "3333333",
    "fax": "33333333",
    "postcode": "00013",
    "city": "Fonte Nuova",
    "firstname": "First Name",
    "lastname": "Last name",
    "middlename": "string",
    "customerId": 21,
    "email": "string",
    "sameAsBilling": 1,
    "saveInAddressBook": 0
  },
  "useForShipping": true
}

@degaray
Copy link
Author

degaray commented Jun 20, 2016

Yes @manuelDistefano however, don't you think that is a bug? Specially if you are selling virtual products?

@manuelDistefano
Copy link

manuelDistefano commented Jun 20, 2016

Yes @degaray, it's a problem for sure, and i'm selling virtual products too.

But i can use this "workaround" until they fix it.

@RUjmiak
Copy link

RUjmiak commented Jun 24, 2016

@ishakhsuvarov Can you reopen ticket? Bug is not fixed...

@qwerty7869
Copy link

@ishakhsuvarov Please tell me how to extend model Repository.php from core file to my project. I tried extending the model but than add to product api is giving error as below.

message": "Source class "%1" for "%2" generation does not exist.",
"parameters": [
"\project\ExtendQuote\Model\Quote\ItemInterface",
"project\ExtendQuote\Model\Quote\Item\Repository"

@tejashp77
Copy link

Any one has a solution of this issue? i am using 2.1.0 and 0 price issue which degaray mentioned above is still there

@NisaKH
Copy link

NisaKH commented Aug 16, 2016

@tejashp77
For this issue, You can use an observer to auto add an empty address to cart.

use this event >> sales_quote_save_before
and use the code below

public function execute( Observer $observer ) {
    $quote = $observer->getEvent()->getQuote();
    $this->logger->debug("Cart Observer is trigged.");

    if ($quote->hasItems() && $quote->getSubtotal() == 0) {
        $this->logger->debug("Set address to cart");
        $quote
            ->setShippingAddress($this->getEmptyAddress())
            ->setBillingAddress($this->getEmptyAddress());
    }
    $shippingAddress
        ->setCollectShippingRates(true)
        ->collectShippingRates();

}

private function getEmptyAddress() {
    $address = $this->addressFactory->create();
    $address->setRegionId(1);
    $address->setRegion(" ");
    $address->setRegionCode(" ");
    $address->setCountryId("EN");
    $address->setStreet([" "]);
    $address->setTelephone(" ");
    $address->setPostcode(" ");
    $address->setCity(" ");
    $address->setFirstname(" ");
    $address->setLastname(" ");
    return $address;
}

Please note that the region id must not be null

** For virtual products, you don't need to set shipping address

@tejashp77
Copy link

from API call it not goes to 'sales_quote_save_before' event. i have tried by put events.xml in etc folder and etc/frontend both.

@NisaKH
Copy link

NisaKH commented Aug 16, 2016

@tejashp77

just put it in etc/events.xml should be enough.

This is how i put the event.
<event name="sales_quote_save_before"> <observer name="cart_save" instance="VendorName\ModuleName\Observer\CartObserver" shared="false" /> </event>

It should be tricked when your customer add a product to cart. please check again :)

@SerhiyShkolyarenko
Copy link
Contributor

Reopened the ticket due to several reports of reproducing the issue.

@ulftietze
Copy link
Member

Hey @dmanners, @benmarks,

we talked about this at the MageUC :)
Thanks for listen to me here!

Greetz,
Ulf

@cyberkiller6
Copy link

Hey @dmanners, @benmarks,

we talked about this at the MageUC :)
Thanks for listen to me here!

Greetz,
Ulf

@ulftietze
Have you found any solutions to this problem?

@ulftietze
Copy link
Member

ulftietze commented Nov 2, 2018

@cyberkiller6
nah not really, found a workaround which may breaks tons of stuff :D
You may disable the plugin which is responsible to delete the address from quote:

<type name="Magento\Quote\Model\Quote">
        <plugin name="clear_addresses_after_product_delete" disabled="true" />
</type>

Problem here is, that this does not fix the issue with the initialize adding a product directly after creating the quote.

@ulftietze
Copy link
Member

Hey @benmarks @dmanners ,

here i am to annoy you :D you said to me i should do that if you dont react ^^
May @ishakhsuvarov can tell more here? :D

Greetz,
Ulf

@okorshenko
Copy link
Contributor

Hi @ulftietze
Thank you for raising the issue again. We will double-check it

@okorshenko okorshenko reopened this Nov 5, 2018
@ulftietze
Copy link
Member

ulftietze commented Nov 5, 2018

Hey Guys,

i've found a temporary workaround, which works at least in my scope to prevent this behavior.
This should definitly not be the solution for the core but may this help others until Magento fix this the correct way ^^

In\Magento\Quote\Model\Quote\Item\Repositoryon line 87, add $quote->getShippingAddress(); to make sure, a shipping address exists.
Your function should look like this:

/**
  * {@inheritdoc}
  */
 public function save(\Magento\Quote\Api\Data\CartItemInterface $cartItem)
 {
     /** @var \Magento\Quote\Model\Quote $quote */
     $cartId = $cartItem->getQuoteId();
     $quote = $this->quoteRepository->getActive($cartId);
     $quote->getShippingAddress();

     $quoteItems = $quote->getItems();
     $quoteItems[] = $cartItem;
     $quote->setItems($quoteItems);
     $this->quoteRepository->save($quote);
     $quote->collectTotals();
     return $quote->getLastAddedItem();
 }

@okorshenko thanks for reopening this 👍

have a nice week,
Ulf

@hoangtuan21193
Copy link

hoangtuan21193 commented Nov 7, 2018

@ulftietze

Thank you for your suggestion but I can not make it work for me. Would you mind if I can see how you add item to cart.

I want to add shopping bag item to cart when customer click [Proceed to Checkout] in Cart page. Then redirect to confirm page.

This is my code, but it's always redirect to cart page again, I don't want that. If I try to not redirect to cart, the price will be 0.

public function addShoppingBag()
    {
        $shoppingBagSku = $this->helper->getShoppingBagSku();
        $shoppingBagProduct = $this->productRepository->get($shoppingBagSku);

        $params = array(
            'product' => $shoppingBagProduct->getId(),
            'qty' => 1
        );

        $request = new \Magento\Framework\DataObject();
        $request->setData($params);

        $this->cart->addProduct($shoppingBagProduct, $request);
        $this->cart->save();
        $this->cart->getQuote()->setTotalsCollectedFlag(false)->collectTotals()->save();

    }

@ulftietze
Copy link
Member

Hey @hoangtuan21193,

i think this is the wrong point to discuss about custom errors here.
The right point to place this would probably be stackoverflow. If we discuss such custom solution in here, we can't talk about the real problem and the solution in Magento Core about this.

Anyway, i'm not like a bad person :D If you take a look at the last fix in the core, also included in this issue. Dunno if this works, but make sure that before you save the quote, you call this function:
$quote->getShippingAddress()->setCollectShippingRates(true);

If you have more problems on this, you may create an stackoverflow question and send me the link ^^

Have a nice day,
Ulf

@hoangtuan21193
Copy link

@ulftietze

Thank you for your response, I tried your suggestion but no luck.
I created a new question in stackoverflow, hope you can understand my problem.

https://stackoverflow.com/questions/53187770/when-add-new-product-programmatically-to-exist-cart-in-magento-2-product-price

Have a nice day too! 💯

@TomashKhamlai TomashKhamlai self-assigned this Nov 13, 2018
@magento-engcom-team
Copy link
Contributor

magento-engcom-team commented Nov 13, 2018

Hi @TomashKhamlai. Thank you for working on this issue.
Looks like this issue is already verified and confirmed. But if your want to validate it one more time, please, go though the following instruction:

  • 1. Add/Edit Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • 2. Verify that the issue is reproducible on 2.3-develop branch

    Details- Add the comment @magento-engcom-team give me 2.3-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!

  • 3. Verify that the issue is reproducible on 2.2-develop branch.

    Details- Add the comment @magento-engcom-team give me 2.2-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.2-develop branch, please add the label Reproduced on 2.2.x

  • 4. If the issue is not relevant or is not reproducible any more, feel free to close it.

@TomashKhamlai TomashKhamlai added Component: Quote Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed and removed bug report Fixed in 2.3.x The issue has been fixed in 2.3 release line Fixed in 2.2.x The issue has been fixed in 2.2 release line Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed labels Nov 13, 2018
@magento-engcom-team magento-engcom-team removed the Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed label Nov 13, 2018
@magento-engcom-team
Copy link
Contributor

magento-engcom-team commented Nov 13, 2018

@TomashKhamlai Thank you for verifying the issue.

Unfortunately, not enough information was provided to created internal ticket. Please consider adding the following:

Once all required information is added, please add label "Issue: Confirmed" again.
Thanks!

@magento-engcom-team magento-engcom-team added Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed and removed Issue: Format is not valid Gate 1 Failed. Automatic verification of issue format is failed labels Nov 13, 2018
@TomashKhamlai TomashKhamlai added the Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed label Nov 13, 2018
@magento-engcom-team
Copy link
Contributor

@TomashKhamlai Thank you for verifying the issue. Based on the provided information internal tickets MAGETWO-96319, MAGETWO-96320 were created

@TomashKhamlai TomashKhamlai removed their assignment Jan 4, 2019
@hatimeria-artur-jewula
Copy link
Contributor

hatimeria-artur-jewula commented Jan 24, 2019

I believe the main problem with this is in the Magento\Quote\Model\Quote::setShippingAddress() method. When removing product from cart, both billing and shipping addresses that are currently added are marked to deletion. Then save handler create a new empty billing address and shipping assignment creates a new shipping address. New billing address gets properly saved but shipping address goes through the following code:

$old = $this->getShippingAddress();
if (!empty($old)) {
    $old->addData($address->getData());
} else {
    $this->addAddress($address->setAddressType(Address::TYPE_SHIPPING));
}

$address is the object with shipping address from quote load and $old is the new shipping address that should be saved. Since $address is marked to deletion in the resource model it gets deleted. New address won't be saved because due to $old->addData($address->getData()); it will have id of the old address and resource model will attempt to do update on already removed column.

My suggestion is to add check there like this:

$old = $this->getShippingAddress();
if (!empty($old)) {
    $old->addData($address->getData());
    if ($address->isDeleted()) {
        $old->setId(null);
    }
} else {
    $this->addAddress($address->setAddressType(Address::TYPE_SHIPPING));
}

or always reset id field

@magento-engcom-team
Copy link
Contributor

Hi @degaray.

Thank you for your report and collaboration!

The issue was fixed by Magento team.

The fix will be available with the upcoming 2.3.3 release.

@magento-engcom-team magento-engcom-team added the Fixed in 2.3.x The issue has been fixed in 2.3 release line label Jun 19, 2019
@nagrgkgc
Copy link

@magento-team
possible to provide fixes ? will have to apply fixes in old version of magento.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Quote Fixed in 2.3.x The issue has been fixed in 2.3 release line Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Reproduced on 2.1.x The issue has been reproduced on latest 2.1 release Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release
Projects
None yet
Development

No branches or pull requests