我希望有人能夠幫助我或指出我朝着正確的方向。我花了一個星期的時間試圖弄清楚如何訪問和更新WP電子商務中的權重字段,並且Ive沒有成功訪問它並更新新的值。訪問和更新序列化數組內的數據的問題
我正在使用的腳本訪問一個網站來刮取漫畫書信息。然後,它解析xml文件中的數據,併爲第二個變量分配WP電子商務特有的值。
整個腳本運行得非常漂亮,爲每本漫畫書創建產品頁面,並將所有數據添加到每個產品中。除了一個!而且我一直堅持了一個星期,試圖找出如何訪問元和更新它。這是用於weight和weight_unit的_wpsc_product_metadata數組以及存儲在其中的其他元值。
漫畫的重量大約爲3盎司,我一直試圖將這個值分配給每張卡片,因爲它創建了「3」和「盎司」 - 我一直沒有成功。
我試過了很多不同的方式,從挖掘到電子商務代碼到在網上查找,閱讀有關多維數組,序列化數組,並且它沒有單擊我的noggin'。請記住,在過去的一個月中,我只使用PHP進行編程,在一個月之前,除了15年前涉足C和C++之外,我對代碼一無所知。那時候,它主要是複製現有的代碼和改變MUDS(多用戶地下城)的輸出 - 當在線遊戲是基於文本的時候,大聲笑。
我認爲我可以通過使用訪問WP電子商務元:
get_post_meta($post_id, '_wpsc_product_metadata', true); That did not work.
實現這一點的其他方法我試過:
get_post_meta(get_the_id($post_id), '_wpsc_product_metadata', true); // no success
我嘗試使用它像這樣特定產品ID:
get_the_id(300), or just '300', i've used single quotes double quotes no qoutes, "{}", etc etc etc etc
現在,當我已經使用:
個get_post_meta($post_id, ''); //while in the loop
的SKU和價格的值輸出但是不知道內_wpsc_product_metadata
是後來,當我使用這個:
$product_data['meta'] = array();
$product_data['meta'] = maybe_unserialize(get_post_meta('$post_id', ''));
這是我所得到的,當我的print_r
Array ([_wpsc_price] => Array ([0] => Array ([0] => 19.99))
[_wpsc_sku] => Array ([0] => Array ([0] => 978-0-7851-5209-5)))
所以我嘗試了一件事,我去了一個特定的產品,我手動設置重量爲5,我改變了代碼如下:
$product_data['meta'] = array();
$product_data['meta'] = maybe_unserialize(get_post_meta('44317', ''));
這是輸出:
Array ([_wpsc_stock] => Array ([0] => Array ([0] =>))
[_wpsc_product_metadata] => Array ([0] => Array ([0] =>
a:19:{s:25:"wpec_taxes_taxable_amount";s:0:"";s:13:"external_link";s:0:
"";s:18:"external_link_text";s:0:"";s:20:"external_link_target";s:0:"";s:6:
"weight";s:3:"0.3";s:11:"weight_unit";s:5:"pound";s:10:"dimensions";a:6:
{s:6:"height";s:1:"0";s:11:"height_unit";s:2:"in";s:5:"width";s:2:"0
";s:10:"width_unit";s:2:"in";s:6:"length";s:1:"0";s:11:"length_unit";
s:2:"in";}s:8:"shipping";a:{s:5:"local";s:1:"0";s:13:"international";s:1:"0";}
s:14:"merchant_notes";s:0:"";s:8:"engraved";s:1:"0";s:23:
"can_have_uploaded_image";s:1:"0";s:15:"enable_comments";s:0:"";
s:24:"unpublish_when_none_left";s:1:"0";s:11:"no_shipping";s:1:"0";s:16:
"quantity_limited";s:1:"0";s:7:"special";s:1:"0";s:17:"display_weight_as";s:5:
"pound";s:16:"table_rate_price";a:2:{s:8:"quantity";a:0:{}s:11:"table_price";a:0:
{}}s:17:"google_prohibited";s:1:"0";})) [_wpsc_special_price] => Array ([0] =>
Array ([0] => 0)) [_edit_last] => Array ([0] => Array ([0] => 1))
[_edit_lock] => Array ([0] => Array ([0] => 1333358836:1)) [_wpsc_sku] =>
Array ([0] => Array ([0] => 978-0-7851-6421-0)) [_wpsc_price] => Array
([0] => Array ([0] => 24.99)) [_wpsc_is_donation] => Array ([0] => Array (
[0] => 0)) [_wpsc_currency] => Array ([0] => Array ([0] => a:0:{})))
我希望,沒有我求有人也許能開導我,並告訴我如何訪問和更新存儲在該數據_wpsc_product_metadata特別是weight和weight_unit,如果你知道怎麼做會更好,你可以給我看一個例子,將新創建的帖子權重更新爲一個值,並將默認weight_unit從磅改爲盎司。
這裏是參考代碼:
<?php
function scraping_comic()
{
$html = file_get_html('http://site-to-strip.com/');
$matches = str_replace (' ', ' ', $article);
foreach($html->find('li.browse_result') as $article)
{
// get comic title
$item['title'] = trim($article->find('h4', 0)->find('span',0)->outertext);
// get comic title url
$item['title_url'] = trim($article->find('h4', 0)->find('a.grid-hidden',0)->href);
// get comic image
$item['image_url'] = trim($article->find('img.main_thumb',0)->src);
// get comic excerpt
$item['excerpt'] = trim($article->find('p.browse_result_description_release', 0)->plaintext);
// get comic sales info
$item['on_sale'] = trim($article->find('.browse_comics_release_dates', 0)->plaintext);
// strip numbers and punctuations
$item['title2'] = trim(preg_replace("/[^A-Za-z-\t\n\s]/","",$article->find('h4',0)->find('span',0)->plaintext));
$item['title3'] = trim(preg_replace("/[^A-Za-z]/","",$article->find('h4',0)->find('span',0)->plaintext));
$ret[] = $item;
}
$html->clear();
unset($html);
return $ret;
}
$ret = scraping_comic();
if (! empty($ret))
{
foreach($ret as $v)
{
//download the image
$url = $v['image_url'];
$title = $v['title3'];
$now = time();
$num = date("w");
if ($num == 0)
{ $sub = 6; }
else { $sub = ($num-1); }
$WeekMon = mktime(0, 0, 0, date("m", $now) , date("d", $now)-$sub, date("Y", $now));
$todayh = getdate($WeekMon);
$d = $todayh[mday];
$m = $todayh[mon];
$y = $todayh[year];
$date_stamp = $d.$m.$y;
//scrape inside pages
$scrape = 'http://domain.com';
$comic_details = $v['title_url'];
$comic_details_url = $scrape.$comic_details;
$url2 = file_get_html($comic_details_url);
foreach($url2->find('.comics_detail_lead_left_panel_content') as $the_details);
$matches = str_replace (' ', ' ', $the_details);
{
$item2['image2_url'] = trim($the_details->find('img.frame-img',0)->src);
$item2['full_desc'] = trim($the_details->find('p',0)->plaintext);
$item2['data'] = trim($the_details->find('dl',0)->plaintext);
}
$url2->clear();
unset($url2);
//download medium-sized image
$root2 = ('/home/****/public_html/wp-content/blogs.dir/14/files/comics/' .$title.$date_stamp. '-medium.jpg');
$image2 = $item2['image2_url'];
copy($image2, $root2);
unset($root2);
unset($image2);
//match specific data and assign variables
$string = $item2['data'];
$number = preg_match("/(Comic|Hardcover|Paperback)[^A-Za-z]+/", $string, $fields);
switch ($fields[1])
{
case ('Comic'):
$cat = array(51, 52);
break;
case ('Hardcover'):
$cat = array(85, 52);
break;
case ('Paperback'):
$cat = array(95, 52);
break;
default: "";
}
$number = preg_match("/((January)|(February)|(March)|(April)|(May)|(June)|(July)|(August)|(September)|(October)|(November)|(December))[^A-Za-z0-9,]+[A-Za-z0-9,\s]+/", $string, $fields);
$date = $fields[0];
$number = preg_match("/((UPC)|(ISBN))[^0-9-]+([0-9-]+)/", $string, $fields);
$upc = $fields[4];
$number = preg_match("/((Price))[^0-9.]+([0-9.\s]+)/", $string, $fields);
$price = $fields[3];
$full_desc = $item2['full_desc'];
$maintitle = $v['title'];
$excerpt = $v['excerpt'];
$comic_post = array();
$comic_post['post_title'] = wp_strip_all_tags($maintitle);
$comic_post['post_content'] = wp_strip_all_tags($full_desc);
$comic_post['post_status'] = 'publish';
$comic_post['post_author'] = 1;
$comic_post['post_type'] = 'wpsc-product';
$comic_post['post_category'] = $cat;
$comic_post['comment_status'] = 'closed';
$comic_post['ping_status'] = 'closed';
$comic_post['post_excerpt'] = wp_strip_all_tags($excerpt);
// create comic book
$post_id = wp_insert_post($comic_post);
// category insertion does not work fixed this by calling wp_set_post_terms
wp_set_post_terms($post_id, $cat, 'wpsc_product_category');
$wpsc_custom = update_post_meta;
$wpsc_custom($post_id, '_wpsc_price', $price);
$wpsc_custom($post_id, '_wpsc_sku', $upc);
//Gain access to WP Ecommerce meta data and assign new values to weight ***IN PROGRESS***
$product_data['meta'] = array();
$product_data['meta'] = maybe_unserialize(get_post_meta($post_id, ''));
//***TESTING***//
echo '<br /> the data <br />';
print_r($product_data['meta']);
//Set featured image for product
$filename = ('comics/' .$title .$date_stamp. '-medium.jpg');
update_post_meta($post_ID, 'image_thumbnail', $imgloc);
set_featured_image($post_id, $filename);
}
}
else { echo 'Could not scrape site!'; }
?>
我忘了提及,我曾經在BBS的日子裏用於播放MajorMUD的腳本。我還記得,有一點看起來很熟悉,我一直在問很多問題。在事實上,我一天只睡4個小時,除了編程,閱讀,學習,吃飯,睡覺休息幾個小時外,什麼都不做。 ...現在讀入Json_decode ....我需要訪問該對象,而它在創建後的循環中,我只有_wpsc_product_metadata的內容顯示,如果我查詢特定的職位「在哪裏」我手動輸入重量。 – 2012-04-02 11:33:22
似乎WP電子商務直到您手動輸入數據纔會分配_wpsc_product_metadata - 然後創建該帖子的表。但我不確定。 – 2012-04-02 11:34:19
我讀過json只有在使用json設置時纔有效,所以我用if(json_decode($ product_data ['meta'])== NULL)運行了一個檢查器。由此產生的錯誤是警告:json_decode()期望參數1是字符串,給定的數組。我需要訪問的元數據在一個數組中,所以我沒有看到這將如何幫助和輸出是不是有效的json。我錯過了什麼嗎? – 2012-04-02 11:57:31