API-vendor/docs/实际经验/使用webhook自动同步woocommerce的订单.md

1.6 KiB
Raw Blame History

使用webhook自动同步woocommerce的订单

erp-API 服务非 https 也可用配置

webhook 调用的时候默认是需要 https但开发时本地一般不配置 ssl或者只是自签名所以需要在 wordpress 中添加非 https 也可用配置。 执行:在你应用的主题对应的 functions.php 中添加以下代码可以放在最后即可访问非https地址 备注:路径:比如你应用的主题是 twentytwentyfive,则对应的文件路径是 <siteFolder>/app/public/wp-content/themes/twentytwentyfive/functions.php

add_filter( 'http_request_args', function( $args ) {
    $args['reject_unsafe_urls'] = false;
    return $args;
} );

// 跨平台获取请求头
$headers = [];
foreach ($_SERVER as $key => $value) {
    if (strpos($key, 'HTTP_') === 0) {
        $header_name = str_replace('_', '-', ucwords(substr($key, 5), '-'));
        $headers[$header_name] = $value;
    }
}

// 修正后的条件判断
if ( (isset($_GET['consumer_key']) && isset($_GET['consumer_secret'])) || isset($headers['Authorization']) ) {
    $_SERVER['HTTPS'] = 'on';
    if (!is_ssl()) { // WordPress内置函数检查
        wp_redirect(str_replace('http://', 'https://', $_SERVER['REQUEST_URI']));
        exit;
    }
}

在wordpress管理端网页添加webhook

地址: http://wp-test.local/wp-admin/admin.php?page=wc-settings&tab=advanced&section=webhooks 举例说明: 1760338330047

最终在订单更新时会调用地址,然后进而调用 syncSingleOrder 函数

备注