0

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.

2 Answers 2

1

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.

5
  • Thanks for the answer but it does not seem to work. I've added; 'if ($value['is_percent']) { echo "yes!"; } else {echo "no :(";}' but it does not output anything at all. the percentage for my testing product is 10.
    – James
    Jan 17, 2014 at 16:04
  • @James you have used is_percentage not as is_percent which I mentioned
    – mageUz
    Jan 17, 2014 at 16:06
  • See edited comment, sorry, pressed enter too soon
    – James
    Jan 17, 2014 at 16:07
  • Try print_r($value) to debug
    – mageUz
    Jan 17, 2014 at 16:08
  • Absolutely no output when I do that... very strange since right before I echo the label and pricing value :S
    – James
    Jan 17, 2014 at 16:12
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.