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

Adjustments on line items when first added to cart via ajax have null line item IDs #1014

Closed
billythekid opened this issue Sep 23, 2019 · 7 comments
Assignees
Labels
Milestone

Comments

@billythekid
Copy link
Contributor

Description

When an item is first added to a cart via ajax the adjustments snapshots in the response do not include the line item's ID. The lineItem returned in the response does have an ID at this point though.

Steps to reproduce

  1. Set a per-item adjustment for a product
  2. Add this product to the cart via an ajax request
  3. Inspect the response cart object, its lineItems and adjustments.
  4. Update the cart again, making no changes, see that the adjustment now has a line item ID

Additional info

  • Craft version: 3.3.4.1
  • PHP version: 7.3.7
  • Database driver & version: MySQL 8.0.16
  • Plugins & versions: Craft Commerce 2.1.13
@nfourtythree
Copy link
Contributor

Hi @billythekid

I am struggling to replicate this or potentially spot the issue you are having. Could you please do me a diff of what you are seeing so I can try and track down the issue you are coming up against.

Thanks.

@nfourtythree nfourtythree added the ℹ️ status: need more info When waiting for user to supply database or more information. label Sep 24, 2019
@billythekid
Copy link
Contributor Author

billythekid commented Sep 24, 2019 via email

@lukeholder
Copy link
Member

@billythekid are you setting the line item on the adjustment model in your custom adjuster?

$adjustment->setLineItem($item);

@billythekid
Copy link
Contributor Author

Hey @nfourtythree here's the JSON response when I first add the item to cart with the 'discount' adjustment. I guess the point of interest is at the bottom but I didn't want to truncate anything here so apologies for the long dump, the discount adjustment snapshot.

