skip to Main Content

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
                    March 5, 2020 => 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
        (
        )

        )
)

2

Answers


  1. Chosen as BEST ANSWER

    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);
    }
    

  2. Hope this helps you.

       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>";
            }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search