コード例 #1
0
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;
}
コード例 #2
0
 public function get_search_products($query = "")
 {
     $products = new CC_Cloud_Product();
     return $products->search($query);
 }
コード例 #3
0
 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;
 }