Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get product weight from Wordpress

The code below is used to get data on a products of the category "books" in my Wordpress website using Woocommerce.

<?php
 $args = array( 'post_type' => 'product', 'posts_per_page' => 200, 'product_cat' => 'books');
       $loop = new WP_Query( $args );


    $send_array = array();
       while ( $loop->have_posts() ) : $loop->the_post(); 
        global $product; 

    $send_array[] = array(

        'id' => get_the_ID(),
        'title' => get_the_title(),
        'content' => get_the_content(),
        'regular_price' => get_post_meta( get_the_ID(), '_regular_price', true),
        'sale_price'=> get_post_meta( get_the_ID(), '_sale_price', true),
        'weight' => woocommerce_get_product_terms($product->id, 'weight', 'names')

    );

     endwhile; 

     wp_reset_query(); 
        ob_clean();
        echo json_encode($send_array);
        exit();

    ?>

This works fine and returns the data correctly except for the weight attribute.

This is a custom attribute set in my website using Woocommerce. In my database the weight attribute shows up in the wp_woocommerce_attribute_taxonomies table. This table has the fields attribute_id, attribute_name, attribute_label etc.

The code I used above to get the weight value of the product doesn't work.

I also tried 'weight' => get_post_meta( get_the_ID(), '_weight', true), but it shows up as an empty string (weight:"") even though that product has a Weight value on the website.

How do I get the weight for each product and add it to the array to the key weight as I have attempted above.

Have been doing some research and this seemed to be the item to work. What am I doing wrong?

How do I fix this?

like image 270
Tester Avatar asked Feb 10 '14 03:02

Tester


People also ask

How do I find product weight in WooCommerce?

To display WooCommerce product weight on archive pages, below the title of the product, add this code to your child theme's functions. php file or via a plugin that allows custom functions to be added, such as the Code snippets plugin. Please don't add custom code directly to your parent theme's functions.

How do you add weight to Wordpress?

Go to WooCommerce -> Settings and click on the "Catalog" tab. Then under the "Product Data" section check the boxes next to "Enable the weight field for products" and "Enable the dimension fields for products".

How do I add a weight unit in WooCommerce?

The built-in weight converter is located in WooCommerce > Settings > General. Scroll down to the 'Weight Unit' setting and select the unit you want to use from the dropdown. If you want to use a plugin, there are a few options available. One is the Weight and Dimensions for WooCommerce plugin.

How do I change unit weight in Wordpress?

Solution 1: by the built-in optionGo to the product Edit screen -> Product Data section -> Shipping tab and enter the product weight. If you want to change the weight unit for all WooCommerce products, go to WooCommerce -> Settings -> Products -> General tab.


1 Answers

global $product;    
$product->get_weight();
like image 53
Taras Kyryliuk Avatar answered Sep 28 '22 20:09

Taras Kyryliuk