重新生成旧订单的 WooCommerce 下载权限 [英] Regenerating WooCommerce Download Permissions on older orders

查看:21
本文介绍了重新生成旧订单的 WooCommerce 下载权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过脚本向所有以前的订单添加一些下载权限,以便批量执行.该脚本似乎可以正常工作,期待一件事.这是脚本……

I am trying to add some download permissions to all previous orders via a script to do them in batch. The script seems to work fine expect for one thing. Here is the script…

function update_download_permissions(){

  $orders = get_posts( array(
    'post_type'      => 'shop_order',
    'post_status'    => 'wc-completed',
    'posts_per_page' => -1
  ) );

  foreach ( $orders as $order ) {
    wc_downloadable_product_permissions( $order->ID, true );
  }

}

问题是 wc_downloadable_product_permissions 函数在 wp_woocommerce_downloadable_product_permissions 表中产生重复的条目.

The problem is the wc_downloadable_product_permissions function is producing duplicate entries in the wp_woocommerce_downloadable_product_permissions table.

我尝试将第二个参数设置为 false(默认值),但这导致没有创建权限.

I tried to set the second argument to false (the default) but that resulted in no permissions being created.

有人对为什么设置重复下载权限有任何想法吗?

Does anybody have any ideas as to why duplicate download permissions are being set?

干杯!

推荐答案

在尝试将项目添加到现有订单然后重新生成权限时,我在挖掘了一些 WooCommerce 源代码后遇到了您的问题.

I came across your question after digging through some of the WooCommerce source code, while attempting to add an item to an existing order and then regenerate the permissions.

wc_downloadable_product_permissions() 将创建重复权限条目的原因是因为它不检查任何现有权限.它只是为订单中的每个项目在权限表中插入另一个条目,这是不好的,因为这将在管理员和用户帐户前端显示为另一个下载.

The reason wc_downloadable_product_permissions() will create duplicate permission entries is because it does not check for any existing permissions. It simply inserts another entry into the permissions table for every item in the order, which is no good because this will then show up as another download in both the admin and user account frontend.

第二个 force 参数(记录不佳)与一个布尔标志相关,该标志指示 wc_downloadable_product_permissions() 之前是否运行过.布尔值在函数结束时通过 set_download_permissions_granted 方法设置为 true.如果 force 为真,它将忽略布尔值.如果 force 为假,布尔值为真,函数将在接近开始时返回.

The second force parameter (poorly documented), is related to a boolean flag that indicates whether wc_downloadable_product_permissions() has run before. The boolean is set to true at the end of the function via the set_download_permissions_granted method. If force is true, it will ignore the boolean. If force is false, and the boolean is true, the function will return near the start.

我创建了这个函数,它使用与管理员订单操作重新生成下载权限"相同的函数:

I created this function which uses the same functions as used by the admin Order action "Regenerate download permissions":

/**
 * Regenerate the WooCommerce download permissions for an order
 * @param  Integer $order_id
 */
function regen_woo_downloadable_product_permissions( $order_id ){

    // Remove all existing download permissions for this order.
    // This uses the same code as the "regenerate download permissions" action in the WP admin (https://github.com/woocommerce/woocommerce/blob/3.5.2/includes/admin/meta-boxes/class-wc-meta-box-order-actions.php#L129-L131)
    // An instance of the download's Data Store (WC_Customer_Download_Data_Store) is created and
    // uses its method to delete a download permission from the database by order ID.
    $data_store = WC_Data_Store::load( 'customer-download' );
    $data_store->delete_by_order_id( $order_id );

    // Run WooCommerce's built in function to create the permissions for an order (https://docs.woocommerce.com/wc-apidocs/function-wc_downloadable_product_permissions.html)
    // Setting the second "force" argument to true makes sure that this ignores the fact that permissions
    // have already been generated on the order.
    wc_downloadable_product_permissions( $order_id, true );

}

这篇关于重新生成旧订单的 WooCommerce 下载权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