Itonomy / magento2-bundle-products-change-qty

Allow quantity changes with the checkbox based options on bundle products.
MIT License
12 stars 13 forks source link

Quantity for checkbox option is not reflected for product items which have can_change_quantity property true #8

Open GRajwani opened 3 years ago

GRajwani commented 3 years ago

Quantity for checkbox option is not reflected for product items which have can_change_quantity property true. For e.g. a bundle product has checkbox product sku and the product sku has can_change_quantity set to 1 (true),

protected function getQty($selection, $qtys, $selectionOptionId) { if ($selection->getSelectionCanChangeQty() && isset($qtys[$selectionOptionId])) { $qty = (float)$qtys[$selectionOptionId] > 0 ? $qtys[$selectionOptionId] : 1; } elseif (isset($qtys[$selectionOptionId][$selection->getId()])) { $qty = (float)$qtys[$selectionOptionId][$selection->getId()] ? $qtys[$selectionOptionId][$selection->getId()] : 1; } else { $qty = (float)$selection->getSelectionQty() ? $selection->getSelectionQty() : 1; } $qty = (float)$qty; return $qty; }

In this situation, it goes in the first if case since it has $selection->getSelectionCanChangeQty() which is true. It does not get the qty value specified and hence it become qty 1

Please advise if you can set the can_change_quantity to 0 for all checkbox items and is not regressed wlesewhere till checkout or any fix can be applied to this piece of code for method getQty.

Thanks, Girdar

drazulay commented 3 years ago

I'm going to test a patch:

protected function getQty($selection, $qtys, $selectionOptionId)
{
        // getSelectionCanChangeQty returns int. Clumsy use of php type system..
        if ($selection->getSelectionCanChangeQty()) {
            // qty stored as int here...
            // coalesce to 1.0 if index not set
            $qty = $qtys[$selectionOptionId] ?? 1.0;
        } else {
            // qty returned as float here... messy.
            $qty = $selection->getSelectionQty();
        }

        // whatever max returns comparing the two (types of) numbers, make it a float.
        return (float) \max($qty , 1.0);
}
drazulay commented 3 years ago

@andyschofieuk see PR: https://github.com/Itonomy/magento2-bundle-products-change-qty/pull/9

cristianmattar commented 2 years ago

Translated by Google Translate. Sorry about that. The problem was that the second condition would never be executed, as the first one is true with this input: "$qtys = [20 => [4110 => '22']]" After a whole day, debugging the module, I see that the solution is simple, just reverse the order:

protected function getQty($selection, $qtys, $selectionOptionId)
{

    if (isset($qtys[$selectionOptionId][$selection->getId()])) {
        $qty = (float)$qtys[$selectionOptionId][$selection->getId()] ? $qtys[$selectionOptionId][$selection->getId()] : 1;
    }
    elseif ($selection->getSelectionCanChangeQty() && isset($qtys[$selectionOptionId])) {
        $qty = (float)$qtys[$selectionOptionId] > 0 ? $qtys[$selectionOptionId] : 1;
    } 
    else {
        $qty = (float)$selection->getSelectionQty() ? $selection->getSelectionQty() : 1;
    }
    $qty = (float)$qty;

    return $qty;

}