Jump to content

Is it possible to do that with PRESTASHOP


Recommended Posts

It's not possible to do that with PrestaShop's settings, but there is a paid module called Attribute Grid here that does it.

Okay thanks perfect man ONE last question when you have like multiple products with like different size and 1 size is out of stock it doesn't go automatically on the one with stock how can you change it so that when that size is out of stock it goes automatically on the one with stock instead

Link to comment
Share on other sites

PrestaShop doesn't have a setting for that either. It would require writing an override that changes the default combination after getting the product information.

Ok thanks so no module does that?

Link to comment
Share on other sites

It is quite difficult, but I've tried to write an override. Try creating override/controllers/front/CategoryController.php with the following:

<?php

class CategoryController extends CategoryControllerCore
{
    public function assignProductList()
    {
        parent::assignProductList();

        foreach ($this->cat_products as &$cat_product) {
            $product = new Product((int)$cat_product['id_product']);
            
            if (Validate::isLoadedObject($product)) {
                $combinations = $product->getAttributeCombinations((int)$this->context->language->id);
                
                $default_attribute = (int)Product::getDefaultAttribute((int)$product->id);
                $default_quantity = 0;
                $first_with_quantity = 0;
                $first_minimal_quantity = 0;
                $first_attribute_price = 0;
                
                foreach ($combinations as $combination) {
                    if ($combination['id_product_attribute'] == $default_attribute) {
                        $default_quantity = $combination['quantity'];   
                    } else if ($first_with_quantity == 0 && $combination['quantity'] > 0) {
                        $first_with_quantity = $combination['id_product_attribute'];
                        $first_quantity = $combination['quantity'];
                        $first_minimal_quantity = $combination['minimal_quantity'];
                        $first_attribute_price = $combination['price'];
                    }
                }
                
                if ($default_quantity == 0 && $first_with_quantity > 0) {
                    $cat_product['available_for_order'] = "1";
                    $cat_product['id_product_attribute'] = $first_with_quantity;
                    $cat_product['cache_default_attribute'] = $first_with_quantity;
                    $cat_product['quantity'] = $first_quantity;
                    $cat_product['product_attribute_minimal_quantity'] = $first_minimal_quantity;
                    $cat_product['price'] = $cat_product['price'] - $cat_product['attribute_price'] + $first_attribute_price;
                    
                    $cat_product['price_tax_exc'] = Product::getPriceStatic(
                        (int)$product->id,
                        false,
                        $first_with_quantity,
                        (Product::$_taxCalculationMethod == PS_TAX_EXC ? 2 : 6)
                    );
            
                    if (Product::$_taxCalculationMethod == PS_TAX_EXC) {
                        $cat_product['price_tax_exc'] = Tools::ps_round($cat_product['price_tax_exc'], 2);
                        $cat_product['price'] = Product::getPriceStatic(
                            (int)$product->id,
                            true,
                            $first_with_quantity,
                            6
                        );
                        $cat_product['price_without_reduction'] = Product::getPriceStatic(
                            (int)$product->id,
                            false,
                            $first_with_quantity,
                            2,
                            null,
                            false,
                            false
                        );
                    } else {
                        $row['price'] = Tools::ps_round(
                            Product::getPriceStatic(
                                (int)$product->id,
                                true,
                                $first_with_quantity,
                                6
                            ),
                            (int)Configuration::get('PS_PRICE_DISPLAY_PRECISION')
                        );
                        $row['price_without_reduction'] = Product::getPriceStatic(
                            (int)$product->id,
                            true,
                            $first_with_quantity,
                            6,
                            null,
                            false,
                            false
                        );
                    }
                                       
                    $cat_product['orderprice'] = $cat_product['price_tax_exc'];
                }
            }
        }
    }
}

Remember to go to Advanced Parameters > Performance and click the "Clear cache" button (or manually delete cache/class_index.php) so PrestaShop finds the override.

 

I didn't have time to fully test this code, so it may need to be improved.

Link to comment
Share on other sites

  • 1 month later...

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...