Search code examples
phpsmartye-commerceprestashopprestashop-1.6

Prestashop Get combination product from id's


I'm trying to display quantity, combinations and etc. on the product-list. So far i've got it to display the combinations just fine, so that i can select an attribute from each group assigned to the product.

Now, how do i combine those "attribute id's" into a combination product, so that i can see prices, add it to cart etc.

This is my module to fetch combinations

function hookdisplayProductOnList($params){
    $product=new Product($params['id_product']);
    $combinations=$product->getAttributeCombinations($this->context->language->id);

    $combArray = [];
    $combArrayIds = [];

    foreach( $combinations as $k => $v )
    {   
        if( !in_array($v["id_attribute"], $combArrayIds))
        {
            $combArrayIds[] = $v["id_attribute"];
            $combArray[ $v["group_name"] ][] = $v;
        }
    }

    $this->smarty->assign('combinations',$combArray);
    return $this->display(__FILE__, 'combinations.tpl');
}

This is how i output the groups

{foreach $combinations as $k=>$comb}

            <ul class="attribute-select" data-group="{$k}">
            {foreach from=$comb item=attr}
                <li data-combId="{$attr.id_attribute}" title="{l s='+'} {convertPrice price=$attr.unit_price_impact}">{$attr.attribute_name}</li>
            {/foreach}
            </ul><br />


{/foreach}

Is there any helper function or stuff like that. Like:

::GetByAttributes($product_id, [ attr_id1, attr_id2, attr_id3 ] );

Solution

  • From a product ID and an array of attribute IDs you can get back a product attribute ID. I think that is what you need?

    $productId = 123;
    $attributeIds = [123, 1234];
    $combinationId = (int) \Product::getIdProductAttributesByIdAttributes(
        $productId,
        $attributeIds
    );
    

    Update

    As noted in the comment by @PululuK in PrestaShop 1.7.3.1 this is deprecated and we should use Product::getIdProductAttributeByIdAttributes() instead, see https://github.com/PrestaShop/PrestaShop/blob/develop/classes/Product.php#L6511.