I'm trying to create a simple xml for all my products in my woocommerce.
I already have build most of it, but I simply cant get the current product price in the loop:
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
include_once $path . '/wp-load.php';
header('Content-Type: text/xml; charset=utf-8');
$xml = new SimpleXMLElement('<STORE/>');
$products = $xml->addChild('PRODUCTS');
// Setup your custom query
$args = array(
'post_type' => 'product',
'posts_per_page' => -1
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() && $count<1) {
$count++;
$loop->the_post();
$wcProduct = new WC_Product( get_the_ID() );
$pr = $wcProduct->get_price_html();
$product = $products->addChild('PRODUCT');
$product->addChild('PRODUCT_URL', get_permalink(get_the_ID()));
$product->addChild('PRODUCT_NAME', get_the_title());
$product->addChild('price', $pr);
}
wp_reset_query();
print($xml->asXML());
?>
it gives something relevant, any ideas how to solve it?
$pr = $wcProduct->price; should be all you need in the loop instead of the get_price_html call.
Related
Hello I applied the code I found here.
But I can't figure out how can I call this image on frontend.
I tried this so far:
$args = array('post_type' => 'katastima');
$the_query = new WP_Query($args);
while ( $the_query->have_posts() ) : $the_query->next_post();
$id= $the_query->post->ID;
$location = get_post_meta($id, 'listingimagediv', true);
echo $location;
endwhile;
but I guess this is not the correct way to get the image link from the custom metabox.
Any suggestions?
Thanks in advance.
You get the photo ID from get_post_meta
So the next step is to get the url of the image, see here
$size = 'full';
$icon = null;
$attr = array( "class" => "img-responsive" );
echo wp_get_attachment_image( $location, $size, $icon, $attr );
I'm trying to get the featured image from the postid passed through the url.
http://www.example.com/schedule-appointment/?postid=589
I've managed to get the postid from the url, but everything goes down hill from there. I must be missing something simple. I'm not a programmer...would love some help.
add_shortcode('CF7_ADD_POST_ID', 'cf7_add_post_id');
function cf7_add_post_id(){
$Path=$_SERVER['REQUEST_URI'];
$control = array();
$control = explode('?', $Path);
$get = $control[1];
$get = explode('=', $get);
$get2 = $get[1];
$args = array(
'post_type' => 'page',
'post__in' => $get2,
);
// Fire up the Query
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ): $the_query->the_post();
$feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->$get2) );
echo '$feat_image';
};
Try this
<?php
add_shortcode('CF7_ADD_POST_ID', 'cf7_add_post_id');
function cf7_add_post_id(){
$ID = isset( $_GET["postid"] ) ? $_GET["postid"] : false;
if( $ID ){
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id( $ID ), 'full' );
$url = $thumb['0'];
echo "<img src ='".$url."' alt = 'Image'>";
}
}
?>
There is no need for the WP_Query , You have one id and you can easily get this done by using following code,
add_shortcode('CF7_ADD_POST_ID', 'cf7_add_post_id');
function cf7_add_post_id(){
$postid = $_GET['postid'];
$feat_image = wp_get_attachment_url( get_post_thumbnail_id($postid) );
echo '$feat_image';
};
I want to get all the product data in the WooCommerce (product sku, name, price, stock count, availability and etc.) Can I do that using wp-query?
This way you can get all products via wp_query :
global $wpdb;
$all_product_data = $wpdb->get_results("SELECT ID,post_title,post_content,post_author,post_date_gmt FROM `" . $wpdb->prefix . "posts` where post_type='product' and post_status = 'publish'");
And if you want further product data then it will be like this :
$product = wc_get_product($product_id);
$product->product_type;
get_post_meta($prodyct_id, '_regular_price');
$product->get_available_variations();
Thanks for all reply.I have resolve that like billows
$full_product_list = array();
$loop = new WP_Query(array('post_type' => array('product', 'product_variation'), 'posts_per_page' => -1));
while ($loop->have_posts()) : $loop->the_post();
$theid = get_the_ID();
$product = new WC_Product($theid);
if (get_post_type() == 'product_variation') {
// ****************** end error checking *****************
} else {
$sku = get_post_meta($theid, '_sku', true);
$selling_price = get_post_meta($theid, '_sale_price', true);
$regular_price = get_post_meta($theid, '_regular_price', true);
$description=get_the_content();
$thetitle = get_the_title();
}
// add product to array but don't add the parent of product variations
if (!empty($sku))
$full_product_list[] = array("PartyID" => (int) $party_id,"Description"=> $description,
"ExternalNumber" => $theid, "ProductName" => $thetitle, "sku" => $sku,
"RegularPrice" => $regular_price, "SellingPrice" => $selling_price,
"ExternalProductCategoryId" => $cat_id, "ExternalProductCategoryName" => $cat_name);
endwhile;
When your building out your query set the post type to be product
$query->set('post_type', 'product');
And that will only search through woocommerce products.
Also if you are creating a theme, you can take any file from the /wp-content/plugins/woocommerce/templates/ directory and put it inside of /wp-content/themes/<yourTheme>/woocommerce to override any woocommerce page.
you can write this code in page that you want to display all product information
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 10
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<br />' . woocommerce_get_product_thumbnail().' '.get_the_title().'';
global $product;
//echo $product;
echo $product->get_id();
echo $product->get_name();
echo $product->get_sale_price();
echo $product->get_regular_price();
echo $product->get_sku();
echo $product->get_low_stock_amount();
echo $product->get_review_count();
echo $product->get_short_description();
$ratting = $product->get_rating_counts();
for($i = 0 ; $i < count($ratting); $i++) {
echo $ratting[i];
}
endwhile;
wp_reset_query();
?>
This is an old question; the answers using WP_Query() are obsolete since 2018.
See my answer here.
Currently this is working code that is queried as standalone search.php.
I have an issue that it searches beginning of title and not anywhere in title what I exactly need.
I have a feeling that I'm close to solution and I tried couple of things and it is probably something that I'm missing in post $args..
global $wp_query;
$type = 'qa';
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'showposts'=>100,
'orderby'=> 'menu_order',
'order' => 'DESC'
);
$my_query = null;
$my_query = new WP_Query($args);
$data = array();
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
$title = get_the_title();
$url = get_the_permalink();
$data[$title] = $url;
endwhile;
}
if(isset($_POST['latestQuery'])){
$latestQuery = $_POST['latestQuery'];
$latestQueryLength = strlen($latestQuery);
$result = array();
foreach($data as $name => $url){
if (substr(strtolower($name),0,$latestQueryLength) == strtolower($latestQuery)){
$result[$name] = $url;
}
}
echo json_encode($result);
}
This is already working in local environment.
JavaScript queries search.php file (above) where it gets json array returned based on query... for example "cla.."
{"Classic something":"http:\/\/www.something\/qa2","Classic something else":"http:\/\/www.something\/qa"}
So I have properly retuned two items that only have "Classic" mentioned in post title.
If I type "som" or "els" it doesn't return anything and it should match as well all "some" and "else" items.
I think it is not an issue with javascript code but with PHP code.
Thanks!
Solved if would be helpful to someone in future.
if(isset($_POST['latestQuery'])){
$latestQuery = $_POST['latestQuery'];
global $wp_query;
$type = 'qa';
$args=array(
'post_type' => $type,
'post_status' => 'publish',
's' => "$latestQuery", // defined query
'showposts'=>100,
'orderby'=> 'menu_order',
'order' => 'DESC',
);
$my_query = null;
$my_query = new WP_Query($args);
$data = array();
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
$title = get_the_title();
$url = get_the_permalink();
$data[$title] = $url;
endwhile;
}
$result = array();
foreach($data as $name => $url){ // removed if
$result[$name] = $url;
}
echo json_encode($result);
}
I' am learning how to make a WordPress Plugin. I did make few simple plugins but not as complicated as this one. It's a Events Calendar. The var_dump from the function "nc_get_start_date()" on the page it outputs wrong dates.
The output from the var_dump(nc_get_start_date());
string(32) "1970-01-01,1970-01-01,1970-01-01"
This is what the function should return in real
23-12-2013, 25-12-2013, 26-12-2013
In the function.php on the plugin folder. This is the codes
/* Query to get the events post from the database */
function get_nc_events(){
global $post;
$query = new WP_Query(
array(
'post_type' => 'events',
'post_status' => 'publish',
'posts_per_page' => -1,
'order' => 'ASC'
)
);
return $query;
}
/* Get the start date from the above function */
function nc_get_start_date(){
$query = get_nc_events();
while ( $query->have_posts() ) : $query->the_post();
$nc_event_id = $post->ID;
$wnc_start_date = get_post_meta( $nc_event_id, 'wnc_start_date');
$wnc_start_date = $wnc_start_date[0];
$wnc_start_date = date("Y-m-d", strtotime($wnc_start_date));
$wnc_start_date_array .= "$wnc_start_date,";
endwhile;
return rtrim($wnc_start_date_array, ",");
}
When I write the code in page-caledar.php without the function it renders everything prefectly.
$query = get_nc_events();
while ( $query->have_posts() ) : $query->the_post();
$nc_event_id = $post->ID;
$wnc_start_date = get_post_meta( $nc_event_id, 'wnc_start_date');
echo $wnc_start_date = $wnc_start_date[0] . "<br/>";
endwhile;
Problem Solved. Thanks everyone. The problem was in this function
/* Get the start date from the above function */
function nc_get_start_date(){
global $post;
$query = get_nc_events();
while ( $query->have_posts() ) : $query->the_post();
$nc_event_id = $post->ID;
$wnc_start_date = get_post_meta( $nc_event_id, 'wnc_start_date');
$wnc_start_date = $wnc_start_date[0];
$wnc_start_date = date("Y-m-d", strtotime($wnc_start_date));
$wnc_start_date_array .= "$wnc_start_date,";
endwhile;
return rtrim($wnc_start_date_array, ",");
}
I didn't make global $post;