0

I am using NAME YOUR PRICE PLUGIN and in the Checkout page of my Wordpress site, i need to get the price value (300) of this Wordpress Object. So i ahev set a loop in my functions.php. But i don't know how to do to target this specifc attribute. Thanks for help

foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
        $cart_product = $values['data'];

        print_r ($values['data']);
    }

    WC_Product_Simple Object
(
    [object_type:protected] => product
    [post_type:protected] => product
    [cache_group:protected] => products
    [data:protected] => Array
        (
            [name] => Faire un don
            [slug] => don
            [date_created] => WC_DateTime Object
                (
                    [utc_offset:protected] => 0
                    [date] => 2019-10-25 11:15:27.000000
                    [timezone_type] => 3
                    [timezone] => Europe/Paris
                )

            [status] => publish
            [featured] => 
                         [sku] => don
            ...
            [purchase_note] => 
        )

    [supports:protected] => Array
        (
            [0] => ajax_add_to_cart
        )

    [id:protected] => 969
    [changes:protected] => Array
        (
            [price] => 300
            [sale_price] => 300
            [regular_price] => 300
        )

    [object_read:protected] => 1
    [extra_data:protected] => Array
        (
        )

        )
)
Pipoo
  • 23
  • 1
  • 1
  • 6
  • 2
    The `WC_Product_Simple` class has the `get_price()` method. So, `$product->get_price()`? – George Mar 05 '20 at 11:17
  • thanks i am afraid this won't work because [price] attribute (from Name Your Price) is not the same than [price] returned by get_price() – Pipoo Mar 05 '20 at 12:26
  • There is a first in [data:protected] and a second in [changes:protected]. I need the second. – Pipoo Mar 05 '20 at 12:27
  • You can try it this way https://stackoverflow.com/questions/20334355/how-to-get-protected-property-of-object-in-php… OR you could see how this is stored in the database and read/dislay it from there – 7uc1f3r Mar 05 '20 at 12:45
  • thanks i used your link and it works fine !!! how can i indicate it's the solution ? – Pipoo Mar 05 '20 at 12:55
  • it is best to post the answer you have found to your question yourself (apply your working method). You can refer to the link and then mark your own answer as the solution. Regards – 7uc1f3r Mar 05 '20 at 13:42
  • the solution is here https://stackoverflow.com/questions/20334355/how-to-get-protected-property-of-object-in-php – Pipoo Mar 05 '20 at 13:43
  • yes I know. But that way you can mark your own question as solved, otherwise you don't have that option. There are several answers to that url, describe which one you used I would say. – 7uc1f3r Mar 05 '20 at 13:58
  • please i don"t see button to mark as solved – Pipoo Mar 05 '20 at 14:18

2 Answers2

0

Try this:

   global $woocommerce;
    $items = $woocommerce->cart->get_cart();

        foreach($items as $item => $values) { 
            $_product =  wc_get_product( $values['data']->get_id()); 
            echo "<b>".$_product->get_title().'</b>  <br> Quantity: '.$values['quantity'].'<br>'; 
            $price = get_post_meta($values['product_id'] , '_price', true);
            echo "  Price: ".$price."<br>";
        }
halfer
  • 19,824
  • 17
  • 99
  • 186
vadivel a
  • 1,754
  • 1
  • 8
  • 18
  • it only get the frist price attribute. the solution is here https://stackoverflow.com/questions/20334355/how-to-get-protected-property-of-object-in-php – Pipoo Mar 05 '20 at 13:44
0

Because of the previous answers just targeted the frist attribute PRICE, i finally found a solution with this method:

function accessProtected($obj, $prop) {
  $reflection = new ReflectionClass($obj);
  $property = $reflection->getProperty($prop);
  $property->setAccessible(true);
  return $property->getValue($obj);
}
Pipoo
  • 23
  • 1
  • 1
  • 6