コード例 #1
0
 public function getProductAttributes($post_id)
 {
     // update cache if required
     if (!array_key_exists($post_id, $this->product_attributes)) {
         $this->product_attributes[$post_id] = WPLA_ProductWrapper::getAttributes($post_id);
     }
     return $this->product_attributes[$post_id];
 }
コード例 #2
0
 public function ajax_wpla_show_product_matches()
 {
     // TODO: check nonce
     if (isset($_REQUEST['id'])) {
         // $market = WPLA_AmazonMarket::getMarket( $_REQUEST['market_id'] );
         $product = get_product($_REQUEST['id']);
         // echo "<pre>";print_r($product);echo"</pre>";
         if ($product) {
             $product_attributes = WPLA_ProductWrapper::getAttributes($product->id, true);
             $wpl_default_matcher_selection = get_option('wpla_default_matcher_selection', 'title');
             switch ($wpl_default_matcher_selection) {
                 case 'title':
                     # product title
                     $query = $product->post->post_title;
                     break;
                 case 'sku':
                     # product sku
                     $query = $product->sku;
                     break;
                 default:
                     # else check for attributes
                     foreach ($product_attributes as $attribute_label => $attribute_value) {
                         if ($attribute_label == $wpl_default_matcher_selection) {
                             $query = $attribute_value;
                         }
                     }
                     break;
             }
             // echo '<h2>'.$query.'</h2>';
             // fall back to title when query is empty
             if (empty($query)) {
                 $query = $product->post->post_title;
             }
             // handle custom query
             if (isset($_REQUEST['query'])) {
                 $query = trim($_REQUEST['query']);
             }
             $default_account_id = get_option('wpla_default_account_id', 1);
             $account = WPLA_AmazonAccount::getAccount($default_account_id);
             if (!$account) {
                 echo "<br>You need to select a default account to be used for matching products.";
                 exit;
             }
             // get product attributes - if possible from cache
             $transient_key = 'wpla_product_match_results_' . sanitize_key($query);
             $products = get_transient($transient_key);
             if (empty($products)) {
                 // call API
                 $api = new WPLA_AmazonAPI($account->id);
                 $products = $api->listMatchingProducts($query);
                 if (is_array($products)) {
                     // get lowest prices
                     $products = $this->populateMatchesWithLowestPrices($products, $account);
                     // save cache
                     set_transient($transient_key, $products, 300);
                 }
                 // echo "<pre>";print_r($transient_key);echo"</pre>";#die();
             }
             // echo "<pre>AJAX:";print_r($products);echo"</pre>";die();
             // get market / site domain - for "view" links
             $market = new WPLA_AmazonMarket($account->market_id);
             if (is_array($products)) {
                 // load template
                 $tpldata = array('plugin_url' => self::$PLUGIN_URL, 'message' => $this->message, 'query' => $query, 'query_product' => $product, 'query_product_attributes' => $product_attributes, 'products' => $products, 'market_url' => $market->url, 'post_id' => $_REQUEST['id'], 'query_select' => isset($_REQUEST['query_select']) ? $_REQUEST['query_select'] : false, 'form_action' => 'admin.php?page=' . self::ParentMenuId);
                 WPLA()->pages['listings']->display('match_product', $tpldata);
                 // } elseif ( $product->Error->Message ) {
                 // 	$errors  = sprintf( __('There was a problem fetching product details for %s.','wpla'), $product->post->post_title ) .'<br>Error: '. $reports->Error->Message;
             } else {
                 $errors = sprintf(__('There were no products found for query %s.', 'wpla'), $query);
                 echo $errors;
             }
             exit;
         } else {
             echo "invalid product";
         }
     }
 }