function wc_customer_has_download_permissions($order_id, $product_id)
{
global $wpdb;
// Check if the download permission already exists for the product and order.
$exists = $wpdb->get_var($wpdb->prepare(
"SELECT COUNT(*) FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions
WHERE order_id = %d AND product_id = %d",
$order_id,
$product_id
));
return $exists > 0;
}
function updates_past_order_pdf_permission()
{
// Exclude cancelled, failed, and refunded statuses
$excluded_statuses = ['wc-cancelled', 'wc-failed', 'wc-refunded'];
$all_statuses = array_keys(wc_get_order_statuses());
$statuses_to_include = array_diff($all_statuses, $excluded_statuses);
// Query orders in the specified date range
$args = [
'limit' => -1,
'status' => $statuses_to_include,
'meta_query' => [
[
'key' => '_created_via',
'value' => 'direct',
'compare' => 'LIKE',
],
],
'date_query' => [
[
'after' => '2025-03-17',
'before' => '2025-03-31',
'inclusive' => true,
],
]
];
$orders = wc_get_orders($args);
if (empty($orders)) {
return 'No orders found.';
}
foreach ($orders as $order) {
// Use an array to track processed products for this order
$processed_products = [];
foreach ($order->get_items() as $item_id => $item) {
$product_id = $item->get_product_id();
$product = wc_get_product($product_id);
// Only process downloadable products
if ($product && $product->is_downloadable()) {
// Prevent duplicate permissions by checking processed array
if (!in_array($product_id, $processed_products) && !wc_customer_has_download_permissions($order->get_id(), $product_id)) {
wc_downloadable_product_permissions($order->get_id(), true, $product_id);
$processed_products[] = $product_id; // Mark this product as processed
}
}
// Handle bundled products
if ($product && $product->is_type('bundle')) {
$bundle_items = $product->get_bundled_items();
foreach ($bundle_items as $bundle_item) {
$child_product_id = $bundle_item->get_product_id();
$child_product = wc_get_product($child_product_id);
// Process child product if it's downloadable and not processed yet
if ($child_product && $child_product->is_downloadable()) {
if (!in_array($child_product_id, $processed_products) && !wc_customer_has_download_permissions($order->get_id(), $child_product_id)) {
wc_downloadable_product_permissions($order->get_id(), true, $child_product_id);
$processed_products[] = $child_product_id; // Mark this child product as processed
}
}
}
}
}
}
return 'Processed successfully.';
}
add_shortcode('updates_past_order_pdf_permission', 'updates_past_order_pdf_permission');
我终于得到了这个问题的答案,在下面我将在下面分享完整的代码,如果您想使用它,只需确保您在批处理中运行此代码,例如01/01/2024到01/04/2025,然后再次在01/04/04/2025到01/08/2025,以避免在服务器上重载。
function wc_customer_has_download_permissions($order_id, $product_id)
{
global $wpdb;
// Check if the download permission already exists for the product and order.
$exists = $wpdb->get_var($wpdb->prepare(
"SELECT COUNT(*) FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions
WHERE order_id = %d AND product_id = %d",
$order_id,
$product_id
));
return $exists > 0;
}
function add_missing_download_permissions($order_id, $product_id)
{
global $wpdb;
// Get the product object
$product = wc_get_product($product_id);
if (!$product || !$product->is_downloadable()) {
return;
}
// Get the download files associated with this product
$downloads = $product->get_downloads();
if (empty($downloads)) {
return;
}
// Get order details
$order = wc_get_order($order_id);
$user_id = $order->get_user_id();
$order_key = $order->get_order_key();
foreach ($downloads as $download_id => $download) {
// Check if this specific file already has permissions
$file_exists = $wpdb->get_var($wpdb->prepare(
"SELECT COUNT(*) FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions
WHERE order_id = %d AND product_id = %d AND download_id = %s",
$order_id,
$product_id,
$download_id
));
if ($file_exists == 0) {
// Insert new download permission if it doesn't exist
$wpdb->insert(
"{$wpdb->prefix}woocommerce_downloadable_product_permissions",
[
'download_id' => $download_id,
'product_id' => $product_id,
'user_id' => $user_id,
'order_id' => $order_id,
'order_key' => $order_key,
'downloads_remaining' => '',
'access_granted' => current_time('mysql'),
'download_count' => 0
],
['%s', '%d', '%d', '%d', '%s', '%s', '%s', '%d']
);
}
}
}
function updates_past_order_pdf_permission()
{
// Exclude cancelled, failed, and refunded statuses
$excluded_statuses = ['wc-cancelled', 'wc-failed', 'wc-refunded'];
$all_statuses = array_keys(wc_get_order_statuses());
$statuses_to_include = array_diff($all_statuses, $excluded_statuses);
// Query orders in the specified date range
$args = [
'limit' => -1,
'status' => $statuses_to_include,
'meta_query' => [
[
'key' => '_created_via',
'value' => 'direct',
'compare' => 'LIKE',
],
],
'date_query' => [
[
'after' => '2025-05-01',
'before' => '2025-12-31',
'inclusive' => true,
],
]
];
$orders = wc_get_orders($args);
if (empty($orders)) {
return 'No orders found.';
}
foreach ($orders as $order) {
$order_id = $order->get_id();
foreach ($order->get_items() as $item_id => $item) {
$product_id = $item->get_product_id();
$product = wc_get_product($product_id);
// Process main product
if ($product && $product->is_downloadable()) {
add_missing_download_permissions($order_id, $product_id);
}
// Process bundled products
if ($product && $product->is_type('bundle')) {
$bundle_items = $product->get_bundled_items();
foreach ($bundle_items as $bundle_item) {
$child_product_id = $bundle_item->get_product_id();
$child_product = wc_get_product($child_product_id);
if ($child_product && $child_product->is_downloadable()) {
add_missing_download_permissions($order_id, $child_product_id);
}
}
}
}
}
return 'Processed successfully.';
}
add_shortcode('updates_past_order_pdf_permission', 'updates_past_order_pdf_permission');