FoxyClient: Is there documentation on how to access coupons from `_embedded`?

I realize you guys may not support the client, but I'm hoping you can point me in the right direction regardless.

I have a populated FoxyClient object which I am passing into another class.
When I dump $fc from within that class, I see all the coupons I am after.

I'm hoping there's an easy way to access the existing data so I can avoid making any new calls for info I already have.
Comments
  • fc_adamfc_adam FoxyCart Team
    @mOrloff,

    I'm assuming you've loaded something like the coupons through the hAPI and you're wanting to get the coupons that are within _embedded there - is that right?

    If so - you'd do something like this:
    $result = $fc->get($coupons_uri, array("limit" => 10));
    $errors = array_merge($errors,$fc->getErrors($result));
    if (!count($errors)) {
    foreach($result['_embedded']['fx:coupons'] as $coupon) {
    echo $coupon['name'];
    }
    }
    The FoxyClient is something we've built - so we'll definitely continue to support that. The above code was roughly taken from our FoxyClient Playground repository - we have some example code there for working with coupons and categories. You can see that here: https://github.com/FoxyCart/foxyclient-php-playground
  • mOrloffmOrloff Member
    edited March 2016
    Kinda.
    I'm working on a `FoxyCartDownloader` class.
    Here's a heavily truncated example of how I'm calling it from the playground `coupons.php` file:
    ...
    $result = $fc->get( $coupons_uri, array("limit" => 10) );
    ...
    $dl = new FoxyCartDownloader( $fc );
    ...

    The `$fc` object already has the coupon data in `[_embedded][fx:coupons]` by time it's passed in.

    As such, I'm hoping to access that data (from within my class) without making a new and redundant call to `$fc->get($coupons_uri, array("limit" => 10))`.

    Suggestions?

    PS: I'm presuming `$fc->get( $uri )` makes a call against the API at the url passed in.
    On that @$$umption, I expect that calling it a second time would be unnecessarily redundant.
    Please correct me if I'm wrong.
  • fc_adamfc_adam FoxyCart Team
    @mOrloff,

    If the $fc object contains the coupon data that you want when you pass it in to your own class - then you should be able to access them through that same object in your class.

    Perhaps I'm missing your problem here though - are you having trouble accessing the values in your class? Ultimately you'll receive whatever you pass through to it.
  • mOrloffmOrloff Member
    edited March 2016
    Yes, trouble accessing the data.

    Based on the coding convention we use, I'm presuming the underscore prefix indicates a protected/private property.
    Still, though, I have tried `var_dump($this->fc->_embedded)`, and the console just says undefined property.

    Is there something obvious I'm missing?
  • fc_adamfc_adam FoxyCart Team
    @mOrloff,

    Try accessing it like I did above as an associative array rather than an object:
    $this->fc['_embedded']
  • Yeah, tried that too.
    Says "~cannot use object as array~" :(
  • fc_adamfc_adam FoxyCart Team
    @mOrfloff,

    If you try to output the embedded coupons right after you get them with the hAPI - does it work there? I have a feeling the troubles you're running into are related to how your taking the values and putting them into the $fc object you've made.

    So if you update your code like this:

    $result = $fc->get( $coupons_uri, array("limit" => 10) );
    foreach($result['_embedded']['fx:coupons'] as $coupon) {
    echo $coupon['name'];
    }
    Does it output the names?

    As another question - how are you actually putting the returned information from the hAPI into the $fc object?
  • Yeah, it works there.
    And now it's seeming like the only options are to either pass in the `$result` separately, or make a redundant call to the API ???
  • fc_adamfc_adam FoxyCart Team
    @mOrloff,

    If you can reference them there, then there must be an issue with how they're worked with after that point. How are you putting the values into the $fc object?
  • The coupons are getting into the $fc object via the $result = $fc->get( $coupons_uri, array("limit" => 10) ); line before passing the $fc object into my new class.

    We're at the point now where can just take an entirely different approach with my class if I have to, but I just really want to figure this out.

    And, just to clarify, this is not an issue specific to my class.
    Even trying to access the _embedded values out of the populated $fc from within the playground's coupons.php file yields the exact same failures
  • fc_adamfc_adam FoxyCart Team
    @mOrloff,

    Ah ok - I see what you're doing now - sorry for misunderstanding it previously. You don't want to pas the $fc object, you want to pass the $result object. That's where the data is placed from the API call, it's not maintained in the $fc object - that's the hAPI object itself.
  • mOrloffmOrloff Member
    edited March 2016
    Because including the coupon codes requires recursive calls to the API, I was hoping to simply pass in the $fc object ... especially since the coupon data I'm after is already in the object at $fc['_embedded']['fx:coupons'] due to a prior call.

    I had to simply take a different approach.
    Thanks for all the help.
  • fc_adamfc_adam FoxyCart Team
    edited March 2016
    @mOrloff,

    I'm glad you were able to find a solution.
    especially since the coupon data I'm after is already in the object at $fc['_embedded']['fx:coupons'] due to a prior call.
    Just to clarify - the _embedded node is actually not really accessible within the $fc object. You may see it present if you output that object to the page - but its included in private nodes within the object as part of how the client works to maintain state. It's not designed to be directly referenced. That data is saved into the variable you specify when you make that call - $result in the case of the code we've discussed previously.

    You mentioned that the data you're after is already included in the return from the API call - if that's the case, it's all ready for you to use in $result. You can access the coupons that have been returned through the API call as $result['_embedded'].

    If you needed to perform more API calls in your other class - you could certainly pass the $fc object along to allow you to do that - but if you need the already returned coupons as well - you'd want to pass $result as well. If you did need to make multiple calls to get the information you need to pass to your other class - you could also do that prior to passing it, collecting the information you need in a new variable and passing that through once all you need is collected.
  • mOrloffmOrloff Member
    edited March 2016
    Yeah, thanks.
    I went the procedural route just to simply get it going.
    I hope to have some time to refine it into something a little more modular soon-ish.

    Anyhow ... I just noticed the `zoom` feature =/

    Have yet to play with it, but my suspicion is that zooming with my initial Coupons call would have alleviated the need for recursive calls for the coupon-codes.
    If so, that would have given me exactly what I was looking for.

    Next time :)
    ~ Mo
Sign In or Register to comment.