Add short code to display products with attribute

Add ability to set default value on checkout fields
This commit is contained in:
Andrew Benbow 2013-03-03 14:51:25 +00:00
parent 46c0f6827e
commit 9e8953ce28
2 changed files with 81 additions and 1 deletions

View File

@ -26,6 +26,7 @@ class WC_Shortcodes {
add_shortcode( 'top_rated_products', array( $this, 'top_rated_products' ) );
add_shortcode( 'featured_products', array( $this, 'featured_products' ) );
add_shortcode( 'woocommerce_messages', array( $this, 'messages_shortcode' ) );
add_shortcode( 'product_attribute', array( $this, 'product_attribute' ) );
// Pages
add_shortcode( 'woocommerce_cart', array( $this, 'cart' ) );
@ -953,4 +954,80 @@ class WC_Shortcodes {
return $args;
}
/**
* List products with an attribute shortcode
* Example [product_attribute attribute='color' filter='black']
*
* @access public
* @param array $atts
* @return string
*/
function product_attribute( $atts ) {
global $woocommerce_loop;
if ( empty( $atts ) ) return;
extract( shortcode_atts( array(
'per_page' => '12',
'columns' => '4',
'orderby' => 'title',
'order' => 'asc',
'attribute' => '',
'filter' => ''
), $atts ) );
$attribute = 'pa_' . sanitize_title($attribute);
$filter = sanitize_title($filter);
if ( ! $attribute || ! $filter ) return;
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'orderby' => $orderby,
'order' => $order,
'posts_per_page' => $per_page,
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array('catalog', 'visible'),
'compare' => 'IN'
)
),
'tax_query' => array(
array(
'taxonomy' => $attribute,
'terms' => $filter,
'field' => 'slug'
)
)
);
ob_start();
$products = new WP_Query( $args );
$woocommerce_loop['columns'] = $columns;
if ( $products->have_posts() ) : ?>
<ul class="products">
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php woocommerce_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
</ul>
<?php endif;
wp_reset_query();
return ob_get_clean();
}
}

View File

@ -1276,7 +1276,8 @@ if ( ! function_exists( 'woocommerce_form_field' ) ) {
'return' => false,
'options' => array(),
'custom_attributes' => array(),
'validate' => array()
'validate' => array(),
'defaultval' => '',
);
$args = wp_parse_args( $args, $defaults );
@ -1292,6 +1293,8 @@ if ( ! function_exists( 'woocommerce_form_field' ) ) {
$args['maxlength'] = ( $args['maxlength'] ) ? 'maxlength="' . absint( $args['maxlength'] ) . '"' : '';
if ( $value == '' ) $value = $args['defaultval'];
// Custom attribute handling
$custom_attributes = array();