如何使用 Action Scheduler tun on/off cron 对 woocommerce 订单进行克隆操作?

How to use Action Scheduler tun on/off cron for clone action on woocommerce order?

提问人:ivanasetiawan 提问时间:11/17/2023 最后编辑:ivanasetiawan 更新时间:11/18/2023 访问量:35

问:

我有一个克隆 WooCommerce 订单 () 的功能,当您单击订单详细信息页面上的按钮时,该函数起作用:mutiara_woocommerce_order_clone

function mutiara_woocommerce_order_clone($order_id = false, $post_status = 'wc-order-proposalreq')
{
    global $wpdb;

    /**
     * Get original post which must be cloned
     */
    $post_id         = (isset($_GET['post']) ? absint($_GET['post']) : absint($_POST['post']));
    if ( (int)$order_id ){
        $post_id = $order_id;
    }
    $post                   = get_post($post_id);
    $current_user           = wp_get_current_user();
    $new_post_author        = get_current_user_id();

    /**
     * If post exists - make clone
     */
    if (isset($post) && $post != null) {

        /**
         * Data for new post object
         */
        $args = array(
            'post_author'    => $new_post_author,
            'post_content'   => $post->post_content,
            'post_excerpt'   => $post->post_excerpt,
            'post_name'      => $post->post_name,
            'post_parent'    => $post->post_parent,
            'post_password'  => $post->post_password,
            'post_status'    => $post_status,
            'post_title'     => $post->post_title,
            'post_type'      => $post->post_type,
            'menu_order'     => $post->menu_order
        );

        /**
         * Create new post (order)
         */
        $new_post_id = wp_insert_post($args);

        /**
         * Grab original order items to be clone
         * Each product inside the new order must also contain the meta_data
         */
        $order     = wc_get_order($post_id);
        $new_order = wc_get_order($new_post_id);

        foreach ($order->get_items() as $item_id => $item) {
            $product  = $item->get_product(); // get an instance of the WC_Product Object
            $new_order->add_product($product, $item->get_quantity()); // Add the product
        }

        /**
         * Duplicate fields using native WC Object API
         */
        $wc_duplicate_fields = [
            'customer_id',
            'billing_first_name',
            'billing_last_name',
            'billing_company',
        ];
        foreach ($wc_duplicate_fields as $field) {
            $setter_method = "set_{$field}";
            $getter_method = "get_{$field}";
            $new_order->$setter_method($order->$getter_method());
        }

        /**
         * Refresh Totals for new order
         */
        $new_order->calculate_totals();

        /**
         * Save all the changes to new order
         */
        $new_order->save();

        /**
         * Redirect user to new post and exit
         */
        wp_redirect(admin_url('post.php?action=edit&post=' . $new_post_id));
        exit;

    } else {
        wp_die(__('Can\'t clone, post with given ID not found: ', IS_WOOCOMMERCE_RECURRING_ORDER_TEXT_DOMAIN) . $post_id);
    }
}

add_action('admin_action_mutiara_woocommerce_order_clone', 'mutiara_woocommerce_order_clone');

我删除了很多东西..使用上面的代码,您可以在 WooCommerce 上克隆订单。我希望能够设置一个触发这些操作的 cron:

  • 克隆订单每周
  • 克隆订单每两周一次
  • 克隆订单每月
  • 停止 cron(也许我需要“标记”原始的“订单 ID”,然后在该特定订单详细信息上添加“Stop cron”)

enter image description here

我安装了插件 Action Scheduler 并遵循它们的使用情况。并像这样应用它:

if ( ! class_exists( 'ActionScheduler', false ) || ! ActionScheduler::is_initialized() ) {
    require_once( dirname( __FILE__ ) . '/libraries/action-scheduler/action-scheduler.php' );
    ActionScheduler::init( dirname( __FILE__ ) . '/libraries/action-scheduler/action-scheduler.php' );
}
/**
 * Schedule `woocommerce_order_action_clone_order_weekly`
 *
 * @param $timestamp (integer)(required) The Unix timestamp representing the date you want the action to run.
 * @param $interval_in_seconds (integer)(required) How long to wait between runs.
 * @param $hook (string)(required) Name of the action hook.
 * @param$args (array) Arguments to pass to callbacks when the hook triggers. Default: array().
 * @param $group (string) The group to assign this job to. Default: '’.
 * @param $unique (boolean) Whether the action should be unique. Default: false.
 * @param $priority (integer) Lower values take precedence over higher values. Defaults to 10, with acceptable values falling in the range 0-255.
 */
if ( false === as_next_scheduled_action( 'woocommerce_order_action_clone_order_weekly' ) ) {
    as_schedule_recurring_action( strtotime( '+5 minutes' ), 300, 'woocommerce_order_action_clone_order_weekly', $order);
}

/**
 * Trigger Clone action from  Order page
 *
 * @param $order
 */
function m_trigger_action_clone_order_weekly( $order ) {
    $order_id = $order->get_id();
    mutiara_woocommerce_order_clone($order_id);
}

add_action( 'woocommerce_order_action_clone_order_weekly', 'm_trigger_action_clone_order_weekly', 20, 1 );

不幸的是,发生了错误:enter image description here

我也试图重构到这一点,错误消失了,但是当我单击按钮时,网站上显示错误:“此网站上出现了严重错误。请查看您的站点管理员电子邮件收件箱以获取说明。

require_once( plugin_dir_path( __FILE__ ) . '/libraries/action-scheduler/action-scheduler.php' );
/**
 * Schedule `woocommerce_order_action_clone_order_weekly`
 */
function trigger_recurring_action() {
    if ( false === as_next_scheduled_action( 'woocommerce_order_action_clone_order_weekly' ) ) {
        as_schedule_recurring_action( strtotime( '+5 minutes' ), 300, 'mutiara_woocommerce_order_clone', $order);
    }
}

/**
 * Trigger Clone action from  Order page
 *
 * @param $order
 */
function m_trigger_action_clone_order_weekly( $order ) {
    trigger_recurring_action();
}

add_action( 'woocommerce_order_action_clone_order_weekly', 'm_trigger_action_clone_order_weekly', 20, 1 );

我做错了什么?如何正确使用操作调度程序? 我的目标是管理员选择“每周克隆订单”,并且将自动创建每周订单,直到管理员选择“停止克隆”。

wordpress woocommerce cron

评论

1赞 LoicTheAztec 11/18/2023
作为更好的选择,您应该使用 Action Scheduler 插件,它是一个可扩展、可跟踪的作业队列,用于后台处理......请注意,WooCommerce 和 WooCommerce 订阅包含/使用操作计划程序库。文档在 ActionScheduler.org 上。
0赞 ivanasetiawan 11/18/2023
我安装了Action Scheduler并尝试使用而不是,但似乎没有任何反应:(as_schedule_recurring_action( strtotime( 'tomorrow' ), 60, 'wpm_woocommerce_order_clone');wp_schedule_event(time(), '2min', 'wpm_woocommerce_order_clone', $order_id);
0赞 LoicTheAztec 11/18/2023
为您的问题添加编辑,其中包含您的新代码以及制作可测试示例所需的一切......然后在这里通知我(我会看一下并尝试回答)。
1赞 ivanasetiawan 11/18/2023
我编辑了这个问题,你能帮忙吗?非常感谢!

答: 暂无答案