将woocommerce链接到asp .net web应用程序

时间:2017-03-04 06:31:51

标签: asp.net sql-server wordpress woocommerce payment-gateway

有没有办法将wordpress网站连接到外部的asp.net网络应用程序?我有一个支持酒店预订的woocommerce平台。但日期选择器&支付网关集成在单独的.NET Web应用程序中完成。我想将woocommerce网站链接到.NET应用程序,并将特定产品详细信息的日期发送到应用程序&在那里进行交易。

是否有人能够指导我连接到.NET应用程序的SQL数据库&将woocommerce产品的数据发送到该特定应用程序?

我在另一个博客中找到了关于如何执行此操作的代码,但我不确定这是否是正确的方法。

/* after an order has been processed, we will use the 'woocommerce_thankyou' hook, to add our function, to send the data */
add_action('woocommerce_thankyou', 'wdm_send_order_to_ext'); 
function wdm_send_order_to_ext( $order_id ){
// get order object and order details
$order = new WC_Order( $order_id ); 
$email = $order->billing_email;
$phone = $order->billing_phone;
$shipping_type = $order->get_shipping_method();
$shipping_cost = $order->get_total_shipping();

// set the address fields
$user_id = $order->user_id;
$address_fields = array('country',
    'title',
    'first_name',
    'last_name',
    'company',
    'address_1',
    'address_2',
    'address_3',
    'address_4',
    'city',
    'state',
    'postcode');

$address = array();
if(is_array($address_fields)){
    foreach($address_fields as $field){
        $address['billing_'.$field] = get_user_meta( $user_id, 'billing_'.$field, true );
        $address['shipping_'.$field] = get_user_meta( $user_id, 'shipping_'.$field, true );
    }
}

// get coupon information (if applicable)
$cps = array();
$cps = $order->get_items( 'coupon' );

$coupon = array();
foreach($cps as $cp){
        // get coupon titles (and additional details if accepted by the API)
        $coupon[] = $cp['name'];
}

// get product details
$items = $order->get_items();

$item_name = array();
$item_qty = array();
$item_price = array();
$item_sku = array();

foreach( $items as $key => $item){
    $item_name[] = $item['name'];
    $item_qty[] = $item['qty'];
    $item_price[] = $item['line_total'];

    $item_id = $item['product_id'];
    $product = new WC_Product($item_id);
    $item_sku[] = $product->get_sku();
}

/* for online payments, send across the transaction ID/key. If the payment is handled offline, you could send across the order key instead */
$transaction_key = get_post_meta( $order_id, '_transaction_id', true );
$transaction_key = empty($transaction_key) ? $_GET['key'] : $transaction_key;   

// set the username and password
$api_username = 'testuser';
$api_password = 'testpass';

// to test out the API, set $api_mode as ‘sandbox’
$api_mode = 'sandbox';
if($api_mode == 'sandbox'){
    // sandbox URL example
    $endpoint = "http://sandbox.example.com/"; 
}
else{
    // production URL example
    $endpoint = "http://example.com/"; 
}

    // setup the data which has to be sent
$data = array(
        'apiuser' => $api_username,
        'apipass' => $api_password,
        'customer_email' => $email,
        'customer_phone' => $phone,
        'bill_firstname' => $address['billing_first_name'],
        'bill_surname' => $address['billing_last_name'],
        'bill_address1' => $address['billing_address_1'],
        'bill_address2' => $address['billing_address_2'],
        'bill_city' => $address['billing_city'],
        'bill_state' => $address['billing_state'],
        'bill_zip' => $address['billing_postcode'],
        'ship_firstname' => $address['shipping_first_name'],
        'ship_surname' => $address['shipping_last_name'],
        'ship_address1' => $address['shipping_address_1'],
        'ship_address2' => $address['shipping_address_2'],
        'ship_city' => $address['shipping_city'],
        'ship_state' => $address['shipping_state'],
        'ship_zip' => $address['shipping_postcode'],
        'shipping_type' => $shipping_type,
        'shipping_cost' => $shipping_cost,
        'item_sku' => implode(',', $item_sku), 
        'item_price' => implode(',', $item_price), 
        'quantity' => implode(',', $item_qty), 
        'transaction_key' => $transaction_key,
        'coupon_code' => implode( ",", $coupon )
    );

        // send API request via cURL
    $ch = curl_init();

    /* set the complete URL, to process the order on the external system. Let’s consider http://example.com/buyitem.php is the URL, which invokes the API */
    curl_setopt($ch, CURLOPT_URL, $endpoint."buyitem.php");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec ($ch);

    curl_close ($ch);

    // the handle response    
    if (strpos($response,'ERROR') !== false) {
            print_r($response);
    } else {
            // success
    }
}

P.S。我在wordpress网站上启用了访客结账,但我已登录该应用程序。该应用程序在外部数据库&应该与之建立联系。

提前致谢!

0 个答案:

没有答案