Skip to content

Commit 3983bec

Browse files
authored
A fix for issue #9265
This line: $quantity = $quantity ?: 1; in ./vendor/magento/module-catalog/Pricing/Price/TierPrice.php, line 75, uses the ternary shorthand operator to set the quantity to 1 when the quantity that is passed can be converted to false. At this point, the quantity comes from the SaleableInterface item - the simple product model itself. Note that the type signature for the getQty method in SaleableInterface indicates that it should always return a float: /** * Returns quantity of saleable item * * @return float */ public function getQty(); However, on the grouped-product page this item comes from this collection: ./vendor/magento/module-catalog/Model/ResourceModel/Product/Link/Product/Collection.php (a collection of linked products). So the quantity that is set in the product model comes from the "Default quantity" field which is stored as an attribute of the link(catalog_product_link_attribute_decimal) - however, since neither Magento nor the underlying Zend libraries have any conversion from the MySQL to PHP types when loading the collections, and MySQL client/server communication is done via a text-based protocol, this "qty" field ends up being a string in PHP. Thus, the getQty function will return a string representation of a decimal value("0.0000000") in a clear violation of the SaleableInterface, and it leads to this issue with the tier price above(since the ternary shorthand operator will treat "0.000000" string as true, not setting the quantity to 1 as a result). Suggested fix does a forceful conversion of the link-attributes with type=decimal to a PHP float-value on the link-product-collection afterLoad event. It is possible to do the same for the integer type as well, but doing it just for the type=decimal seems least intrusive, and it would solve this interface-mismatch and tier-price issue.
1 parent 1a8dd8c commit 3983bec

File tree

1 file changed

+24
-0
lines changed
  • app/code/Magento/Catalog/Model/ResourceModel/Product/Link/Product

1 file changed

+24
-0
lines changed

app/code/Magento/Catalog/Model/ResourceModel/Product/Link/Product/Collection.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,4 +361,28 @@ private function joinProductsToLinks()
361361
);
362362
}
363363
}
364+
365+
/**
366+
* After the collection has been loaded
367+
*
368+
* @return $this
369+
*/
370+
protected function _afterLoad()
371+
{
372+
if($this->getLinkModel()) {
373+
$attributes = $this->getLinkAttributes();
374+
foreach($this as $item) {
375+
foreach ($attributes as $attribute) {
376+
$code = $attribute['code'];
377+
$value = $item->getData($code);
378+
if($value !== null) {
379+
if($attribute['type'] == 'decimal') {
380+
$item->setData($code,(float)$value);
381+
}
382+
}
383+
}
384+
}
385+
}
386+
return parent::_afterLoad();
387+
}
364388
}

0 commit comments

Comments
 (0)