{
  "JSON":{
    "success":true,
    "cart":{
      "id":"22096",
      "number":"ba3914f16a5f7918f3aadbd75e963fc7",
      "couponCode":null,
      "itemTotal":720,
      "itemSubtotal":2400,
      "totalPaid":0,
      "email":"billythekid@github.com",
      "isCompleted":false,
      "dateOrdered":null,
      "datePaid":null,
      "currency":"GBP",
      "paymentCurrency":"GBP",
      "lastIp":"127.0.0.1",
      "message":null,
      "returnUrl":null,
      "orderStatusId":null,
      "orderLanguage":"en-GB",
      "shippingMethod":"freeShipping",
      "shippingMethodId":"1",
      "paymentMethodId":"2",
      "gatewayId":"2",
      "paymentSourceId":null,
      "customerId":1,
      "isPaid":false,
      "paidStatus":"unpaid",
      "totalQty":1,
      "pdfUrl":"https://mc.test/index.php?p=actions/commerce/downloads/pdf&number=ba3914f16a5f7918f3aadbd75e963fc7&option=ajax",
      "isEmpty":false,
      "totalWeight":100,
      "total":720,
      "totalPrice":720,
      "availableShippingMethods":{
        "freeShipping":{
          "id":"1",
          "name":"Free UK Shipping, we offer free delivery on all UK orders",
          "handle":"freeShipping",
          "enabled":"1",
          "isLite":null,
          "price":0
        }
      },
      "shippingAddressId":"752",
      "shippingAddress":{
        "id":"752",
        "isStoreLocation":false,
        "attention":null,
        "title":"Mr",
        "firstName":"Billy",
        "lastName":"Fagan",
        "address1":"7 Carden Place",
        "address2":"",
        "city":"Aberdeen",
        "zipCode":"AB10 1PP",
        "phone":"",
        "alternativePhone":null,
        "businessName":"",
        "businessTaxId":null,
        "businessId":null,
        "stateName":null,
        "countryId":"77",
        "stateId":null,
        "fullName":"Billy Fagan",
        "countryText":"United Kingdom",
        "stateText":"",
        "stateValue":"",
        "abbreviationText":""
      },
      "billingAddressId":"752",
      "billingAddress":{
        "id":"752",
        "isStoreLocation":false,
        "attention":null,
        "title":"Mr",
        "firstName":"Billy",
        "lastName":"Fagan",
        "address1":"7 Carden Place",
        "address2":"",
        "city":"Aberdeen",
        "zipCode":"AB10 1PP",
        "phone":"",
        "alternativePhone":null,
        "businessName":"",
        "businessTaxId":null,
        "businessId":null,
        "stateName":null,
        "countryId":"77",
        "stateId":null,
        "fullName":"Billy Fagan",
        "countryText":"United Kingdom",
        "stateText":"",
        "stateValue":"",
        "abbreviationText":""
      },
      "totalTax":0,
      "totalTaxIncluded":120,
      "totalShippingCost":0,
      "totalDiscount":-1680,
      "lineItems":{
        "1581":{
          "id":1581,
          "price":2400,
          "saleAmount":0,
          "salePrice":2400,
          "qty":1,
          "weight":100,
          "length":0,
          "height":0,
          "width":0,
          "total":720,
          "snapshot":{
            "productId":"8640",
            "isDefault":"1",
            "sku":"1108507",
            "price":2400,
            "sortOrder":"1",
            "width":null,
            "height":null,
            "length":null,
            "weight":"100.0000",
            "stock":"1",
            "hasUnlimitedStock":"0",
            "minQty":"1",
            "maxQty":null,
            "deletedWithProduct":false,
            "id":"8641",
            "tempId":null,
            "draftId":null,
            "revisionId":null,
            "uid":"906fad82-ec67-4672-a3a7-b9d8b1648132",
            "fieldLayoutId":null,
            "contentId":"8476",
            "enabled":"1",
            "archived":"0",
            "siteId":"1",
            "enabledForSite":"1",
            "title":"18ct white gold three row diamond ring",
            "slug":null,
            "uri":null,
            "dateCreated":"2018-10-16T19:22:14+01:00",
            "dateUpdated":"2019-08-31T17:24:45+01:00",
            "dateDeleted":null,
            "trashed":false,
            "propagateAll":false,
            "newSiteIds":[

            ],
            "resaving":false,
            "duplicateOf":null,
            "previewing":false,
            "hardDelete":false,
            "ref":null,
            "status":"enabled",
            "structureId":null,
            "url":"https://mc.test/1108507-18ct-white-gold-three-row-diamond-ring?variant=8641",
            "isAvailable":true,
            "isPromotable":true,
            "shippingCategoryId":1,
            "taxCategoryId":1,
            "onSale":false,
            "cpEditUrl":"#",
            "product":{
              "postDate":"2018-11-02T00:00:00+00:00",
              "expiryDate":null,
              "typeId":"2",
              "taxCategoryId":"1",
              "shippingCategoryId":"1",
              "promotable":"1",
              "freeShipping":"0",
              "enabled":"1",
              "availableForPurchase":"1",
              "defaultVariantId":"8641",
              "defaultSku":"1108507",
              "defaultPrice":"2400.0000",
              "defaultHeight":"0.0000",
              "defaultLength":"0.0000",
              "defaultWidth":"0.0000",
              "defaultWeight":"100.0000",
              "taxCategory":null,
              "name":null,
              "id":"8640",
              "tempId":null,
              "draftId":null,
              "revisionId":null,
              "uid":"c1927a94-beb4-4f27-b4df-3c2dc5bd42ef",
              "fieldLayoutId":"15",
              "contentId":"8475",
              "archived":"0",
              "siteId":"1",
              "enabledForSite":"1",
              "title":"18ct white gold three row diamond ring",
              "slug":"1108507-18ct-white-gold-three-row-diamond-ring",
              "uri":"1108507-18ct-white-gold-three-row-diamond-ring",
              "dateCreated":"2018-10-16T19:22:13+01:00",
              "dateUpdated":"2019-08-31T17:24:45+01:00",
              "dateDeleted":null,
              "trashed":false,
              "propagateAll":false,
              "newSiteIds":[

              ],
              "resaving":false,
              "duplicateOf":null,
              "previewing":false,
              "hardDelete":false,
              "ref":null,
              "status":"live",
              "structureId":null,
              "url":"https://mc.test/1108507-18ct-white-gold-three-row-diamond-ring"
            },
            "description":"18ct white gold three row diamond ring",
            "purchasableId":"8641",
            "options":[

            ],
            "sales":[

            ]
          },
          "note":"",
          "orderId":"22096",
          "purchasableId":"8641",
          "taxCategoryId":1,
          "shippingCategoryId":1,
          "onSale":false,
          "options":[

          ],
          "optionsSignature":"d751713988987e9331980363e24189ce",
          "subtotal":2400
        }
      },
      "totalLineItems":1,
      "adjustments":{
        "discount":[
          {
            "id":15634,
            "type":"discount",
            "name":"My Client Sale",
            "description":"18ct white gold three row diamond ring is in the My Client sale.",
            "amount":-1680,
            "sourceSnapshot":{
              "description":"18ct white gold three row diamond ring is in the My Client sale.",
              "lineItemId":null
            },
            "orderId":"22096"
          }
        ],
        "tax":[
          {
            "id":15635,
            "type":"tax",
            "name":"UK/EU VAT",
            "description":"20% inc",
            "amount":120,
            "sourceSnapshot":{
              "id":"2",
              "name":"UK/EU VAT",
              "rate":"0.2000000000",
              "include":"1",
              "isVat":"0",
              "taxable":"price",
              "taxCategoryId":"1",
              "isLite":null,
              "taxZoneId":"4"
            },
            "orderId":"22096"
          }
        ]
      },
      "totalAdjustments":2
    }
  },
  "Response payload":{
    "EDITOR_CONFIG":{
      "text":"{\"success\":true,\"cart\":{\"id\":\"22096\",\"number\":\"ba3914f16a5f7918f3aadbd75e963fc7\",\"couponCode\":null,\"itemTotal\":720,\"itemSubtotal\":2400,\"totalPaid\":0,\"email\":\"billythekid@github.com\",\"isCompleted\":false,\"dateOrdered\":null,\"datePaid\":null,\"currency\":\"GBP\",\"paymentCurrency\":\"GBP\",\"lastIp\":\"127.0.0.1\",\"message\":null,\"returnUrl\":null,\"orderStatusId\":null,\"orderLanguage\":\"en-GB\",\"shippingMethod\":\"freeShipping\",\"shippingMethodId\":\"1\",\"paymentMethodId\":\"2\",\"gatewayId\":\"2\",\"paymentSourceId\":null,\"customerId\":1,\"isPaid\":false,\"paidStatus\":\"unpaid\",\"totalQty\":1,\"pdfUrl\":\"https://jc.test/index.php?p=actions/commerce/downloads/pdf&number=ba3914f16a5f7918f3aadbd75e963fc7&option=ajax\",\"isEmpty\":false,\"totalWeight\":100,\"total\":720,\"totalPrice\":720,\"availableShippingMethods\":{\"freeShipping\":{\"id\":\"1\",\"name\":\"Free UK Shipping, we offer free delivery on all UK orders\",\"handle\":\"freeShipping\",\"enabled\":\"1\",\"isLite\":null,\"price\":0}},\"shippingAddressId\":\"752\",\"shippingAddress\":{\"id\":\"752\",\"isStoreLocation\":false,\"attention\":null,\"title\":\"Mr\",\"firstName\":\"Billy\",\"lastName\":\"Fagan\",\"address1\":\"7 Carden Place\",\"address2\":\"\",\"city\":\"Aberdeen\",\"zipCode\":\"AB10 1PP\",\"phone\":\"\",\"alternativePhone\":null,\"businessName\":\"\",\"businessTaxId\":null,\"businessId\":null,\"stateName\":null,\"countryId\":\"77\",\"stateId\":null,\"fullName\":\"Billy Fagan\",\"countryText\":\"United Kingdom\",\"stateText\":\"\",\"stateValue\":\"\",\"abbreviationText\":\"\"},\"billingAddressId\":\"752\",\"billingAddress\":{\"id\":\"752\",\"isStoreLocation\":false,\"attention\":null,\"title\":\"Mr\",\"firstName\":\"Billy\",\"lastName\":\"Fagan\",\"address1\":\"7 Carden Place\",\"address2\":\"\",\"city\":\"Aberdeen\",\"zipCode\":\"AB10 1PP\",\"phone\":\"\",\"alternativePhone\":null,\"businessName\":\"\",\"businessTaxId\":null,\"businessId\":null,\"stateName\":null,\"countryId\":\"77\",\"stateId\":null,\"fullName\":\"Billy Fagan\",\"countryText\":\"United Kingdom\",\"stateText\":\"\",\"stateValue\":\"\",\"abbreviationText\":\"\"},\"totalTax\":0,\"totalTaxIncluded\":120,\"totalShippingCost\":0,\"totalDiscount\":-1680,\"lineItems\":{\"1581\":{\"id\":1581,\"price\":2400,\"saleAmount\":0,\"salePrice\":2400,\"qty\":1,\"weight\":100,\"length\":0,\"height\":0,\"width\":0,\"total\":720,\"snapshot\":{\"productId\":\"8640\",\"isDefault\":\"1\",\"sku\":\"1108507\",\"price\":2400,\"sortOrder\":\"1\",\"width\":null,\"height\":null,\"length\":null,\"weight\":\"100.0000\",\"stock\":\"1\",\"hasUnlimitedStock\":\"0\",\"minQty\":\"1\",\"maxQty\":null,\"deletedWithProduct\":false,\"id\":\"8641\",\"tempId\":null,\"draftId\":null,\"revisionId\":null,\"uid\":\"906fad82-ec67-4672-a3a7-b9d8b1648132\",\"fieldLayoutId\":null,\"contentId\":\"8476\",\"enabled\":\"1\",\"archived\":\"0\",\"siteId\":\"1\",\"enabledForSite\":\"1\",\"title\":\"18ct white gold three row diamond ring\",\"slug\":null,\"uri\":null,\"dateCreated\":\"2018-10-16T19:22:14+01:00\",\"dateUpdated\":\"2019-08-31T17:24:45+01:00\",\"dateDeleted\":null,\"trashed\":false,\"propagateAll\":false,\"newSiteIds\":[],\"resaving\":false,\"duplicateOf\":null,\"previewing\":false,\"hardDelete\":false,\"ref\":null,\"status\":\"enabled\",\"structureId\":null,\"url\":\"https://jc.test/1108507-18ct-white-gold-three-row-diamond-ring?variant=8641\",\"isAvailable\":true,\"isPromotable\":true,\"shippingCategoryId\":1,\"taxCategoryId\":1,\"onSale\":false,\"cpEditUrl\":\"#\",\"product\":{\"postDate\":\"2018-11-02T00:00:00+00:00\",\"expiryDate\":null,\"typeId\":\"2\",\"taxCategoryId\":\"1\",\"shippingCategoryId\":\"1\",\"promotable\":\"1\",\"freeShipping\":\"0\",\"enabled\":\"1\",\"availableForPurchase\":\"1\",\"defaultVariantId\":\"8641\",\"defaultSku\":\"1108507\",\"defaultPrice\":\"2400.0000\",\"defaultHeight\":\"0.0000\",\"defaultLength\":\"0.0000\",\"defaultWidth\":\"0.0000\",\"defaultWeight\":\"100.0000\",\"taxCategory\":null,\"name\":null,\"id\":\"8640\",\"tempId\":null,\"draftId\":null,\"revisionId\":null,\"uid\":\"c1927a94-beb4-4f27-b4df-3c2dc5bd42ef\",\"fieldLayoutId\":\"15\",\"contentId\":\"8475\",\"archived\":\"0\",\"siteId\":\"1\",\"enabledForSite\":\"1\",\"title\":\"18ct white gold three row diamond ring\",\"slug\":\"1108507-18ct-white-gold-three-row-diamond-ring\",\"uri\":\"1108507-18ct-white-gold-three-row-diamond-ring\",\"dateCreated\":\"2018-10-16T19:22:13+01:00\",\"dateUpdated\":\"2019-08-31T17:24:45+01:00\",\"dateDeleted\":null,\"trashed\":false,\"propagateAll\":false,\"newSiteIds\":[],\"resaving\":false,\"duplicateOf\":null,\"previewing\":false,\"hardDelete\":false,\"ref\":null,\"status\":\"live\",\"structureId\":null,\"url\":\"https://mc.test/1108507-18ct-white-gold-three-row-diamond-ring\"},\"description\":\"18ct white gold three row diamond ring\",\"purchasableId\":\"8641\",\"options\":[],\"sales\":[]},\"note\":\"\",\"orderId\":\"22096\",\"purchasableId\":\"8641\",\"taxCategoryId\":1,\"shippingCategoryId\":1,\"onSale\":false,\"options\":[],\"optionsSignature\":\"d751713988987e9331980363e24189ce\",\"subtotal\":2400}},\"totalLineItems\":1,\"adjustments\":{\"discount\":[{\"id\":15634,\"type\":\"discount\",\"name\":\"My Client Sale\",\"description\":\"18ct white gold three row diamond ring is in the My Client sale.\",\"amount\":-1680,\"sourceSnapshot\":{\"description\":\"18ct white gold three row diamond ring is in the My Client sale.\",\"lineItemId\":null},\"orderId\":\"22096\"}],\"tax\":[{\"id\":15635,\"type\":\"tax\",\"name\":\"UK/EU VAT\",\"description\":\"20% inc\",\"amount\":120,\"sourceSnapshot\":{\"id\":\"2\",\"name\":\"UK/EU VAT\",\"rate\":\"0.2000000000\",\"include\":\"1\",\"isVat\":\"0\",\"taxable\":\"price\",\"taxCategoryId\":\"1\",\"isLite\":null,\"taxZoneId\":\"4\"},\"orderId\":\"22096\"}]},\"totalAdjustments\":2}}",
      "mode":"application/json"
    }
  }
}
  • and here's the response when I just hit the update-cart endpoint with no changes to qty or anything:
{
  "JSON":{
    "success":true,
    "cart":{
      "id":"22096",
      "number":"ba3914f16a5f7918f3aadbd75e963fc7",
      "couponCode":null,
      "itemTotal":720,
      "itemSubtotal":2400,
      "totalPaid":0,
      "email":"billythekid@github.com",
      "isCompleted":false,
      "dateOrdered":null,
      "datePaid":null,
      "currency":"GBP",
      "paymentCurrency":"GBP",
      "lastIp":"127.0.0.1",
      "message":null,
      "returnUrl":null,
      "orderStatusId":null,
      "orderLanguage":"en-GB",
      "shippingMethod":"freeShipping",
      "shippingMethodId":"1",
      "paymentMethodId":"2",
      "gatewayId":"2",
      "paymentSourceId":null,
      "customerId":1,
      "isPaid":false,
      "paidStatus":"unpaid",
      "totalQty":1,
      "pdfUrl":"https://mc.test/index.php?p=actions/commerce/downloads/pdf&number=ba3914f16a5f7918f3aadbd75e963fc7&option=ajax",
      "isEmpty":false,
      "totalWeight":100,
      "total":720,
      "totalPrice":720,
      "availableShippingMethods":{
        "freeShipping":{
          "id":"1",
          "name":"Free UK Shipping, we offer free delivery on all UK orders",
          "handle":"freeShipping",
          "enabled":"1",
          "isLite":null,
          "price":0
        }
      },
      "shippingAddressId":"752",
      "shippingAddress":{
        "id":"752",
        "isStoreLocation":false,
        "attention":null,
        "title":"Mr",
        "firstName":"Billy",
        "lastName":"Fagan",
        "address1":"7 Carden Place",
        "address2":"",
        "city":"Aberdeen",
        "zipCode":"AB10 1PP",
        "phone":"",
        "alternativePhone":null,
        "businessName":"",
        "businessTaxId":null,
        "businessId":null,
        "stateName":null,
        "countryId":"77",
        "stateId":null,
        "fullName":"Billy Fagan",
        "countryText":"United Kingdom",
        "stateText":"",
        "stateValue":"",
        "abbreviationText":""
      },
      "billingAddressId":"752",
      "billingAddress":{
        "id":"752",
        "isStoreLocation":false,
        "attention":null,
        "title":"Mr",
        "firstName":"Billy",
        "lastName":"Fagan",
        "address1":"7 Carden Place",
        "address2":"",
        "city":"Aberdeen",
        "zipCode":"AB10 1PP",
        "phone":"",
        "alternativePhone":null,
        "businessName":"",
        "businessTaxId":null,
        "businessId":null,
        "stateName":null,
        "countryId":"77",
        "stateId":null,
        "fullName":"Billy Fagan",
        "countryText":"United Kingdom",
        "stateText":"",
        "stateValue":"",
        "abbreviationText":""
      },
      "totalTax":0,
      "totalTaxIncluded":120,
      "totalShippingCost":0,
      "totalDiscount":-1680,
      "lineItems":{
        "1581":{
          "id":"1581",
          "price":2400,
          "saleAmount":0,
          "salePrice":2400,
          "qty":"1",
          "weight":100,
          "length":0,
          "height":0,
          "width":0,
          "total":720,
          "snapshot":{
            "productId":"8640",
            "isDefault":"1",
            "sku":"1108507",
            "price":2400,
            "sortOrder":"1",
            "width":null,
            "height":null,
            "length":null,
            "weight":"100.0000",
            "stock":"1",
            "hasUnlimitedStock":"0",
            "minQty":"1",
            "maxQty":null,
            "deletedWithProduct":false,
            "id":"8641",
            "tempId":null,
            "draftId":null,
            "revisionId":null,
            "uid":"906fad82-ec67-4672-a3a7-b9d8b1648132",
            "fieldLayoutId":null,
            "contentId":"8476",
            "enabled":"1",
            "archived":"0",
            "siteId":"1",
            "enabledForSite":"1",
            "title":"18ct white gold three row diamond ring",
            "slug":null,
            "uri":null,
            "dateCreated":"2018-10-16T19:22:14+01:00",
            "dateUpdated":"2019-08-31T17:24:45+01:00",
            "dateDeleted":null,
            "trashed":false,
            "propagateAll":false,
            "newSiteIds":[

            ],
            "resaving":false,
            "duplicateOf":null,
            "previewing":false,
            "hardDelete":false,
            "ref":null,
            "status":"enabled",
            "structureId":null,
            "url":"https://mc.test/1108507-18ct-white-gold-three-row-diamond-ring?variant=8641",
            "isAvailable":true,
            "isPromotable":true,
            "shippingCategoryId":1,
            "taxCategoryId":1,
            "onSale":false,
            "cpEditUrl":"#",
            "product":{
              "postDate":"2018-11-02T00:00:00+00:00",
              "expiryDate":null,
              "typeId":"2",
              "taxCategoryId":"1",
              "shippingCategoryId":"1",
              "promotable":"1",
              "freeShipping":"0",
              "enabled":"1",
              "availableForPurchase":"1",
              "defaultVariantId":"8641",
              "defaultSku":"1108507",
              "defaultPrice":"2400.0000",
              "defaultHeight":"0.0000",
              "defaultLength":"0.0000",
              "defaultWidth":"0.0000",
              "defaultWeight":"100.0000",
              "taxCategory":null,
              "name":null,
              "id":"8640",
              "tempId":null,
              "draftId":null,
              "revisionId":null,
              "uid":"c1927a94-beb4-4f27-b4df-3c2dc5bd42ef",
              "fieldLayoutId":"15",
              "contentId":"8475",
              "archived":"0",
              "siteId":"1",
              "enabledForSite":"1",
              "title":"18ct white gold three row diamond ring",
              "slug":"1108507-18ct-white-gold-three-row-diamond-ring",
              "uri":"1108507-18ct-white-gold-three-row-diamond-ring",
              "dateCreated":"2018-10-16T19:22:13+01:00",
              "dateUpdated":"2019-08-31T17:24:45+01:00",
              "dateDeleted":null,
              "trashed":false,
              "propagateAll":false,
              "newSiteIds":[

              ],
              "resaving":false,
              "duplicateOf":null,
              "previewing":false,
              "hardDelete":false,
              "ref":null,
              "status":"live",
              "structureId":null,
              "url":"https://mc.test/1108507-18ct-white-gold-three-row-diamond-ring"
            },
            "description":"18ct white gold three row diamond ring",
            "purchasableId":"8641",
            "options":[

            ],
            "sales":[

            ]
          },
          "note":"",
          "orderId":"22096",
          "purchasableId":"8641",
          "taxCategoryId":1,
          "shippingCategoryId":1,
          "onSale":false,
          "options":[

          ],
          "optionsSignature":"d751713988987e9331980363e24189ce",
          "subtotal":2400
        }
      },
      "totalLineItems":1,
      "adjustments":{
        "discount":[
          {
            "id":15636,
            "type":"discount",
            "name":"My Client Sale",
            "description":"18ct white gold three row diamond ring is in the My Client sale.",
            "amount":-1680,
            "sourceSnapshot":{
              "description":"18ct white gold three row diamond ring is in the My Client sale.",
              "lineItemId":"1581"
            },
            "orderId":"22096"
          }
        ],
        "tax":[
          {
            "id":15637,
            "type":"tax",
            "name":"UK/EU VAT",
            "description":"20% inc",
            "amount":120,
            "sourceSnapshot":{
              "id":"2",
              "name":"UK/EU VAT",
              "rate":"0.2000000000",
              "include":"1",
              "isVat":"0",
              "taxable":"price",
              "taxCategoryId":"1",
              "isLite":null,
              "taxZoneId":"4"
            },
            "orderId":"22096"
          }
        ]
      },
      "totalAdjustments":2
    }
  },
  "Response payload":{
    "EDITOR_CONFIG":{
      "text":"{\"success\":true,\"cart\":{\"id\":\"22096\",\"number\":\"ba3914f16a5f7918f3aadbd75e963fc7\",\"couponCode\":null,\"itemTotal\":720,\"itemSubtotal\":2400,\"totalPaid\":0,\"email\":\"billythekid@github.com\",\"isCompleted\":false,\"dateOrdered\":null,\"datePaid\":null,\"currency\":\"GBP\",\"paymentCurrency\":\"GBP\",\"lastIp\":\"127.0.0.1\",\"message\":null,\"returnUrl\":null,\"orderStatusId\":null,\"orderLanguage\":\"en-GB\",\"shippingMethod\":\"freeShipping\",\"shippingMethodId\":\"1\",\"paymentMethodId\":\"2\",\"gatewayId\":\"2\",\"paymentSourceId\":null,\"customerId\":1,\"isPaid\":false,\"paidStatus\":\"unpaid\",\"totalQty\":1,\"pdfUrl\":\"https://mc.test/index.php?p=actions/commerce/downloads/pdf&number=ba3914f16a5f7918f3aadbd75e963fc7&option=ajax\",\"isEmpty\":false,\"totalWeight\":100,\"total\":720,\"totalPrice\":720,\"availableShippingMethods\":{\"freeShipping\":{\"id\":\"1\",\"name\":\"Free UK Shipping, we offer free delivery on all UK orders\",\"handle\":\"freeShipping\",\"enabled\":\"1\",\"isLite\":null,\"price\":0}},\"shippingAddressId\":\"752\",\"shippingAddress\":{\"id\":\"752\",\"isStoreLocation\":false,\"attention\":null,\"title\":\"Mr\",\"firstName\":\"Billy\",\"lastName\":\"Fagan\",\"address1\":\"7 Carden Place\",\"address2\":\"\",\"city\":\"Aberdeen\",\"zipCode\":\"AB10 1PP\",\"phone\":\"\",\"alternativePhone\":null,\"businessName\":\"\",\"businessTaxId\":null,\"businessId\":null,\"stateName\":null,\"countryId\":\"77\",\"stateId\":null,\"fullName\":\"Billy Fagan\",\"countryText\":\"United Kingdom\",\"stateText\":\"\",\"stateValue\":\"\",\"abbreviationText\":\"\"},\"billingAddressId\":\"752\",\"billingAddress\":{\"id\":\"752\",\"isStoreLocation\":false,\"attention\":null,\"title\":\"Mr\",\"firstName\":\"Billy\",\"lastName\":\"Fagan\",\"address1\":\"7 Carden Place\",\"address2\":\"\",\"city\":\"Aberdeen\",\"zipCode\":\"AB10 1PP\",\"phone\":\"\",\"alternativePhone\":null,\"businessName\":\"\",\"businessTaxId\":null,\"businessId\":null,\"stateName\":null,\"countryId\":\"77\",\"stateId\":null,\"fullName\":\"Billy Fagan\",\"countryText\":\"United Kingdom\",\"stateText\":\"\",\"stateValue\":\"\",\"abbreviationText\":\"\"},\"totalTax\":0,\"totalTaxIncluded\":120,\"totalShippingCost\":0,\"totalDiscount\":-1680,\"lineItems\":{\"1581\":{\"id\":\"1581\",\"price\":2400,\"saleAmount\":0,\"salePrice\":2400,\"qty\":\"1\",\"weight\":100,\"length\":0,\"height\":0,\"width\":0,\"total\":720,\"snapshot\":{\"productId\":\"8640\",\"isDefault\":\"1\",\"sku\":\"1108507\",\"price\":2400,\"sortOrder\":\"1\",\"width\":null,\"height\":null,\"length\":null,\"weight\":\"100.0000\",\"stock\":\"1\",\"hasUnlimitedStock\":\"0\",\"minQty\":\"1\",\"maxQty\":null,\"deletedWithProduct\":false,\"id\":\"8641\",\"tempId\":null,\"draftId\":null,\"revisionId\":null,\"uid\":\"906fad82-ec67-4672-a3a7-b9d8b1648132\",\"fieldLayoutId\":null,\"contentId\":\"8476\",\"enabled\":\"1\",\"archived\":\"0\",\"siteId\":\"1\",\"enabledForSite\":\"1\",\"title\":\"18ct white gold three row diamond ring\",\"slug\":null,\"uri\":null,\"dateCreated\":\"2018-10-16T19:22:14+01:00\",\"dateUpdated\":\"2019-08-31T17:24:45+01:00\",\"dateDeleted\":null,\"trashed\":false,\"propagateAll\":false,\"newSiteIds\":[],\"resaving\":false,\"duplicateOf\":null,\"previewing\":false,\"hardDelete\":false,\"ref\":null,\"status\":\"enabled\",\"structureId\":null,\"url\":\"https://mc.test/1108507-18ct-white-gold-three-row-diamond-ring?variant=8641\",\"isAvailable\":true,\"isPromotable\":true,\"shippingCategoryId\":1,\"taxCategoryId\":1,\"onSale\":false,\"cpEditUrl\":\"#\",\"product\":{\"postDate\":\"2018-11-02T00:00:00+00:00\",\"expiryDate\":null,\"typeId\":\"2\",\"taxCategoryId\":\"1\",\"shippingCategoryId\":\"1\",\"promotable\":\"1\",\"freeShipping\":\"0\",\"enabled\":\"1\",\"availableForPurchase\":\"1\",\"defaultVariantId\":\"8641\",\"defaultSku\":\"1108507\",\"defaultPrice\":\"2400.0000\",\"defaultHeight\":\"0.0000\",\"defaultLength\":\"0.0000\",\"defaultWidth\":\"0.0000\",\"defaultWeight\":\"100.0000\",\"taxCategory\":null,\"name\":null,\"id\":\"8640\",\"tempId\":null,\"draftId\":null,\"revisionId\":null,\"uid\":\"c1927a94-beb4-4f27-b4df-3c2dc5bd42ef\",\"fieldLayoutId\":\"15\",\"contentId\":\"8475\",\"archived\":\"0\",\"siteId\":\"1\",\"enabledForSite\":\"1\",\"title\":\"18ct white gold three row diamond ring\",\"slug\":\"1108507-18ct-white-gold-three-row-diamond-ring\",\"uri\":\"1108507-18ct-white-gold-three-row-diamond-ring\",\"dateCreated\":\"2018-10-16T19:22:13+01:00\",\"dateUpdated\":\"2019-08-31T17:24:45+01:00\",\"dateDeleted\":null,\"trashed\":false,\"propagateAll\":false,\"newSiteIds\":[],\"resaving\":false,\"duplicateOf\":null,\"previewing\":false,\"hardDelete\":false,\"ref\":null,\"status\":\"live\",\"structureId\":null,\"url\":\"https://mc.test/1108507-18ct-white-gold-three-row-diamond-ring\"},\"description\":\"18ct white gold three row diamond ring\",\"purchasableId\":\"8641\",\"options\":[],\"sales\":[]},\"note\":\"\",\"orderId\":\"22096\",\"purchasableId\":\"8641\",\"taxCategoryId\":1,\"shippingCategoryId\":1,\"onSale\":false,\"options\":[],\"optionsSignature\":\"d751713988987e9331980363e24189ce\",\"subtotal\":2400}},\"totalLineItems\":1,\"adjustments\":{\"discount\":[{\"id\":15636,\"type\":\"discount\",\"name\":\"My Client Sale\",\"description\":\"18ct white gold three row diamond ring is in the My Client sale.\",\"amount\":-1680,\"sourceSnapshot\":{\"description\":\"18ct white gold three row diamond ring is in the My Client sale.\",\"lineItemId\":\"1581\"},\"orderId\":\"22096\"}],\"tax\":[{\"id\":15637,\"type\":\"tax\",\"name\":\"UK/EU VAT\",\"description\":\"20% inc\",\"amount\":120,\"sourceSnapshot\":{\"id\":\"2\",\"name\":\"UK/EU VAT\",\"rate\":\"0.2000000000\",\"include\":\"1\",\"isVat\":\"0\",\"taxable\":\"price\",\"taxCategoryId\":\"1\",\"isLite\":null,\"taxZoneId\":\"4\"},\"orderId\":\"22096\"}]},\"totalAdjustments\":2}}",
      "mode":"application/json"
    }
  }
}

