Mateuniu Posted June 24, 2022 Share Posted June 24, 2022 Hello I'm trying to override my CartController -> "Check product quantity availability" The problem is how to get quantity of single product, not all combinations. My code is: if ('update' !== $mode && $this->shouldAvailabilityErrorBeRaised($product, $qty_to_check)) { $this->{$ErrorKey}[] = $this->trans( 'The item %product% in your cart is no longer available in this quantity. You cannot proceed with your order until the quantity is adjusted %productQuantity% - %productCheck%.', array('%product%' => $product->name, '%productQuantity%' => $product->quantity, '%productCheck%' => $qty_to_check), 'Shop.Notifications.Error' ); } I'm trying to display product quantity (of single combination) with $product->quantity but it shows quantity of all combinations. Thanks :) Link to comment Share on other sites More sharing options...
AfterGlow93 Posted June 26, 2022 Share Posted June 26, 2022 (edited) On 6/24/2022 at 6:51 PM, Mateuniu said: Hello I'm trying to override my CartController -> "Check product quantity availability" The problem is how to get quantity of single product, not all combinations. My code is: if ('update' !== $mode && $this->shouldAvailabilityErrorBeRaised($product, $qty_to_check)) { $this->{$ErrorKey}[] = $this->trans( 'The item %product% in your cart is no longer available in this quantity. You cannot proceed with your order until the quantity is adjusted %productQuantity% - %productCheck%.', array('%product%' => $product->name, '%productQuantity%' => $product->quantity, '%productCheck%' => $qty_to_check), 'Shop.Notifications.Error' ); } I'm trying to display product quantity (of single combination) with $product->quantity but it shows quantity of all combinations. Thanks Hi, You have to use $combinations instead of $product. To loop through each combination on one product, use a foreach loop : {foreach from=$combinations item=combination} {$combination->quantity} {/foreach} To show only the selected combination, grab combination from your product in cart using {$product.id_product_attribute} So, the whole thing would be done with something like like this : {if $combinations} {foreach from=$combinations item=combination} {if $combination.id == $product.id_product_attribute} {$combination->quantity} <-- show quantity of current combination in product loop --> {/if} {/foreach} {else} ....... <-- insert your current code for products without combination --> {/if} Edited June 26, 2022 by AfterGlow93 (see edit history) Link to comment Share on other sites More sharing options...
Mateuniu Posted June 27, 2022 Author Share Posted June 27, 2022 9 hours ago, AfterGlow93 said: Hi, You have to use $combinations instead of $product. To loop through each combination on one product, use a foreach loop : {foreach from=$combinations item=combination} {$combination->quantity} {/foreach} To show only the selected combination, grab combination from your product in cart using {$product.id_product_attribute} So, the whole thing would be done with something like like this : {if $combinations} {foreach from=$combinations item=combination} {if $combination.id == $product.id_product_attribute} {$combination->quantity} <-- show quantity of current combination in product loop --> {/if} {/foreach} {else} ....... <-- insert your current code for products without combination --> {/if} Hej, thank you for your response. I see in your code that it's for .tpl file, does it gonna work in my CartController.php ? Link to comment Share on other sites More sharing options...
AfterGlow93 Posted June 27, 2022 Share Posted June 27, 2022 9 hours ago, Mateuniu said: Hej, thank you for your response. I see in your code that it's for .tpl file, does it gonna work in my CartController.php ? 9 hours ago, Mateuniu said: Hej, thank you for your response. I see in your code that it's for .tpl file, does it gonna work in my CartController.php ? Hi, Yes, i thought you just want to display it through car template. On cart controller, it will be slightly different, you need to grab attributes data instead of product data. Here is the function in product class (PS 1.7.8.6, should be different on older versions) : public function getAttributeCombinationsById($id_product_attribute, $id_lang, $groupByIdAttributeGroup = true) { if (!Combination::isFeatureActive()) { return []; } $sql = 'SELECT pa.*, product_attribute_shop.*, ag.`id_attribute_group`, ag.`is_color_group`, agl.`name` AS group_name, al.`name` AS attribute_name, a.`id_attribute`, a.`position` FROM `' . _DB_PREFIX_ . 'product_attribute` pa ' . Shop::addSqlAssociation('product_attribute', 'pa') . ' LEFT JOIN `' . _DB_PREFIX_ . 'product_attribute_combination` pac ON pac.`id_product_attribute` = pa.`id_product_attribute` LEFT JOIN `' . _DB_PREFIX_ . 'attribute` a ON a.`id_attribute` = pac.`id_attribute` LEFT JOIN `' . _DB_PREFIX_ . 'attribute_group` ag ON ag.`id_attribute_group` = a.`id_attribute_group` LEFT JOIN `' . _DB_PREFIX_ . 'attribute_lang` al ON (a.`id_attribute` = al.`id_attribute` AND al.`id_lang` = ' . (int) $id_lang . ') LEFT JOIN `' . _DB_PREFIX_ . 'attribute_group_lang` agl ON (ag.`id_attribute_group` = agl.`id_attribute_group` AND agl.`id_lang` = ' . (int) $id_lang . ') WHERE pa.`id_product` = ' . (int) $this->id . ' AND pa.`id_product_attribute` = ' . (int) $id_product_attribute . ' GROUP BY pa.`id_product_attribute`' . ($groupByIdAttributeGroup ? ',ag.`id_attribute_group`' : '') . ' ORDER BY pa.`id_product_attribute`'; $res = Db::getInstance()->executeS($sql); $computingPrecision = Context::getContext()->getComputingPrecision(); //Get quantity of each variations foreach ($res as $key => $row) { $cache_key = $row['id_product'] . '_' . $row['id_product_attribute'] . '_quantity'; if (!Cache::isStored($cache_key)) { $result = StockAvailable::getQuantityAvailableByProduct($row['id_product'], $row['id_product_attribute']); Cache::store( $cache_key, $result ); $res[$key]['quantity'] = $result; } else { $res[$key]['quantity'] = Cache::retrieve($cache_key); } $ecotax = (float) $res[$key]['ecotax'] ?: 0; $res[$key]['ecotax_tax_excluded'] = $ecotax; $res[$key]['ecotax_tax_included'] = Tools::ps_round($ecotax * (1 + Tax::getProductEcotaxRate() / 100), $computingPrecision); } return $res; } You can call this function with an IF statement, like "If i have an id_product_attributes existing" then, call the function getAttributeCombinationsById and it will return the quantity for the selected combination. Hope this helps Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now