Woocommerce php array select dropdown only shows 10 results - php

I have this product dropdown for Woocommerce.
Problem is: it does only display 10 product..
I would like it to show all of my products!
Can anyone guide me to the problem :-) ?
Dropdown:
<option value=""> - Select poster - </option>';
$args = array( 'post_type' => 'product' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) :
$loop->the_post();
$post_id = $loop->post->ID;
$product = wc_get_product($post_id);
$image_id = $product->get_image_id();
$image_url = wp_get_attachment_image_url( $image_id, 'full' );
$title = $product->get_name();
$permalink = $product->get_permalink();
echo '<option value="'.$image_url.'" producturl="'.$permalink.'".>'.$title.'</option>';
endwhile;
// Reset post data
wp_reset_postdata();
echo'</select>
Note: A jQuery scripts sorts the dropdown alphabetically, and another script makes next and previous buttons for the dropdown - but i guess thats not the problem..

solved: just WP settings -> max post pr page was set to 10

Related

WooCommerce product category pages with previous and next term links

I want to customize WooCommerce category page (archive-product.php) to show current product category, but also I want to create Previous & Next links to previous and next category term links somewhere in the page.
I've tried a lot of solutions with no luck. I've manage to implement a code that take actual category ID, I've added +1 and -1 and I was able to create the links but the big issue is when 'next link' is on the last category that is not working, is not infinite loop for the links. I.E if I'm on cat page 6 that is last category I have, next one should be 1 and prev one should be 5 and if I'm on first category, prev one should be 6 and next one should be 2.
Please find below the piece of code I`m using and is working but not as I need:
<?php
$taxonomy = 'product_cat';
$cate = get_queried_object();
$cateID = $cate->term_id;
$next_cateID = $cateID + 1;
$prev_cateID = $cateID - 1;
$next_term_link = get_term_link( $next_cateID, $taxonomy );
$next_term = get_term( $next_cateID, $taxonomy );
$next_slug = $next_term->name;
$prev_term_link = get_term_link( $prev_cateID, $taxonomy );
$prev_term = get_term( $prev_cateID, $taxonomy );
$prev_slug = $prev_term->name;
?>
<p><?php echo $prev_slug; ?></p>
<p><?php echo $next_slug; ?></p>
Updated:
You just need to define your first and your last categories Ids. Then it will work as an infinite loop (assuming that your product category terms are sequential):
<?php
if ( is_product_category() ) :
$taxonomy = 'product_cat'; // WooCommerce product category taxonomy
$term_id = get_queried_object_id(); // Current product category term Id
// Get first product category term Id
$query_args = array('taxonomy' => 'product_cat', 'hide_empty' => false, 'orderby' => 'term_id', 'number' => '1', 'fields' => 'ids');
$term_id_1st = get_terms($query_args);
$term_id_1st = reset($term_id_1st);
// Get last product category term Id
$query_args['order'] = 'DESC';
$term_id_end = get_terms($query_args);
$term_id_end = reset($term_id_end);
$next_term_id = $term_id_end == $term_id ? $term_id_1st : $term_id + 1;
$prev_term_id = $term_id_1st == $term_id ? $term_id_end : $term_id - 1;
$next_term_link = get_term_link( $next_term_id, $taxonomy );
$next_term = get_term( $next_term_id, $taxonomy );
$prev_term_link = get_term_link( $prev_term_id, $taxonomy );
$prev_term = get_term( $prev_term_id, $taxonomy );
?>
<p><?php echo $prev_term->name; ?></p>
<p><?php echo $next_term->name; ?></p>
<?php endif; ?>
To get the last category you can try the following

Show price in Woocommerce product custom loop

I'm using Woocommerce Bookings with only a base cost filled. No other rules. How do I display product price in a loop like this?
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => -1 );
$loop = new WP_Query( $args );
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
ID is: <?php the_ID(); ?>
Title is: <?php the_title(); ?>
Price: <?php echo esc_html( get_post_meta( get_the_ID(), '_regular_price', true ) ); ?>
<?php endwhile; wp_reset_query(); ?>
The price field didn't show anything
UPDATE
another code I tried:
<?php
global $woocommerce;
$product = new WC_Product_Booking($productID);
$base_price = $product->get_price();
$product_data = $product->get_data();
$product_pricing = get_post_meta( $product_id, '_wc_booking_pricing', true);
?>
<?php echo $product_pricing; ?>
<?php echo $base_price;?>
both also return the value of zero
ANOTHER TRY
<?php
global $woocommerce;
$product = new WC_Product_Booking($productID);
$product = wc_get_product( $product_id );
$base_price = $product->get_price();
$product_data = $product->get_data();
$product_pricing = $product_data['pricing'];
foreach($product_pricing as $key => $princing ){
$pricing_type = $princing['type'];
$pricing_base_cost = $princing['base_cost'];
$pricing_base_modifier = $princing['base_modifier'];
$pricing_cost = $princing['cost'];
$pricing_modifier = $princing['modifier'];
$pricing_from = $princing['from'];
$pricing_to = $princing['to'];
}
$pricing_data = get_post_meta( $product_id, '_wc_booking_pricing', false); ?>
<?php echo $pricing_data; ?>
nothing works :( they're all either showing blanks or showing zero.
Can anyone help point in the right direction?
many thanks
Try (from the docs):
$product = new WC_Product_Booking($productID);
//don't know if you need 'echo'
echo $product->get_price_html()
If not, I think the logic of 2nd snippet it's ok, but probably productID != postID,
so another solution would be:
find the postID for that productID
call get_post_meta( postID, '_wc_booking_pricing', true);
print the price
EDIT
My answer's too long for being posted in the comments.
Are you sure that you are set correctly the product from wp-admin?
In order to do the second suggestion, you need access to the database.
Get the ID of the post(product) in wp-admin when you create/modify it, you should have in the url something like {URL}/wp-admin/id=123.
Once you have the ID go in wp_post_meta table in the DB and look for all the fields with that post_id, and check if you can find the correct price.
Once you find it, you can call get_post_meta(correctID,correctKey)
It seems like you're pulling the right meta key, could be related with you using esc_url(), when you should be using just esc_html(). I'd try that.

How to fetch the woocommerce product image from wp_query

Hi I am inflating the dropdown the woocommerce product titles with the below code
<select Name='choose' id="chooseme">
<?php
$args = array( 'post_type' => array('product') ,'posts_per_page' => 100);
$loop = new WP_Query( $args );
;
while ( $loop->have_posts() ) :
$loop->the_post();
echo '<option selected value="'.the_title().'</option>';
endwhile;
?>
</select>
Now I want to get the products Image from this query. Is it possible to get the image also from this code or is there any other solution for this?
You just need to use get_the_post_thumbnail():
echo get_the_post_thumbnail($loop->post->ID, 'yourTable');
Inside the body of while()
Example:
while ($loop->have_posts()) : $loop->the_post();
echo get_the_post_thumbnail($loop->post->ID, 'yourTable');
endwhile;

Fetch all products of a category in custom post type in wordpress

I am new in wordpress and I am using RT-Theme 15 and I Want to show all
product of custom Post type. When I fetch products all products is not coming
of a particular category. only 9 product is coming out of 15.
here is code whic is showing 9 products:-
<?php
#
# rt-theme product loop
#
global $args,$wp_query;
//column
$box_counter = 0;
if(is_tax()) $args = array_merge( $wp_query->query, $args);
query_posts($args);
$product = array();
$postCount = 0;
if ( have_posts() ) : while ( have_posts() ) : the_post();
?>
<?php
//box class
$product[$postCount]['title'] = get_the_title();
$product[$postCount]['thumb'] = find_image_org_path(get_post_meta($post->ID, THEMESLUG.'product_image_url', true));
$product[$postCount]['image'] = #vt_resize( '', $thumb, $w, $h, ''.$crop.'' );
$product[$postCount]['short_desc'] = get_post_meta($post->ID, THEMESLUG.'short_description', true);
$product[$postCount]['permalink'] = get_permalink();
$product[$postCount]['watt'] = get_post_meta($post->ID, 'wpcf-watt', true);
$postCount = $postCount + 1;
?>
<?php endwhile?>
<?php
echo "<pre>";print_r($product);
?>
<?php endif; wp_reset_query();?>
But I want to all products into array. I don't know how to show all products and
where is setting I have to change to show to all products.
You can use argument posts_per_page in your query. Set it to -1 and it will fetch all the posts.
So something like this:
$args['posts_per_page'] = -1; query_posts($args);
You can set count use this code in function.php file
function change_posts_per_page( $query )
{
$query->set( 'posts_per_page', 5 );
}
add_action( 'pre_get_posts', 'change_posts_per_page' );
or
You can set posts count from admin panel -
1] Go to Dashboard > Setting
2] Then Reading Settings.
3] In to that set post count 15.
4] Then Save Changes

WooCommerce return product object by id

I am creating a custom theme for woocommerce and I need to be able to create a mini product display. I am having problems finding documentation on the woocommerce api. I have a comma delimited list of product IDs that I need to iterate through and display a custom mini product display for each in sequence.
$key_values = get_post_custom_values('rel_products_ids');
//get comma delimited list from product
$rel_product_ids = explode(",", trim($key_values, ","));
// create array of just the product ids
foreach ( $rel_product_ids as $pid ) {
//sequentially get each id and do something with it
$loop = new WP_Query( array( 'post__in' => $pid ) );
// also tried ...
//$loop = new WP_Query( array( 'ID' => $pid ) );
while ( $loop->have_posts() ) : $loop->the_post(); $_product = &new WC_Product( $loop->post->ID );
//do stuff here I have stripped the html in favor of getting to the meat of the issue
woocommerce_show_product_sale_flash( $post, $_product );
if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_single');
get_permalink( $loop->post->ID );
the_title();
$_product->get_price_html();
endwhile;
}
Any help would be appreciated.
Thank you,
Tim
Use this method:
$_product = wc_get_product( $id );
Official API-docs: wc_get_product
Another easy way is to use the WC_Product_Factory class and then call function get_product(ID)
http://docs.woothemes.com/wc-apidocs/source-class-WC_Product_Factory.html#16-63
sample:
// assuming the list of product IDs is are stored in an array called IDs;
$_pf = new WC_Product_Factory();
foreach ($IDs as $id) {
$_product = $_pf->get_product($id);
// from here $_product will be a fully functional WC Product object,
// you can use all functions as listed in their api
}
You can then use all the function calls as listed in their api:
http://docs.woothemes.com/wc-apidocs/class-WC_Product.html
Alright, I deserve to be throttled. definitely an RTM but not for WooCommerce, for Wordpress.
Solution found due to a JOLT cola (all hail JOLT cola).
TASK:
Field named 'related_product_ids' added to a custom post type. So when that post is displayed mini product displays can be displayed with it.
PROBLEM:
Was having a problem getting the multiple ids returned via WP_Query.
SOLUTION:
$related_id_list = get_post_custom_values('related_product_ids');
// Get comma delimited list from current post
$related_product_ids = explode(",", trim($related_id_list[0],','));
// Return an array of the IDs ensure no empty array elements from extra commas
$related_product_post_ids = array( 'post_type' => 'product',
'post__in' => $related_product_ids,
'meta_query'=> array(
array( 'key' => '_visibility',
'value' => array('catalog', 'visible'),'compare' => 'IN'
)
)
);
// Query to get all product posts matching given IDs provided it is a published post
$loop = new WP_Query( $related_posts );
// Execute query
while ( $loop->have_posts() ) : $loop->the_post(); $_product = get_product( $loop->post->ID );
// Do stuff here to display your products
endwhile;
Thank you for anyone who may have spent some time on this.
Tim
global $woocommerce;
var_dump($woocommerce->customer->get_country());
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = new WC_product($cart_item['product_id']);
var_dump($product);
}

Categories