@lukeholder Yep, full code here:

namespace billythekid\myclient\adjusters;

use billythekid\myclient\MyClient as Plugin;

use craft\commerce\base\AdjusterInterface;
use craft\commerce\elements\Order;
use craft\commerce\elements\Product;
use craft\commerce\models\LineItem;
use craft\commerce\models\OrderAdjustment;

class MCSaleAdjuster implements AdjusterInterface
{

  /**
   * Returns adjustments to add to the order
   *
   * @param Order $order
   * @return OrderAdjustment[]
   * @throws \Exception
   */
  public function adjust(Order $order): array
  {
    $adjustments = [];

    if (Plugin::getInstance()->service->saleIsOn())
    {

      /** @var LineItem $item */
      foreach ($order->getLineItems() as $item)
      {

        $purchasable = $item->getPurchasable();
        $product     = $purchasable->product;

        /** @var Product $product */
        if (Plugin::getInstance()->service->productOnSale($product))
        {
          if (!empty($product->mcSalePrice) && $product->mcSalePrice > 0 && $product->mcSalePrice < $purchasable->getPrice())
          {
            $originalPrice = $purchasable->getPrice();
            $salePrice     = $product->mcSalePrice;
            $difference    = (-1 * ($originalPrice - $salePrice)) * $item->qty;

            $adjustment                 = new OrderAdjustment;
            $adjustment->type           = 'discount';
            $adjustment->name           = 'My Client Sale';
            $adjustment->description    = $product->title . " is in the My Client sale.";
            $adjustment->sourceSnapshot = [
                'description' => $adjustment->description,
                'lineItemId'  => $item->id,
            ];
            $adjustment->amount         = $difference;
            $adjustment->setOrder($order);
            $adjustment->setLineItem($item);

            $adjustments[] = $adjustment;
          }
        }
      }
    }

    return $adjustments;

  }
}

@billythekid
Copy link
Contributor Author

So I guess the reason here is that the lineitem hasn't been persisted when the adjuster is first run, so has no ID yet?

@lukeholder lukeholder assigned lukeholder and unassigned nfourtythree Oct 3, 2019
@lukeholder
Copy link
Member

Correct, the adjustment doesn't have an ID at that point in the snapshot but we just need to add the lineitem ID to the json output of the adjustment for it to appear. Will do.

@lukeholder lukeholder added bug and removed 🔎 status: investigating Trying to reproduce ℹ️ status: need more info When waiting for user to supply database or more information. labels Oct 10, 2019
@lukeholder lukeholder added this to the 2.2 milestone Oct 10, 2019
@lukeholder
Copy link
Member

Thanks, fixed for the next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants