I would like to show all products and their variations on the Wp/Woocommerce /shop/ page. (/woocommerce/archive-product.php). The process I'm describing below is my attempt at getting this to work. Alternatives are welcome.
What is the norm: Product A and Product B show on the site. Product A has 'ad to cart' button. Product B has 'Select Options' button.
What I'm after: Product A is a 'simple' product and shows on the shop page Product B is a 'variable' product and has 2 combinations (Black/Blue). An Add to cart button adds this variation to the cart
I would like to show 3 products on the shop page equaling 1* product A and 2* product B.
So far I've pulled out if the product is variable some basic details about this product variation but I cannot seem to find the thumbnail?
$postId = get_the_ID();
$args = array(
'post_type' => 'product_variation',
'post_status' => array( 'private', 'publish' ),
'numberposts' => -1,
'orderby' => 'menu_order',
'order' => 'asc',
'post_parent' => $postId
);
$variations = get_posts( $args );
//var_dump($variations);
//$p = get_post_meta($postId, '_product_attributes', true); // return pa_flavor
//var_dump($p);
if(!empty($variations)) {
foreach ($variations as $variation) {
$vari = new WC_Product_Variation($variation->ID);
$availableAttr = $vari->get_attribute('Flavour');
$flavour = get_post_meta($variation->ID, 'attribute_pa_flavour', true);
Therefore, I can get the attribute for 'flavor' and am looking for the thumbnail that is defined for each variation...? Any help/pointers to where this could be are appreciated.
Also [ probably my lack of understanding of Wordpress ] I cannot seem to pass/access variables in wc_get_template(). This is probably a secondary question but still related to the fundamental issue of displaying both single and variation products on one page.
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price_variation', 10 );
if ( ! function_exists( 'woocommerce_template_loop_price_variation' ) ) {
function woocommerce_template_loop_price_variation($prodVar=array()) {
if(empty($prodVar) || $prodVar['product_type'] == 'simple') {
wc_get_template( 'loop/price.php' );
} else {
wc_get_template( 'loop/price-variation.php' , $prodVar );
}
}
}
And in /price-variation.php (copy of price.php woocommerce base)
global $product;
var_dump($prodVar); // PRODUCED NOTICE to undefined variable???
<?php if ( $price_html = $product->get_price_html() ) : ?>
<span class="price"><?php echo $price_html; ?></span>
<?php endif; ?>
I don't want it global...? or is this the only way?
Any help or recommendations appreciated.
$variation->get_image()
should get a variation object's thumbnail. Also, I think you can changenew WC_Product_Variation
towc_get_product()
. – helgatheviking