function cc_ajax_product_search() { CC_Log::write('AJAX product search: ' . print_r($_REQUEST['search'], true)); $products = CC_Cloud_Product::search($_REQUEST['search']); $options = array(); /* Product info from search results: Array ( [id] => 54d3e70dd2a57d1adc002eb1 [name] => Ooma HD2 Handset [sku] => hd2 [price] => 60.0 [on_sale] => [sale_price] => [currency] => $ [expires_after] => [formatted_price] => $60.00 [formatted_sale_price] => $ [digital] => [type] => product [status] => available ) */ foreach ($products as $p) { CC_Log::write('Product info from search results: ' . print_r($p, true)); $options[] = array('id' => json_encode($p), 'text' => $p['name']); } echo json_encode($options); die; }
/** * Attempt to load all cloud products into self::$products * * @return void */ public function load_all() { $url = self::$cloud->api . 'products'; $headers = array('Accept' => 'application/json'); $response = wp_remote_get($url, self::$cloud->basic_auth_header($headers)); if (!self::$cloud->response_ok($response)) { // CC_Log::write("CC_Cloud_Product::get_products failed: $url :: " . print_r( $response, true ) ); throw new CC_Exception_API("Failed to retrieve products from Cart66 Cloud"); } else { self::$products = json_decode($response['body'], true); CC_Log::write('Called get_products() :: Loaded product data from the cloud'); } }
public function get_search_products($query = "") { $products = new CC_Cloud_Product(); return $products->search($query); }
public function create_post($sku, $content = '', $excerpt = '') { CC_Log::write("Creating post for product sku: {$sku}"); $post_id = null; $search_results = CC_Cloud_Product::search($sku); CC_Log::write("Cloud product search results: " . print_r($search_results, true)); if (is_array($search_results) && count($search_results)) { $product_info = array_shift($search_results); if (is_array($product_info) && count($product_info)) { $slug = sanitize_key(str_replace(' ', '-', strtolower($product_info['name']))); $title = cc_sanitize('name', 'text_field', $product_info); if (null == $this->page_exists($title)) { $post_data = array('comment_status' => 'closed', 'ping_status' => 'closed', 'post_author' => 1, 'post_name' => $slug, 'post_title' => $title, 'post_status' => 'publish', 'post_type' => 'cc_product'); if (!empty($content)) { $post_data['post_content'] = $content; } if (!empty($excerpt)) { $post_data['post_excerpt'] = $excerpt; } CC_Log::write('About to create cart66 product post with this post data: ' . print_r($post_data, true)); $post_id = wp_insert_post($post_data); if ($post_id > 0) { CC_Log::write('Created cart66 product post with id: ' . $post_id . "\nNow adding meta data: " . print_r($product_info, true)); update_post_meta($post_id, $this->json_key, $product_info); foreach ($product_info as $key => $value) { update_post_meta($post_id, $this->prefix . $key, $value); } } else { CC_Log::write('Unable to create cart66 product post: ' . $post_id); } } else { CC_Log::write('Not creating new product post because a post already exists with title: ' . $title); } } } else { CC_Log::write('Unable to retrieve product information for SKU: ' . $sku); } return $post_id; }
public static function product_price($product_sku) { $price = ''; if ($product_sku) { try { $product = new CC_Cloud_Product(); $products = $product->get_products(); foreach ($products as $p) { if ($p['sku'] == $product_sku) { CC_Log::write("Getting price for product: " . print_r($p, TRUE)); $price = $p['on_sale'] == 1 ? $p['formatted_sale_price'] : $p['formatted_price']; } } } catch (CC_Exception_API $e) { $price = "Error: " . $e->getMessage(); } } CC_Log::write("Returning product price for {$product_sku}: {$price}"); return $price; }