PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, March 4, 2022

[FIXED] Woocommerce - Trying to get variable product quantity sum in admin

 March 04, 2022     php, sql, woocommerce, wordpress     No comments   

Issue

as in title, i'm trying to get sum of my variable product (for each product) in admin instead of 'out-of-stock' or 'in-stock' indication. I've variable product so i need sum of one variation to get all my product (ive a pant in different size and different colour but i need only sum of all size). Anyone got this before me? Thanks in advance, Francesco


Solution

Finally i found solution. First of all added this

function add_qty_admin( $column ) {
        if (!isset($columns['total_qty']))
        $columns['total_qty'] = "Quantità";
        return $columns;
    }
add_filter( 'manage_posts_columns', 'add_qty_admin' );

to functions.php in order to have a column in my backend.

Then i've added also this

function admin_post_data_row($column_name, $post_id)
{
    global $wpdb;
    switch($column_name)
    {
        case 'total_qty':
            $query = "SELECT sum(meta_value)
                      FROM $wpdb->posts AS p, $wpdb->postmeta AS s
                      WHERE p.post_parent = %d
                      AND p.post_type = 'product_variation'
                      AND p.post_status = 'publish'
                      AND p.id = s.post_id
                      AND s.meta_key = '_stock'";

            $product_qty = $wpdb->get_var($wpdb->prepare($query,$post_id));
            if ($product_qty) echo $product_qty;
            break;

        default:
            break;
    }
}
add_action( 'manage_posts_custom_column', 'admin_post_data_row', 10, 2);

where $query is SQL query that sum all _stock value in postmeta table for post_parent posts that are the variations of post_id.

Sorry for my poor english (i'm italian), i hope this code can be helpful fot other people. Francesco.



Answered By - Francesco Di Candia
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing