-3

I'm using a composite product plugin for Woocommerce and Wordpress and am simply trying to make the price echoed in the highlighted line divide by 2 and display that value (the divided value instead of the whole value).

Here is the code:

            <option data-title="<?php echo get_the_title( $product_id ); ?>" value="<?php echo $product_id; ?>" <?php echo selected( $selected_value, $product_id, false ); ?>><?php

            if ( $quantity_min == $quantity_max && $quantity_min > 1 )
                $quantity = ' &times; ' . $quantity_min;
            else
                $quantity = '';

            echo get_the_title( $product_id ) . $quantity;

        ->->    echo $product->get_composited_item_price_string( $component_id, $product_id );

        ?>
        </option>     

Here is the line that I would like to be divided by two and then displayed (there are arrows next to it above).

echo $product->get_composited_item_price_string( $component_id, $product_id );

Thanks in advance for any help you can provide, it's much appreciated!

sjagr
  • 15,983
  • 5
  • 40
  • 67
  • 2
    `echo $product->get_composited_item_price_string( $component_id, $product_id ) / 2;` You could have tried at least something and you probably would have gotten it the first time. – sjagr Dec 15 '14 at 17:53
  • I tried the above before posting, it returns a value of 0 for some reason. It should return something in this format - "Product - from $x.xx". Using this code, it returns "Product0" – user1566274 Dec 15 '14 at 18:08
  • Ah, probably because that function is returning " - from $x.xx" and not just "x.xx". You should be posting the things you've tried so we don't have to guess what you've tried. See if you can find another function or property in the `$product` class that gives you the raw product. What does `echo get_class($product);` give you? (This is a troubleshooting step.) – sjagr Dec 15 '14 at 18:12
  • Got it, my apologies for not mentioning that. echo get_class($product); returns "WC_Product_Composite" – user1566274 Dec 15 '14 at 18:15
  • Trying to find something that returns the raw product. – user1566274 Dec 15 '14 at 18:16
  • You can try to `print_r($product)` to get the properties, and `print_r(get_class_methods($product))` to get all of the available methods. Can you see anything there that's useful? – sjagr Dec 15 '14 at 18:17

3 Answers3

0
echo ($product->get_composited_item_price_string( $component_id, $product_id ) / 2);

That should do the job. You might also consider using number_format function to format resulting number.

Agares
  • 1,222
  • 1
  • 13
  • 22
0

try:

echo ($product->get_composited_item_price_string( $component_id, $product_id ) / 2);
Eric Anderson
  • 358
  • 2
  • 4
  • 15
0

depending on what you want ( a float or an integer ) use the following :

echo intval($product->get_composited_item_price_string( $component_id, $product_id )) / 2;

echo floatval($product->get_composited_item_price_string( $component_id, $product_id )) / 2;
teeyo
  • 3,665
  • 3
  • 22
  • 37
  • Thanks for the response! For whatever reason, when using intval, a value of 0 is returned. It should return something in this format - "Product - from $x.xx". Using the intval string, it returns "Product0" and using the floatval string, it returns "Product-0". Please let me know if I can provide any additional helpful information. – user1566274 Dec 15 '14 at 18:11
  • does `$product->get_composited_item_price_string( $component_id, $product_id )` return a number or a string with a number ? I mean does it return `X` or `Product X` ? – teeyo Dec 16 '14 at 09:23
  • because if it returns a string concatenated with the number it the functions intval and floatval will always return a 0, so you can return just the number and echo it like so : `echo 'Product-' . intval($product->get_composited_item_price_string( $component_id, $product_id )) / 2;` – teeyo Dec 16 '14 at 09:53
  • Hi teeyo, I very much appreciate you answering again - `$product->get_composited_item_price_string( $component_id, $product_id ` actually returns `- From - $x.xx`. So, when I try your suggestion `echo 'Product-' . intval($product->get_composited_item_price_string( $component_id, $product_id )) / 2;`, it still returns `Product-0` Thanks for your help tho! – user1566274 Dec 17 '14 at 16:55
  • Yup, when there's astring before the number , like in your case, it will always return 0, I wont tell you to change your function but you can make another version of that function which returns only the number needed, or you can use regular expressions to extract the number. – teeyo Dec 17 '14 at 17:53
  • anytime @user1566274 ;) – teeyo Dec 18 '14 at 09:20