WooCommerce Webhooks Custom / Action not saving

时间:2015-08-31 14:09:21

标签: php wordpress woocommerce webhooks

每当您尝试在webhooks (来自WooCommerce>设置> Webhooks)中设置自定义/操作主题时,只要您更新对webhook的更改,它就会取消设置。 换句话说,它会撤消您的自定义主题并将其返回到主题下拉列表的“选择选项”。

任何帮助都表示赞赏。非常感谢你!

编辑:除了下面的评论之外,我还尝试通过以下过滤器woocommerce_webhook_topic_hooks创建自己的自定义主题,但是,它不会在下拉列表中显示为选项。

以下代码从functions.php开始,与任何WordPress钩子一样。

代码

function custom_woocommerce_webhook_topics( $topic ) {
    $topic['order.refunded'] = array(
        'woocommerce_process_shop_order_meta',
        'woocommerce_api_edit_order',
        'woocommerce_order_edit_status',
        'woocommerce_order_status_changed'
    );

    return $topic;
}

add_filter( 'woocommerce_webhook_topic_hooks', 'custom_woocommerce_webhook_topics', 10, 1 );

编辑2:添加更多上下文

1 个答案:

答案 0 :(得分:2)

我遇到了同样的问题。选择任何内置主题都可以正常工作。但是选择Action并输入任何WooCommerce订阅操作仍然会恢复。我还尝试在同一个文件(wp-content / plugins / woocommerce-subscriptions / includes / class-wcs-webhooks.php)中创建一个新的自定义主题,创建内置主题,镜像1:1的代码对于新的“subscription.paymentcomplete”主题,“坚持”(例如,subscription.created)的主题之一。它出现在下拉列表中,但在保存下拉列表后会恢复为默认的“选择一个选项...”。

//wp-content/plugins/woocommerce-subscriptions/includes/class-wcs-webhooks.php


public static function init() {

    ...
    add_action( 'woocommerce_subscription_created_for_order', __CLASS__ . '::add_subscription_created_callback', 10, 2 );
    add_action( 'woocommerce_subscription_payment_complete', __CLASS__ . '::add_subscription_payment_complete_callback', 10,  );
    ...
}

public static function add_topics( $topic_hooks, $webhook ) {

    if ( 'subscription' == $webhook->get_resource() ) {
        $topic_hooks = apply_filters( 'woocommerce_subscriptions_webhook_topics', array(
            'subscription.created' => array(
                'wcs_api_subscription_created',
                'wcs_webhook_subscription_created',
                'woocommerce_process_shop_subscription_meta',
            ),
            'subscription.paymentcomplete' => array(
                'wcs_webhook_subscription_payment_complete'
                'woocommerce_process_shop_subscription_meta',
            ),
            ...
        ), $webhook );
    }

    return $topic_hooks;
}

public static function add_topics_admin_menu( $topics ) {

    $front_end_topics = array(
        'subscription.created' => __( ' Subscription Created', 'woocommerce-subscriptions' ),
        'subscription.paymentcomplete' => __( ' Subscription Payment Complete', 'woocommerce-subscriptions' ),
        ...
    );

    return array_merge( $topics, $front_end_topics );
}

public static function add_subscription_created_callback( $order, $subscription ) {
    do_action( 'wcs_webhook_subscription_created', $subscription->id );
}

public static function add_subscription_payment_complete_callback( $order, $subscription ) {
    do_action( 'wcs_webhook_subscription_payment_complete', $subscription->id );
}

解决方案是:

add_filter( 'woocommerce_valid_webhook_events', __CLASS__ . '::add_event', 10, 1 );

public static function add_event( $events) {

        $events[] = 'paymentcomplete';

        return $events;
}