how to get product attributes from wordpress database

末鹿安然 提交于 2019-11-30 05:32:01

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.

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

$args = array( 'post_type' => 'product','' ); $products = get_posts( $args );

     foreach ($products as $product) {
        $data = get_post_meta($product->ID);            
        $pr['regular_price'] = $data['_regular_price']['0'];
        $pr['sale_price'] = $data['_sale_price']['0'];
Pallavi Jain
$args = array( 'post_type' => 'product','' ); $products = get_posts( $args );

 foreach ($products as $product) {
    $data = get_post_meta($product->ID);            
    $pr['regular_price'] = $data['_regular_price']['0'];
    $pr['sale_price'] = $data['_sale_price']['0'];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!