• Resolved leiho

    (@leiho)


    Hello,

    For an online shop selling records, I’d like to prevent product downloads before the release date which is based on simple ACF field at product page level.

    I was about to edit a few template pages (order-details.php, order-downloads.php,
    email-downloads.php) when I found out I could eventually restrict permissions based on ‘woocommerce_order_is_download_permitted’ filter.

    The code below is almost working but fails when an order is made of multiple products where at least one is set before the release date. In this case all product downloads are hidden ((view-order, order-received and email order) while I only want to hide the ones that have not been released yet.

     php
    /**
     * Restrict product downloads before release date
     *
     * @param [type] $data
     * @param [type] $order
     * @return void
     */
    function restrict_download_permission_before_release($data, $order)
    {
        if ($data === true) {
            $items = $order->get_items();
            foreach ($items as $item_id => $item) {
                // $product_id = $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id();
                $release_date = get_field('date_de_sortie', $item->get_product_id(), false);
                $date_now = date('Ymd');
                if ($release_date > $date_now) return false;
            }
        }
        return $data;
    }
    add_filter('woocommerce_order_is_download_permitted', 'restrict_download_permission_before_release', 10, 2);
    

    So apparently when I return false, I set the global download permission to false where I’d like to specially target and filter permission for each product.

    Should I use another filter or something? Any help would be really appreciated at this point, thank you!

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Conditionally restrict product downloads’ is closed to new replies.