Changer la declinaison par défaut quand hors stock sur la fiche produit Prestashop

Article écrit le

id_product = (int) Tools::getValue('id_product');
        $this->id_product_attribute = (int) Tools::getValue('id_product_attribute', null);

        // START CODE myShop
        // On veux savoir s il y a encore du stock dans cette declinaison
        $stock = StockAvailable::getQuantityAvailableByProduct($this->id_product, $this->id_product_attribute);
        
        // If the product is not available in desired attributes, change the default id_product_attribute
        if ($stock <=0) {
            // on regarde si d'autre declinaison existe et on du stock
            $product_attribute_ids = Product::getProductAttributesIds($this->id_product);
            foreach ($product_attribute_ids as $product_attribute_data) {
                // Access "id_product_attribute" from the associative array
                $product_attribute_id = $product_attribute_data['id_product_attribute'];
        
                // Get the stock for each product attribute
                $product_attribute_stock[$product_attribute_id] = StockAvailable::getQuantityAvailableByProduct($this->id_product, $product_attribute_id);
            }

           // Find the first attribute with stock > 0 and set it as the default
            foreach ($product_attribute_stock as $product_attribute_id => $available_stock) {
                if ($available_stock > 0) {
                    // Set the default id_product_attribute to this attribute
                    $this->id_product_attribute = $product_attribute_id;

                    // Optionally, break the loop if you want to stop at the first available attribute
                    break;
                }
            }
            
        }
        //END CODE myShop


        // Load viewing modes
        if ('1' === Tools::getValue('quickview')) {
            $this->setQuickViewMode();
        }

        // We are in a preview mode only if proper admin token was also provided in the URL
        if ('1' === Tools::getValue('preview') && Tools::getValue('adtoken') == Tools::getAdminToken(
            'AdminProducts'
            . (int) Tab::getIdFromClassName('AdminProducts')
            . (int) Tools::getValue('id_employee')
        )) {
            $this->setPreviewMode();
        }

        // Try to load product object, otherwise immediately redirect to 404
        if ($this->id_product) {
            $this->product = new Product($this->id_product, true, $this->context->language->id, $this->context->shop->id);
        }
        if (!Validate::isLoadedObject($this->product)) {
            header('HTTP/1.1 404 Not Found');
            header('Status: 404 Not Found');
            $this->errors[] = $this->trans('This product is no longer available.', [], 'Shop.Notifications.Error');
            $this->setTemplate('errors/404');

            return;
        }

        // Check if the user is on correct URL and redirect if needed
        $this->canonicalRedirection();

        // Set proper template to product
        $this->setTemplate('catalog/product', [
            'entity' => 'product',
            'id' => $this->id_product,
        ]);

        // Performs multiple checks and redirects user to error page if needed
        $this->checkPermissionsToViewProduct();

        // Load product category
        $this->initializeCategory();
    }
}
Alexandre Carette

Expert Prestashop & Shopify, éducateur & CEO CodemyShop

Discussion (20)

Very straight-to-point article. Really worth time reading. Thank you! But tools are just the instruments for the UX designers. The knowledge of the design tools are as important as the creation of the design strategy.

Much appreciated! Glad you liked it ☺️

The article covers the essentials, challenges, myths and stages the UX designer should consider while creating the design strategy.

Thanks for sharing this. I do came from the Backend development and explored some of the tools to design my Side Projects.

Sign up for our newsletter

Stay up to date with the roadmap progress, announcements and exclusive discounts feel free to sign up with your email.