How can I echo out an Item's price in Wordpress/ Woocmerce? - html

lets say I am in the Page section where you put the content in WYSIWYG, my plan is to display one woocomerce item's price using php to pull out the info. I downloaded this Plugin Insert PHP where it allows you to put php code in the WYSIWYG section using a special shortcode. The thing is that I am not an expert on php whatsoever, so how would I even start?
so far I've researched this:
<?php echo $product->get_price_html(); ?>
lets say its the item with an SKU: 6-501
how would you do an php to pull the out the price ONLY of that item?

Use the get_price() method instead:
<?php echo $product->get_price(); ?>

WooCommerce version 2.3 and up has a function wc_get_product_id_by_sku( $sku ).
so you could do something like,
$sku = '6-501';
$_product_id = wc_get_product_id_by_sku( $sku );
$_product = new WC_Product( $_product_id );
echo $_product->get_price_html();

Assuming that you are using Insert PHP plugin, Place the following code between [insert_php] and [/insert_php]:
$sku = '6-501';
global $wpdb;
$id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $sku ) );
if ( $id ){
$product = new WC_Product( $id );
echo $product->get_price();
}
That should work.

Related

Update prices before loading shop / product list in WooCommerce

I am currently developing a website for a trade / wholesale company who offer their clients different discounts (there are roughly 30 discount groups).
I am using wordpress / woocommerce.
I have access to each customers price list through a webservice which I have successfully integrated.
I am currently getting the price and updating woocommerce using the hook 'woocommerce_before_shop_loop'.
The issue I am having is that if all the current prices in woocommerce are set to say $1.00 but the price the current customer pays is $2.00 the first time the page loads it says $1.00 until its reloaded.
I understand that its because the query is obviously performed before both my script runs and the page loads.
function t31_before_shop_list_call() {
if ( is_user_logged_in()) {
$Conn = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
$itemCodesArray = array(); // WILL CONTAIN ARRAY OF SKU's FOR WS
while (have_posts()) : the_post();
global $product;
if( $product->is_type( 'simple' ) ){
$sku = get_post_meta( get_the_ID(), '_sku', true );
array_push($itemCodesArray, $sku);
} elseif( $product->is_type( 'variable' ) ){
$child_ids = $product->get_children();
foreach ($child_ids as $child_id) {
$sku = get_post_meta( $child_id, '_sku', true );
array_push($itemCodesArray, $sku);
}
}
endwhile;
... RUN WEBSERVICE THAT GETS CLIENTS PRICES FOR THE
... RUN THROUGH EACH PRODUCT RETURNED IN WEBSERVICE AND EXEC FOLLOWING UPDATE
UPDATE wp_postmeta
SET meta_value = " . $GDUSproduct_price . "
WHERE post_id = " . $GDUSproduct_id . "
AND meta_key = '_regular_price';";
DELETE
FROM `wp_options`
WHERE (`option_name` LIKE '_transient_wc_var_prices_%'
OR option_name` LIKE '_transient_timeout_wc_var_prices_%')";
}
}
add_action( 'woocommerce_before_shop_loop', 't31_before_shop_list_call', 15 );
Is there a hook I can use that executes before the query is performed or is there a way to re query before the html is displayed.?

Wordpress Trim Words in Content in Posts Archive With HTML Formatting

I'm trying to limit the number of Words in a repeating posts/posts archive. However, when I trim the words WordPress seems to remove all the formatting - the word limit works fine. Here is what I have so far:
<?php
// trim the content
global $post;
$my_content= $post->post_content;
//$my_content = get_the_content();
trimmed_my_content = wp_trim_words( $my_content, 400, ' <span class="moretext">More</span>' );
//echo $trimmed_my_content;
echo apply_filters('trimmed_my_content', $trimmed_my_content);
?>
You will see from the // out PHP that I have tried a couple of approaches with no success. However, if I just output the content in the loop with no filtering it works fine:
<?php the_content('Read more...'); ?>
Works with HTML filtering/formatting.
I should note I want to include this code in my WordPress template as I am limiting the content/excerpt elsewhere and I don't want it to be universal. Obviously, a function in functions.php called by PHP in the template is fine.
You can use
$my_content = apply_filters('the_content', $post->post_content);
Instead of:
$my_content = $post->post_content;
<?php
global $post;
//$my_content= $post->post_content;
$my_content=apply_filters('the_content',$post->post_content);//this will get the content for you
trimmed_my_content = wp_trim_words( $my_content, 400, ' <span class="moretext">More</span>' );
echo $trimmed_my_content;
?>
Or you can now format your data as required.

How to echo specific mysql data in specific places using php?

If the 'SELECT' statement is used to select data from a database then how do we echo specific rows to specific places on a page using php?
To explain this better - I am trying to SELECT * ALL FROM a table but to echo multiple rows to particular places on the html page using php.
So, imagine that my entire mark up and css has 20 thumbnails on a page and each thumbnail has data and an image that is unique to each thumbnail....do I have to replicate the below 20 times?
I am thinking that the best way to do this (which is probably completely wrong) is to use this statement
SELECT * FROM name_of_table WHERE ID = 4 >>> i.e. where I'd like that specific data echoed....
So, if I have 20 thumbnails do I do this 20 times?
<?php
// Connects to your Database
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error());
$data = mysql_query("SELECT * FROM name_of_table WHERE ID = 4;")
or die(mysql_error());
Print "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data ))
{
Print "<tr>";
Print "<th>Name:</th> <td>".$info['name'] . "</td> ";
Print "<th>Product:</th> <td>".$info['product_name'] . " </td></tr>";
}
Print "</table>";
?>
And, rinse and repeat but I change the below statement each time for each thumbnail (each thumbnail has unique data that comes from each row on the MySQL)
SELECT * FROM name_of_table WHERE ID = 4;
What is the best way of doing this?
Thanks!
Simple example.. First get the data with wanted ID:s. Create function for data request.
<?php
// Connects to your Database
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error());
$data = mysql_query("SELECT * FROM name_of_table WHERE ID IN (2,3,4,5,6);")
or die(mysql_error());
// This holds all data rows
$data_array = array();
while($info = mysql_fetch_array( $data ))
$data_array[] = $data;
// Function for rendering data to html
function getItemHtml($id) {
$html = "";
foreach($data_array as $row) {
if ($row['ID'] == $id) {
$html = "<td>" . $row['title'] . "</td>";
// etc.. create item html here
break;
}
}
return $html;
}
// To create one item just call this with item id.
echo getItemHtml(4);
?>

Two Queries with WP_Query append second one to end with Pagination

What I am trying to do is create two queries for properties. One will retrieve the regular results based on a normal query. The second query will retrieve properties closely related to the first query. I am able to run both queries and retrieve all results with posts_per_page set to unlimited and no pagination. The problem when adding pagination is that both loops run and display posts on each page.
The page would have 3 from the first loop followed by 3 from the second loop.
I have tried to merge the two queries into one and show them but the same results happen. 3 and 3.
I am thinking that I need to append somehow to be sure that the second loop is getting output after the first. Any thoughts?
Here are my loops(I excluded args because of length)
<?php
$queryOne = new WP_Query($args);
$queryTwo = new WP_Query($args2);
$results = new WP_Query();
$results->posts = array_merge($queryOne->posts, $queryTwo->posts);
?>
<?php foreach($results->posts as $post) : ?>
<?php setup_postdata( $post ); ?>
<?php get_template_part( 'property-listing' ); ?>
<?php endforeach; ?>
as parse_query relies on post_count you'll have to add the two post_counts. In your example post_count is not set. It should work if you populate the post_count. Simply add this at the end:
$results->post_count = $queryOne->post_count + $queryTwo->post_count;
Your complete example:
<?php
$queryOne = new WP_Query($args);
$queryTwo = new WP_Query($args2);
$results = new WP_Query();
$results->posts = array_merge($queryOne->posts, $queryTwo->posts);
$results->post_count = $queryOne->post_count + $queryTwo->post_count;
foreach($results->posts as $post) :
setup_postdata( $post );
get_template_part( 'property-listing' );
endforeach;
?>

Wordpress (database); Show last modified date?

How can I show the last modified date, when my Mysql-database was updated?
(I know there is plugin for showing latest modified date for post/pages, I want to do similar, but show when fields/metadata was updated)
Thankful for answers"
Ok, solved it.
<?php
global $wpdb;
$result = $wpdb->get_results("SHOW TABLE STATUS FROM database_name LIKE 'table_name';");
foreach ($result as $data) {
$updatetime = $data->Update_time;
}
$date = substr($updatetime, 0, -3);
echo $date;
?>