• Resolved Richard Webster

    (@rwebster85)


    Hey guys I’m pulling in a list of all downloads for a customer using

    if ( $downloads = WC()->customer->get_downloadable_products() ) {

    The problem is I’m not able to check for a [product_id] in that array using in_array. Using print_r on $downloads I can see there is a [product_id] and I need to check against that. Here’s by code:

    if(in_array( $product->id , $downloads)) {
    	//do something
    } ;

    What am I doing wrong? If I run the array through a foreach loop like so:

    <?php foreach ( $downloads as $download ) :

    I can check no problem and it’s working using:

    if ($download['product_id'] == $product->id) {

    But I need to be able to check before I do a foreach loop. I need to be able to run

    if ($download['product_id'] == $product->id) {

    before I run the foreach.

    https://www.ads-software.com/plugins/woocommerce/

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Contributor Mike Jolley

    (@mikejolley)

    You’d need to loop as you say and compare as you loop.

    If you need only download permissions of a certain product ID, maybe you should query the database rather than use get_downloadable_products

    Thread Starter Richard Webster

    (@rwebster85)

    Thanks for the post Mike. The downloads aren’t the issue, I’ve built something pretty cool for that now ??

    The crux of my problem now is I want to add some HTML, prior to running my foreach loop, if the current product ID is in the array. For that I thought it would be simple case of comparing the product ID to the array, because that’s exactly what I’m doing in the foreach, just not outside.

    However I can’t do that. in_array isn’t working. My guess is there’s some syntax I’m missing, but I can’t figure it out. I need to do 1 thing if it’s true, not just 1 thing for each one you see. As there could be more than one in the array.

    In the print_r the ID value reads as

    [product_id] => 35
    Thread Starter Richard Webster

    (@rwebster85)

    Ok so simple solution… just declare a variable, increment in the foreach, and check in the foreach that the value is 1. If 1, do the HTML.

    I’d still like to know how to check an array using the above steps. Something to do with the [] I think, I just can’t figure out the syntax.

    Plugin Contributor Mike Jolley

    (@mikejolley)

    This function is magic if you can work it out ?? https://codex.www.ads-software.com/Function_Reference/wp_list_pluck

    Thread Starter Richard Webster

    (@rwebster85)

    Nice, looks like it might work ??

    Plugin Contributor Mike Jolley

    (@mikejolley)

    Good luck ??

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘in_array not working on get_downloadable_products()’ is closed to new replies.