Woocommerce call url (after complete order)

I have 3 questions (need help):

  1. I do not know, how to run this plugin (gives me fatal error) please check my script (I am beginner)
  2. Need help with admin page to set up APIkey and choose language for call url http://xxx.CZ or http://xxx.SK (This page is not scripted yet)
  3. How to add my plugin admin page to woocommerce admin page?

This plugin is for Woocommerce. It is supposed to call specific URL (http://heureka.cz/or .sk/dotaznik/"Clients API set up in admin page in woocommerce"/"Customers email"/"Order ID"/"bought Products ID"/) when customers order si complete.

Read More

I am beginner in PHP and WordPress. Thank you all for helping me.

CODE:

<?php
/*
Plugin Name: Overené zákazníkmi Heureka
Plugin URI: http://www.podujatie.eu
Version: 0.1
Description: 
Author: Podujatie.eu, Ing. Igor Kóňa
Tested up to: 3.6
Author URI: http://www.podujatie.eu
Text Domain: woocommerce-new-badge
License: GNU General Public License v3.0
License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
/**
 * Check if WooCommerce is active
 **/
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {

    if ( ! class_exists( 'WC_HO' ) ) {

        class WC_HO {

        function heurekaovereno( $order_id ) {     
        error_log( "Order complete for order $order_id", 0 ); } 
        add_action( 'woocommerce_order_status_completed', 'heurekaovereno' );

    // order object (optional but handy)
    $order = new WC_Order( $order_id );

    // do some stuff here
        private function sendRequest($url)
        {
        $parsed = parse_url($url);
        $fp = fsockopen($parsed['host'], 80, $errno, $errstr, 5);
        if (!$fp) {
            throw new HeurekaOverenoException($errstr . ' (' . $errno . ')');
            } else {
            $return = '';
            $out = "GET " . $parsed['path'] . "?" . $parsed['query'] . " HTTP/1.1rn" .
                    "Host: " . $parsed['host'] . "rn" .
                    "Connection: Closernrn";
            fputs($fp, $out);
            while (!feof($fp)) {
                $return .= fgets($fp, 128);
            }
            fclose($fp);
            $returnParsed = explode("rnrn", $return);

            return empty($returnParsed[1]) ? '' : trim($returnParsed[1]);
            }
        }
        /**
     * Sends request to Heureka Overeno service and checks for valid response
     * 
     * @return boolean true
     */
    public function send()
        {
        if (empty($this->email)) {
            throw new HeurekaOverenoException('Customer email address not set');
        }

        // create URL
        $url = $this->getUrl() . '?id=' . $this->apiKey . '&email=' . urlencode($this->email);
        foreach ($this->products as $product) {
            $url .= '&produkt[]=' . urlencode($product);
            }
        foreach ($this->productsItemId as $itemId) {
            $url .= '&itemId[]=' . urlencode($itemId);
            }

        // add order ID
        if (isset($this->orderId)) {
            $url .= '&orderid=' . urlencode($this->orderId);
            }

        // send request and check for valid response
        $contents = $this->sendRequest($url);
        if ($contents == FALSE) {
            throw new HeurekaOverenoException('Unable to create HTTP request to Heureka Overeno service');
            } elseif ($contents == self::RESPONSE_OK) {
            return TRUE;
            } else {
            throw new HeurekaOverenoException($contents);
            }
        }
        /**
     * Adds ordered products using item ID
     *
     * @param string $itemId Ordered product item ID
     */
    public function addProductItemId($itemId)
    {
        $this->productsItemId[] = $itemId;
    }
        /**
     * Adds ordered products using name
     * 
     * Products names should be provided in UTF-8 encoding. The service can handle
     * WINDOWS-1250 and ISO-8859-2 if necessary
     *
     * @param string $productName Ordered product name
     */
    public function addProduct($productName)
    {
        $this->products[] = $productName;
    }
        /**
     * Heureka endpoint URL
     *
     * @var string     
     */
    const BASE_URL = 'http://www.heureka.cz/direct/dotaznik/objednavka.php';
    const BASE_URL_SK = 'http://www.heureka.sk/direct/dotaznik/objednavka.php';

    /**
     * Language IDs
     *
     * @var int     
     */
    const LANGUAGE_CZ = 1;
    const LANGUAGE_SK = 2;
    /**
     * Valid response value
     *
     * @var string     
     */
    const RESPONSE_OK = 'ok';

    /**
     * Shop API key
     *
     * @var string     
     */
    private $apiKey;

    /**
     * Customer email
     *
     * @var string     
     */
    private $email;

    /**
     * Ordered products
     *
     * @var array     
     */
    private $products = array();

    /**
     * Order ID
     *
     * @var int    
     */
    private $orderId;

    /**
     * Current language identifier
     *
     * @var int     
     */
    private $languageId = 1;

    /**
     * Ordered products provided using item ID
     * 
     * @var array
     */
    private $productsItemId = array();

    /**
     * Initialize Heureka Overeno service 
     *
     * @param string $apiKey Shop API key
     * @param int $languageId Language version settings
     */
    public function __construct($apiKey, $languageId = self::LANGUAGE_CZ)
    {
        $this->setApiKey($apiKey);
        $this->languageId = $languageId;
    }

    /**
     * Sets API key and check well-formedness
     * 
     * @param string $apiKey Shop api key
     */
    public function setApiKey($apiKey)
    {
        if (preg_match('(^[0-9abcdef]{32}$)', $apiKey)) {
            $this->apiKey = $apiKey;
        } else {
            throw new OverflowException('Api key ' . $apiKey . ' is invalid.');
        }
    }

    /**
     * Sets customer email
     *
     * @param string $email Customer email address
     */
    public function setEmail($email)
    {
        $this->email = $email;
    }

    // Default options
    add_option( 'wc_nb_newness', '30' );


    // Admin
    add_action( 'woocommerce_settings_image_options_after', array( $this, 'admin_settings' ), 20);
    add_action( 'woocommerce_update_options_catalog', array( $this, 'save_admin_settings' ) );



            /*-----------------------------------------------------------------------------------*/
            /* Class Functions */
            /*-----------------------------------------------------------------------------------*/

            // Load the settings
            function admin_settings() {
                woocommerce_admin_fields( $this->settings );
            }


            // Save the settings
            function save_admin_settings() {
                woocommerce_update_options( $this->settings );
            }

        if (!isset($wpdb)) $wpdb = $GLOBALS['wpdb'];

        $heurekaovereno_ver = '1.00';

        $WC_HO = new WC_HO();
        }
    }
}
?>

Related posts

Leave a Reply

1 comment