Home / eCommerce / WooCommerce to Google Sheets schaahi store
Duplicate Snippet

Embed Snippet on Your Site

WooCommerce to Google Sheets schaahi store

Code Preview
php
<?php
add_action('woocommerce_order_status_completed', 'send_order_to_google_sheets', 10, 1);
function send_order_to_google_sheets($order_id) {
    $order = wc_get_order($order_id);
    if (!$order) return;
    $line_items = $order->get_items();
    $product_ids = array();
    $product_names = array();
    foreach ($line_items as $item) {
        $product_ids[] = $item->get_product_id();
        $product_names[] = $item->get_name();
    }
    $data = array(
        'id'            => $order->get_id(),
        'date_created'  => $order->get_date_created()->date('Y-m-d H:i:s'),
        'status'        => $order->get_status(),
        'total'         => $order->get_total(),
        'billing'       => array(
            'first_name' => $order->get_billing_first_name(),
            'last_name'  => $order->get_billing_last_name(),
            'phone'      => $order->get_billing_phone(),
            'email'      => $order->get_billing_email()
        ),
        'line_items'    => array(
            array(
                'product_id' => implode(', ', $product_ids),
                'name'       => implode(', ', $product_names)
            )
        ),
        'coupon_lines'  => array()
    );
    $url = 'https://script.google.com/macros/s/AKfycbzUOHwMlrb-QIRlBStxPHLawuJURS0hepfFG8LqkVhjSeBraFkCwHcdxzKsfUxCiCuRGw/exec';
    wp_remote_post($url, array(
        'method'  => 'POST',
        'headers' => array('Content-Type' => 'application/json'),
        'body'    => json_encode($data),
        'timeout' => 15
    ));
}

Comments

Add a Comment