{"id":501,"date":"2012-01-11T13:32:00","date_gmt":"2012-01-11T13:32:00","guid":{"rendered":"http:\/\/www.sitekickr.com\/blog\/?p=501"},"modified":"2012-01-11T13:32:00","modified_gmt":"2012-01-11T13:32:00","slug":"wp-e-commerce-product-meta-data","status":"publish","type":"post","link":"https:\/\/www.sitekickr.com\/blog\/wp-e-commerce-product-meta-data\/","title":{"rendered":"WP E-Commerce product meta data"},"content":{"rendered":"<p>If you&#39;ve been involved in a moderately custom e-commerce store based on the WP-Ecommerce WordPress plugin, you&#39;ve probably come across the need to display product meta data (dimensions, weight, links, etc.).<\/p>\n<p>You may have already decided that the easiest way is to draw this information direct from the database. After some digging, you discover that this information is located in the <em>wp_postmeta<\/em> table. Okay, makes sense. The products, after all, are a WordPress content type.<\/p>\n<p>After digging a little deeper, you discover that all product metadata is &quot;merged&quot; not only into one table record, but one field! You&#39;re wondering how this semi-colon and color delimitted list somehow translates to metadata. If you have never serialized an Array in PHP, this would naturally puzzle you.<\/p>\n<p>This value is actually a serialized Array of product data:<\/p>\n<p><code>a:19:{s:25:&quot;wpec_taxes_taxable_amount&quot;;s:0:&quot;&quot;;s:13:&quot;external_link&quot;;s:0:&quot;&quot;;s:18:&quot;external_link_text&quot;;s:0:&quot;&quot;;s:20:&quot;external_link_target&quot;;s:0:&quot;&quot;;s:6:&quot;weight&quot;;s:1:&quot;1&quot;;s:11:&quot;weight_unit&quot;;s:5:&quot;pound&quot;;s:10:&quot;dimensions&quot;;a:6:{s:6:&quot;height&quot;;s:1:&quot;8&quot;;s:11:&quot;height_unit&quot;;s:2:&quot;in&quot;;s:5:&quot;width&quot;;s:2:&quot;4 &quot;;s:10:&quot;width_unit&quot;;s:2:&quot;in&quot;;s:6:&quot;length&quot;;s:1:&quot;3&quot;;s:11:&quot;length_unit&quot;;s:2:&quot;in&quot;;}s:8:&quot;shipping&quot;;a:2:{s:5:&quot;local&quot;;s:1:&quot;0&quot;;s:13:&quot;international&quot;;s:1:&quot;0&quot;;}s:14:&quot;merchant_notes&quot;;s:0:&quot;&quot;;s:8:&quot;engraved&quot;;s:1:&quot;0&quot;;s:23:&quot;can_have_uploaded_image&quot;;s:1:&quot;0&quot;;s:15:&quot;enable_comments&quot;;s:0:&quot;&quot;;s:24:&quot;unpublish_when_none_left&quot;;s:1:&quot;0&quot;;s:11:&quot;no_shipping&quot;;s:1:&quot;0&quot;;s:16:&quot;quantity_limited&quot;;s:1:&quot;0&quot;;s:7:&quot;special&quot;;s:1:&quot;0&quot;;s:17:&quot;display_weight_as&quot;;s:5:&quot;pound&quot;;s:16:&quot;table_rate_price&quot;;a:2:{s:8:&quot;quantity&quot;;a:0:{}s:11:&quot;table_price&quot;;a:0:{}}s:17:&quot;google_prohibited&quot;;s:1:&quot;0&quot;;}<br \/>\n\t<\/code><\/p>\n<p>By using the <strong>unserialize()<\/strong> function, we are able to convert this value back into an Array, making it much easier to grab individual pieces of metadata.<\/p>\n<p>The following query might fill in a few gaps as well:<\/p>\n<p><code>$cartsql = $wpdb-&gt;prepare( &quot;SELECT cart.*, meta.meta_value <br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; FROM &quot; . WPSC_TABLE_CART_CONTENTS . &quot; cart<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; INNER JOIN wp_postmeta meta<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ON meta.post_id = cart.prodid <br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WHERE meta_key = &#39;_wpsc_product_metadata&#39;&quot;);<br \/>\n\t<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you&#39;ve been involved in a moderately custom e-commerce store based on the WP-Ecommerce WordPress plugin, you&#39;ve probably come across the need to display product&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"amp_status":""},"categories":[16,103],"tags":[135],"_links":{"self":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/501"}],"collection":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/comments?post=501"}],"version-history":[{"count":1,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/501\/revisions"}],"predecessor-version":[{"id":502,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/501\/revisions\/502"}],"wp:attachment":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/media?parent=501"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/categories?post=501"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/tags?post=501"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}