woocommerce-ios
woocommerce-ios copied to clipboard
Swift.DecodingError on purchasable products. Expected to decode Bool but found a Number.
Ref: p4a5px-2PC-p2
Some plugins seem to return 0 or 1 rather than the expected boolean for the purchasable API response. This leads to problems loading content within the app as data can't be decoded:
let purchasable = try container.decode(Bool.self, forKey: .purchasable)
We can attempt and offer an alternative decoding if the expected one fails. For example, we offer alternative types for salePrice as is known that plugins alter this field:
// Even though a plain install of WooCommerce Core provides string values,
// some plugins alter the field value from String to Int or Decimal.
let salePrice = container.failsafeDecodeIfPresent(targetType: String.self,
forKey: .salePrice,
shouldDecodeTargetTypeFirst: false,
alternativeTypes: [
.string(transform: { (onSale && $0.isEmpty) ? "0" : $0 }),
.decimal(transform: { NSDecimalNumber(decimal: $0).stringValue })])
?? ""
Impact: medium – bug with workaround, in a low-priority area. Severity: medium – 2 user reports Priority: medium
Another report in 5801963-zd-woothemes.
Another case in 5815022-zd-woothemes. I requested the merchant to contact the plugin developer to change the data type of the purchasable field to boolean.