Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get product attributes from wordpress database

Writing custom code to create product detail page with wordpress database.

I have displayed product title, desc, price, stock, etc and got stuck up with product attributes. In the database, _product_attributes is stored in serialized manner in wp_postmeta table in database. And i couldn't unserailize attributes from it. But i found, each attribute value with it own price has been stored in wp_postmeta in some other post_id.

for example, product with post_id=55 has attribute name 'Size value' having values 14 and 18 and price 300 and 350, is displayed as attributes value and price in post_id=110,111

wp_postmetawp-postmeta-example

is there any formula behind? Any idea to find this product attribute value and corresponding price value?

like image 680
Priyanka Avatar asked Feb 26 '14 07:02

Priyanka


People also ask

How do I find product attributes in WordPress?

Add global attributes to productGo to: Products > Add Product (or edit an existing one). Select the Attributes tab in the Product Data. There you can choose any of the attributes that you've created in the dropdown menu. Select Add.

Where are WooCommerce product attributes stored?

WooCommerce attributes are stored in the database as taxonomies. This makes it easy for you to organize your data and display it on your product pages. You can use attributes to filter products, and they will be displayed on the product page.

How do I get all attributes in WooCommerce?

Access All Product Attribute's And Their Data global $product; // Get all attributes and their data $attributes = $product->get_attributes(); foreach ( $attributes as $attribute ) { if ( is_object($attribute) ) { // Array of attribute data $attribute_data = $attribute->get_data(); // Do what you need to do... } }

How do I get attribute value in WooCommerce?

You can do this by using the WC_Product::get_instance() method. Once you have the product object, you can use the WC_Product::get_attribute() method to get an attribute value.


2 Answers

I took a little bit of a different approach, I created a stored procedure in my database that will return all terms associated with a woocommerce product. I decided to go this route because i can call the procedure from my wordpress site and the desktop app I am creating without having to write the function in two different languages.

Though I'd post it here for others to use.

CREATE DEFINER=`database_name_here`@`%` PROCEDURE `get_product_attributes`(IN ProductName TEXT)
BEGIN
SELECT DISTINCT
     p.post_title AS 'Product Name',
     t.name AS 'Term Name',
     tt.taxonomy AS 'Term Type',
     tt.description AS 'Term Description'
FROM
     wp_posts AS p
INNER JOIN
     wp_term_relationships AS tr ON p.id = tr.object_id
INNER JOIN
     wp_term_taxonomy AS tt ON tt.term_taxonomy_id = tr.term_taxonomy_id
INNER JOIN
     wp_terms AS t ON t.term_id = tt.term_id
WHERE
     p.post_title= ProductName
AND
     p.post_type = 'product';
END
like image 98
Fütemire Avatar answered Sep 25 '22 15:09

Fütemire


Product attributes are stored in two locations - in wp_terms, wp_term_taxonomy and wp_term_relationships (that's the first place - each attribute is preceded by pa_ for its taxonomy name - e.g. if you have a color attribute, it's under pa_color) then also as a PHP serialized array in wp_postmeta under '_product_attributes' meta_key.

You can find the method to construct the seriliazed attributes array here:

https://github.com/woothemes/woocommerce/blob/master/includes/class-wc-ajax.php

Look for function save_attributes() and add_attribute to see how the serialized array is constructed.

Update: Later versions of wooCommerce also have a serialized array in wp_options under the _transient_wc_attribute_taxonomies key and a new table called wp_woocommerce_attribute_taxonomies.

like image 22
Dave Hilditch Avatar answered Sep 22 '22 15:09

Dave Hilditch