Pergunta

I have created a printable page of product details and all is working well. The only thing which is failing is checking if the product's child price type is fixed or percent;

enter image description here

I have successfully fetched and checked custom options for the price type as follows;

//get options                   
            $options = $product->getOptions();

        foreach ($options as $option)
        {
            $o = $option->getValues();

            foreach ($o as $v)
            {
                //if price_type fixed = echo price
                if ($v["price_type"] == "fixed")
                {
                    $optPrice = $v["price"];
                } //else percent = echo product price + %
                else
                {
                    $optPrice = $priceExcTax * ($v["price"]/100);
                }

                //number_format price
                $optPrice = number_format($optPrice, 2);

                //echo price and title

                $output .= $v['title'] . " (+ &pound;" . $optPrice . " + VAT)<br />";
            }
        }

I hoped this would work in a similar way for child products but I've tried the following with no luck;

$confAttributes = $product->getTypeInstance(true)->getConfigurableAttributesAsArray($product);
                        foreach ($confAttributes as $confAttribute)
                        {
                            echo "<em>Choose " . $confAttribute['store_label'] . "...</em><br />";

                            foreach ($confAttribute["values"] as $value)
                            {
                                echo $value["label"] . " (+ &pound;";

                                //if price_type fixed = echo price
                                if ($value["price_type"] == "fixed")
                                {
                                    $optPrice = $value["pricing_value"];
                                }
                                else //else percent = product price + %
                                {
                                    $optPrice = $priceExcTax * ($value["pricing_value"]/100);
                                }

                                $optPrice = number_format($optPrice,2);
                                echo $optPrice . " + VAT)<br />";
                            }
                        }

Having searched a little I've also tried;

if ($value["is_percentage"])

with no luck either.

The price of child products is ALWAYS being seen as fixed, can anyone help?


UPDATE:

I've even looked up the product I am testing in the raw database and can see the 'is_percent' field so I am unsure as to why anything I try is not working.

Foi útil?

Solução 2

I have found my answer, after some help and further looking at my code. Really @mageUz did answer my question but when I tried it, it didn't work as I was completely backwards in where I was putting the code.

To check the price type the $value['is_percent'] field is there and does work.

Lesson to be learned here, make sure you check your code and that you are putting the right things in the right places and secondly using echo '<pre>' . print_r(<the-var-you-want>) . '</pre>' is an excellent tool.

Outras dicas

Use if ($value["is_percent"]). Here is $confAttribute["values"] fields:

Array
(
    [product_super_attribute_id] => 11
    [value_index] => 45
    [label] => 4a
    [default_label] => 4a
    [store_label] => 4a
    [is_percent] => 1
    [pricing_value] => 10.0000
    [use_default_value] => 1
)

And if you use price as percentage you should input valid non thero value to price input.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a magento.stackexchange
scroll top