-
-
Save brettsmason/d3cdcf52716941fe9aa880b155026324 to your computer and use it in GitHub Desktop.
WooCommerce - Display a list of variable products priced by a single or several variant states (attributes)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: SYM Woo Extensions | |
* Plugin URI: https://symphonyagency.com | |
* Description: A set of utilities for use with WooCommerce | |
* Version: 1.0.0 | |
* Author: Chris Jenkins | |
* Author URI: http://symphonyagency.com | |
* License: GPL3 | |
* Text Domain: sym-woo | |
*/ | |
// We need to retrieve the product level variation ID based on a combination of terms | |
function get_product_variation_id($product_id, $attributes ) { | |
$variation_id = find_matching_product_variation_id ( $product_id, $attributes); | |
return $variation_id; | |
} | |
function find_matching_product_variation_id($product_id, $attributes) | |
{ | |
return (new \WC_Product_Data_Store_CPT())->find_matching_product_variation( | |
new \WC_Product($product_id), | |
$attributes | |
); | |
} | |
// When we have a product ID and a variation ID, we need to retrieve the price for that variation | |
function get_variation_price_by_id($product_id, $variation_id){ | |
$product = new WC_Product_Variable($product_id); | |
$variations = $product->get_available_variations(); | |
foreach ($variations as $variation) { | |
if($variation['variation_id'] == $variation_id){ | |
$display_regular_price = $variation['display_regular_price']; | |
$display_price = $variation['display_price']; | |
$priceArray = array( | |
'display_regular_price' => $display_regular_price, | |
'display_price' => $display_price | |
); | |
$priceObject = (object)$priceArray; | |
return $priceObject; | |
} | |
} | |
} | |
// And here's the display function for the shortcode using the combination of above | |
function single_variation_display($atts){ | |
$atts = shortcode_atts( | |
array( | |
'category' => '', | |
'size' => '', | |
'foundation' => '', | |
), $atts ); | |
$product_cat = $atts['category']; | |
$product_size = $atts['size']; | |
$product_foundation = $atts['foundation']; | |
$attributes = [ | |
'attribute_pa_size' => $product_size, | |
'attribute_pa_box-spring' => $product_foundation | |
]; | |
$currency_symbol = get_woocommerce_currency_symbol(); | |
$product_display = '<div class="woocommerce columns-3 "><ul class="products columns-3">'; | |
$args = array( | |
'posts_per_page' => -1, | |
'product_cat' => $product_cat, | |
'post_type' => 'product', | |
'orderby' => 'title', | |
); | |
$the_query = new WP_Query( $args ); | |
// The Loop | |
while ( $the_query->have_posts() ) { | |
$the_query->the_post(); | |
$product_id = get_the_ID(); | |
$product = wc_get_product($product_id); | |
// Get our variation ID | |
$variation_ID = get_product_variation_id( $product_id, $attributes ); | |
ob_start(); | |
?> | |
<li <?php wc_product_class( '', $product_id ); ?>> | |
<?php do_action('woocommerce_before_shop_loop_item'); ?> | |
<span class="product-images"> | |
<?php | |
/** | |
* woocommerce_before_shop_loop_item_title hook | |
* | |
* @hooked woocommerce_show_product_loop_sale_flash - 10 | |
* @hooked woocommerce_template_loop_product_thumbnail - 10 | |
*/ | |
do_action('woocommerce_before_shop_loop_item_title'); | |
?> | |
</span> | |
<div class="product-details"> | |
<div class="product-details-container"> | |
<?php | |
/** | |
* woocommerce_shop_loop_item_title hook | |
* | |
* @hooked woocommerce_template_loop_product_title - 10 | |
*/ | |
//we are not using this hook in Evolve Plus Theme, because the title produced is not a link, | |
//we are using the following at line 79 <h3 class="product-title"><a...</a></h3> | |
//do_action( 'woocommerce_shop_loop_item_title' ); | |
?> | |
<h3 class="product-title"><span><?php the_title(); ?></span></h3> | |
<div class="clearfix"> | |
<?php | |
$variation_price = get_variation_price_by_id($product_id, $variation_ID); | |
$current_price = '<ins><span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">'. $currency_symbol .'</span>'.$variation_price -> display_price.'</span></ins>'; | |
$on_sale = $product->is_on_sale(); | |
if($on_sale){ | |
echo '<span class="onsale">Sale!</span>'; | |
$regular_price = '<del><span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">'. $currency_symbol .'</span>'.$variation_price -> display_regular_price.'</span></del>'; | |
} | |
?> | |
<span class="price">Starting at <?php echo $regular_price.' '.$current_price; ?> </span> | |
<?php | |
unset($regular_price); | |
unset($current_price); | |
?> | |
</div> | |
</div> | |
</div> | |
<?php | |
/** | |
* woocommerce_after_shop_loop_item hook | |
* | |
* @hooked woocommerce_template_loop_add_to_cart - 10 | |
*/ | |
do_action('woocommerce_after_shop_loop_item'); | |
?> | |
</li> | |
<?php | |
$product_display .= ob_get_clean(); | |
} | |
wp_reset_postdata(); | |
$product_display .='</ul></div>'; | |
return $product_display; | |
} | |
add_shortcode('single-variation','single_variation_display'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment