public function getProductAttributes($post_id) { // update cache if required if (!array_key_exists($post_id, $this->product_attributes)) { $this->product_attributes[$post_id] = ProductWrapper::getAttributes($post_id); } return $this->product_attributes[$post_id]; }
public function ajax_wple_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 = ProductWrapper::getAttributes($product->id, true); $wpl_default_matcher_selection = get_option('wple_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']); } // get product attributes - if possible from cache $transient_key = 'wple_product_match_results_' . sanitize_key($query); $products = get_transient($transient_key); if (empty($products)) { // call API $this->initEC(); $products = $this->EC->callFindProducts($query); $this->EC->closeEbay(); if (is_array($products)) { // save cache set_transient($transient_key, $products, 300); } // echo "<pre>";print_r($transient_key);echo"</pre>";#die(); } // echo "<pre>products: ";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, 'post_id' => $_REQUEST['id'], 'query_select' => isset($_REQUEST['query_select']) ? $_REQUEST['query_select'] : false, 'form_action' => 'admin.php?page=' . self::ParentMenuId); WPLE()->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"; } } }
public function processAttributeShortcodes($post_id, $tpl_html, $max_length = false) { // check for attribute shortcodes if (preg_match_all("/\\[\\[attribute_(.*)\\]\\]/uUsm", $tpl_html, $matches)) { // attribute shortcodes i.e. [[attribute_Brand]] $product_attributes = ProductWrapper::getAttributes($post_id); WPLE()->logger->debug('processAttributeShortcodes() - product_attributes: ' . print_r($product_attributes, 1)); // parent attribute for split child variations $parent_post_id = ProductWrapper::getVariationParent($post_id); $parent_attributes = $parent_post_id ? ProductWrapper::getAttributes($parent_post_id) : array(); WPLE()->logger->debug('processAttributeShortcodes() - parent_attributes: ' . print_r($parent_attributes, 1)); // process each found shortcode foreach ($matches[1] as $attribute) { // check product and parent attributes if (isset($product_attributes[$attribute])) { $attribute_value = $product_attributes[$attribute]; } elseif (isset($parent_attributes[$attribute])) { $attribute_value = $parent_attributes[$attribute]; } else { $attribute_value = ''; } // format multiple attribute values $attribute_value = str_replace('|', '<br/>', $attribute_value); // replace placeholder $processed_html = str_replace('[[attribute_' . $attribute . ']]', $attribute_value, $tpl_html); // check if string exceeds max_length after processing shortcode if ($max_length && $this->mb_strlen($processed_html) > $max_length) { $attribute_value = ''; $processed_html = str_replace('[[attribute_' . $attribute . ']]', $attribute_value, $tpl_html); } $tpl_html = $processed_html; } } return $tpl_html; }
public function buildItemSpecifics($id, $item, $listing, $post_id) { // new ItemSpecifics $ItemSpecifics = new NameValueListArrayType(); // get listing data // $listing = ListingsModel::getItem( $id ); // get product attributes $processed_attributes = array(); $attributes = ProductWrapper::getAttributes($post_id); WPLE()->logger->info('product attributes: ' . (sizeof($attributes) > 0 ? print_r($attributes, 1) : '-- empty --')); // apply item specifics from profile $specifics = $listing['profile_data']['details']['item_specifics']; // merge item specifics from product $product_specifics = get_post_meta($post_id, '_ebay_item_specifics', true); if (!empty($product_specifics)) { $specifics = array_merge($specifics, $product_specifics); } // WPLE()->logger->info('item_specifics: '.print_r($specifics,1)); // WPLE()->logger->debug('get variationAttributes: '.print_r($this->variationAttributes,1)); // WPLE()->logger->debug('get variationSplitAttributes: '.print_r($this->variationSplitAttributes,1)); foreach ($specifics as $spec) { if ($spec['value'] != '') { // fixed value $value = $spec['value']; $value = html_entity_decode($value, ENT_QUOTES); if ($this->mb_strlen($value) > 50) { continue; } $NameValueList = new NameValueListType(); $NameValueList->setName($spec['name']); $NameValueList->setValue($value); if (!in_array($spec['name'], $this->variationAttributes)) { $ItemSpecifics->addNameValueList($NameValueList); $processed_attributes[] = $spec['name']; WPLE()->logger->info("specs: added custom value: {$spec['name']} - {$value}"); } } elseif ($spec['attribute'] != '') { // pull value from product attribute $value = isset($attributes[$spec['attribute']]) ? $attributes[$spec['attribute']] : ''; $value = html_entity_decode($value, ENT_QUOTES); // process custom attributes $custom_attributes = apply_filters('wplister_custom_attributes', array()); foreach ($custom_attributes as $attrib) { if ($spec['attribute'] == $attrib['id']) { // pull value from attribute if (isset($attrib['meta_key'])) { $value = get_post_meta($post_id, $attrib['meta_key'], true); // for split variations, check for value on variation level if ($post_id != $listing['post_id']) { $variation_value = get_post_meta($listing['post_id'], $attrib['meta_key'], true); if ($variation_value) { $value = $variation_value; } // WPLE()->logger->info("specs: variation_value for: {$spec['name']} - " . $variation_value ); } } // set fixed value (since 2.0.9.5) if (isset($attrib['value'])) { $value = $attrib['value']; } // use callback (since 2.0.9.6) if (isset($attrib['callback']) && is_callable($attrib['callback'])) { $value = call_user_func($attrib['callback'], $post_id, $id); } } } // if ( '_sku' == $spec['attribute'] ) $value = ProductWrapper::getSKU( $post_id ); // handle variation attributes for a single split variation // instead of listing all values, use the correct attribute value from variationSplitAttributes if (array_key_exists($spec['attribute'], $this->variationSplitAttributes)) { $value = $this->variationSplitAttributes[$spec['attribute']]; } // skip empty values if (!$value) { WPLE()->logger->info("specs: skipped empty product attribute: {$spec['name']} - " . $value); continue; } $NameValueList = new NameValueListType(); $NameValueList->setName($spec['name']); // $NameValueList->setValue( $value ); // support for multi value attributes // $value = 'blue|red|green'; $values = explode('|', $value); foreach ($values as $value) { if ($this->mb_strlen($value) > 50) { continue; } $NameValueList->addValue($value); } if (!in_array($spec['name'], $this->variationAttributes)) { $ItemSpecifics->addNameValueList($NameValueList); $processed_attributes[] = $spec['attribute']; WPLE()->logger->info("specs: added product attribute: {$spec['name']} - " . join(', ', $values)); } } } // skip if item has no attributes // if ( count($attributes) == 0 ) return $item; // get excluded attributes and merge with processed attributes $excluded_attributes = $this->getExcludedAttributes(); $processed_attributes = array_merge($processed_attributes, $excluded_attributes); $convert_attributes_mode = get_option('wplister_convert_attributes_mode', 'all'); // add ItemSpecifics from product attributes - if enabled foreach ($attributes as $name => $value) { $value = html_entity_decode($value, ENT_QUOTES); if ($this->mb_strlen($value) > 50) { continue; } if ($convert_attributes_mode == 'none') { continue; } // handle variation attributes for a single split variation // instead of listing all values, use the correct attribute value from variationSplitAttributes if (array_key_exists($name, $this->variationSplitAttributes)) { $value = $this->variationSplitAttributes[$name]; } $NameValueList = new NameValueListType(); $NameValueList->setName($name); // support for multi value attributes // $value = 'blue|red|green'; $values = explode('|', $value); foreach ($values as $value) { $NameValueList->addValue($value); if ($convert_attributes_mode == 'single') { break; } // only use first value in 'single' mode } // only add attribute to ItemSpecifics if not already present in variations or processed attributes if (!in_array($name, $this->variationAttributes) && !in_array($name, $processed_attributes)) { $ItemSpecifics->addNameValueList($NameValueList); WPLE()->logger->info("attrib: added product attribute: {$name} - " . join(', ', $values)); } } if (count($ItemSpecifics) > 0) { $item->setItemSpecifics($ItemSpecifics); WPLE()->logger->info(count($ItemSpecifics) . " item specifics were added."); } return $item; }