0

I have a WP site using WooCommerce. Our products are made up of physical stock, a download and some links to pages on our site.

I had set this up by placing the links to the specific product pages in WooCommerce's 'downloadable files' section which did work fine until now. Something changed and I get a 'file not on server error' when I try to add links to the 'downloadable files' area now.

I am trying to recreate this by adding the links in manually to the my-downloads.php file (new template in child theme) but I cannot work out the code to check on the qualifying criteria for the links.

In essence what I need to do is identical to what WooCoomerce does to check which (if any) downloads to display on the My Account page.

The only difference is I want to create a simple link to specific pages on my site (which are private to those who have purchased the item).

It must check:

  1. if the user is logged in
  2. have they purchased 'item a', 'item b', 'item c', etc. (4 items, 7 SKU's as 1 product has 4 variations)
  3. was the purchase in the last 365 days

Then:

  • if they purchased 'item a' and meet other criteria display link(s)
  • if they purchased 'item b' and meet other criteria display link(s)

etc.

Any help with this would be greatly appreciated, I am not a programmer, just pretty good with using software and can follow instructions well enough to hack around in php files to get stuff done; I get lost with the hooks and filters and what they return/need.

2

This is not that complicated, here is a code snippet that will get you started:

if(is_user_logged_in()) {

    //We only need these products to check
    $products_to_check = array(81,82,83,84);
    $customer_bought = array();

    //Get all orders made by the current user in the last 365 days
    $customer_orders = get_posts( apply_filters( 'woocommerce_my_account_my_orders_query', array(
        'numberposts' => $order_count,
        'meta_key'    => '_customer_user',
        'meta_value'  => get_current_user_id(),
        'post_type'   => wc_get_order_types( 'view-orders' ),
        'post_status' => array('wc-completed', 'wc-processing'),
        'date_query' => array(
            array(
                'column' => 'post_date_gmt',
                'after' => '1 year ago',
            )
        )
    )));

    //Loop trough the orders
    foreach ( $customer_orders as $customer_order ) {
        $order = wc_get_order();
        $order->populate( $customer_order );
        $items = $order->get_items();

        //Loop trough the order items and see if it is a product we need to cehck
        foreach($items as $item) {          
            if(in_array($item['product_id'], $products_to_check)) {
                $customer_bought[] = $item['product_id'];
            }
        }

    }

} else {
    echo 'You need to sign in first!';
}

So in short: we make sure that the user is signed in, we get all of the orders made by the currently logged in customer in the last year, we loop trough the orders and see if they bought the specific item(s) we're looking for. After, this, you can use the $customer_bought array to check if they bought a product or not, something like this:

if(in_array(81, $customer_bought)) {
    echo 'You bought the item #81 in the last 365 days, so you will get these links...';
}

if(in_array(82, $customer_bought)) {
    echo 'You bought the item #82 in the last 365 days, so you will get these links...';
}

Source: http://woahcommerce.com/2015/06/check-if-user-bought-a-specific-woocommerce-product/

5
  • Thanks you passatgt, I ran out of ways to Google this problem and none of my Googling efforts over the last few days have thrown up that link. Very much appreciated, that should give me plenty to play with in the morning! Jun 28 '15 at 19:08
  • Hi there, I have spent some time playing with your code and that on the linked page and keep coming up with the same error: Warning: in_array() expects parameter 2 to be array, string given in /nas/wp/www/staging/lsesetup/wp-content/themes/storefront-child/woocommerce/myaccount/my-downloads.php on line 85 Warning: in_array() expects parameter 2 to be array, string given in /nas/wp/www/staging/lsesetup/wp-content/themes/storefront-child/woocommerce/myaccount/my-downloads.php on line 89 Any ideas? Jun 28 '15 at 21:21
  • OK, so I found a solution: 'if(in_array($customer_bought, array('81'))', seems to fix the array issue. Will update once I have completed testing. Jun 29 '15 at 9:13
  • Hi! I updated the code, the issue was in this line: ` $customer_bought = $item['product_id']; ` and should be replaced with $customer_bought[] = $item['product_id']; The rest of the code is fine:)
    – passatgt
    Jun 29 '15 at 9:23
  • Many thanks passatgt. Do you have a PayPal account, can I buy you a drink? Jun 29 '15 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.