Woocommerce CRM 连接

问题描述 投票:0回答:1

我正在尝试将我的 Woocommerce 连接到 CRM。他们给了我这个示例代码。 我的问题: 我需要在 Woocommerce 中的哪个文件中添加此代码。

class SimplicateApi {

    public $authentication_key;
    public $authentication_secret;
    public $api_url;

    public function __construct($domain, $key, $secret){

        $this->authentication_key = $key;
        $this->authentication_secret = $secret;
        $this->api_url  = 'https://'.$domain.'/api/v2';
        }

    public function makeApiCall($method, $url, $payload = NULL) {
        // Generate the list of headers to always send.
        $headers = array(
                "User-Agent: simplicate-koppeling",// Sending a User-Agent header is a best practice.
                "Authentication-Key: ".$this->authentication_key,
                "Authentication-Secret: ".$this->authentication_secret,
                "Accept: application/json",             // Always accept JSON response.
        );

        $endpoint = $this->api_url . $url;

        $curl = curl_init($endpoint);

        switch(strtoupper($method)) {
            case "GET":
                // Nothing to do, GET is the default and needs no
                // extra headers.
                break;
            case "POST":
                // Add a Content-Type header (IMPORTANT!)
                $headers[] = "Content-Type: application/json";
                curl_setopt($curl, CURLOPT_POST, true);
                curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
                break;
            case "PATCH":
                // Add a Content-Type header (IMPORTANT!)
                $headers[] = "Content-Type: application/json";
                curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PATCH");
                curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
                break;
            case "DELETE":
                curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
                break;
            default:
                exit;
        }

        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
        $response = curl_exec($curl);

        $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);

        if (self::isFailure($httpCode)) {
            return array('errorNumber' => $httpCode,
                    'error' => 'Request  returned HTTP error '.$httpCode,
                    'request_url' => $url);
        }

        $curl_errno = curl_errno($curl);
        $curl_err = curl_error($curl);

        if ($curl_errno) {
            $msg = $curl_errno.": ".$curl_err;
            curl_close($curl);
            return array('errorNumber' => $curl_errno,
                    'error' => $msg);
        }
        else {
            error_log("Response: ".$response);
            curl_close($curl);
            return json_decode($response, true);


        }
    }

    public static function isFailure($httpStatus){
        // Simplistic check for failure HTTP status
        return ($httpStatus >= 400);
    }
}



$SimplicateApi = new SimplicateApi('yourdomain.simplicate.nl','yourapikey','yourapisecret');

// pepare the payload to create an organization
$org_payload = array(
        'name' => $variable_with_organization_name,
        'phone' => $variable_with_organization_phone,
        'email' => $variable_with_organization_email,
        'note' => $variable_with_note,
        'relation_type' => array(
            'id'=>'' //provide the relationtypeid, f.e. relationtype:796ce0d318a2f5db515efc18bba82b90
        ),
        'visiting_address' => array(
            'country_code'          =>  'NL'
        ), // can be extented with other address data
        'postal_address' => array(
            'country_code'          =>  'NL'
        ) // can be extented with other address data
);

// add the organization to the CRM
$organization = $SimplicateApi->makeApiCall('POST','/crm/organization',json_encode($org_payload));

还有一个问题。 这部分怎么样:

// pepare the payload to create an organization
$org_payload = array(
        'name' => $variable_with_organization_name,
        'phone' => $variable_with_organization_phone,
        'email' => $variable_with_organization_email,
        'note' => $variable_with_note,

我从哪里可以从 woocommerce 获取这些变量?

php woocommerce
1个回答
1
投票

您需要添加自定义 api 路由。

第一个技巧:

在 woocommerce-ac.php 中添加:

add_action('woocommerce_api_loaded', 'wpc_register_wp_api_endpoints');
function wpc_register_wp_api_endpoints() {
    include_once('woo-includes/api/v2/class-wc-api-carts.php');
    add_filter('woocommerce_api_classes', 'filter_woocommerce_api_classes', 10, 1);
}
function filter_woocommerce_api_classes($array) {
    $cart = array('WC_API_Carts');
    return array_merge($array, $cart);
}

并在 woo-includes/api/v2 中添加 class-wc-api-customs.php;

就这样完成了 完整详情

© www.soinside.com 2019 - 2024. All rights reserved.