echo custom data from mysql table on product page woocommerce

Viewed 247

Dears,

I've created a python script that exports data from MSSQL and imports into the MySQL database. Everything works smoothly.

Now I want to display the new column data which I've added... but I don't have any PHP skills.

The data is in wc_product_meta_lookup table. The column name is dLieferdatum. The content of dLieferdatum is the estimated date of availability of an item.

I would highly appreciate any idea how to display this on my product :-)

Further note: I already built a function that displays if an item is available or not available. I want to display the data from dLieferdatum instead of "bestellbar" if dLieferdatum is not NULL

function show_stock() {
    global $product;
        // if manage stock is enabled

        if ( $product->stock ) {  

            // if stock is low
            if ( number_format($product->stock,0,'','') > 0 && number_format($product->stock,0,'','') < 7) { 
                echo '<div class="less-available"><i class="fa fa-truck"></i> auf Lager</div>';
                echo '<div class="ind_individual-delivery-time">Zustellung in 1 - 3 Werktagen</div>'; 
            }        

            // if more than 6 are available
            if ( number_format($product->stock,0,'','') > 0 && number_format($product->stock,0,'','') > 6) { 
                echo '<div class="available"><i class="fa fa-truck"></i> auf Lager</div>'; 
                echo '<div class="ind_individual-delivery-time">Zustellung in 1 - 3 Werktagen</div>'; 
            }
        }

        if ( $product->managing_stock() && number_format($product->stock,0,'','') < 1) {
            echo '<div class="backorder_item"><i class="fa fa-truck"></i> bestellbar</div>';            
        }

    }
1 Answers

1) First have you tried using WC_Data get_meta() method on the WC_Product Object, like:

$dLieferdatum = $product->get_meta( "dLieferdatum" );

Not sure that it will work.


2) You can use also WPDB Class to make a simple SQL query on wp_wc_product_meta_lookup table and embed that dynamic query on a custom function as follow:

function get_product_meta_dLieferdatum( $product_id ) {
    global $wpdb;
    return $wpdb->get_var( $wpdb->prepare("
        SELECT dLieferdatum
        FROM {$wpdb->prefix}wc_product_meta_lookup
        WHERE product_id = '%d'
    ", $product_id ) );
}

USAGE for a dynamic product id from $product_id variable:

 $dLieferdatum = get_product_meta_dLieferdatum( $product->get_id() );

It should work.

Note: You should really avoid making changes on default Wordpress and Woocommerce database tables structure. Instead you should add "dLieferdatum" as custom product meta data or add a custom table to the database.

Related