Ordering options. Closes #1166.

This commit is contained in:
Mike Jolley 2012-10-09 15:57:02 +01:00
parent 382bb815a9
commit 5868b63b93
6 changed files with 86 additions and 9 deletions

View File

@ -30,6 +30,7 @@ function woocommerce_attributes() {
$attribute_name = sanitize_title( esc_attr( $_POST['attribute_name'] ) );
$attribute_type = esc_attr( $_POST['attribute_type'] );
$attribute_label = esc_attr( $_POST['attribute_label'] );
$attribute_orderby = esc_attr( $_POST['attribute_orderby'] );
if ( ! $attribute_label )
$attribute_label = ucwords( $attribute_name );
@ -44,7 +45,8 @@ function woocommerce_attributes() {
array(
'attribute_name' => $attribute_name,
'attribute_label' => $attribute_label,
'attribute_type' => $attribute_type
'attribute_type' => $attribute_type,
'attribute_orderby' => $attribute_orderby
)
);
@ -60,6 +62,7 @@ function woocommerce_attributes() {
$attribute_name = sanitize_title( esc_attr( $_POST['attribute_name'] ) );
$attribute_type = esc_attr( $_POST['attribute_type'] );
$attribute_label = esc_attr( $_POST['attribute_label'] );
$attribute_orderby = esc_attr( $_POST['attribute_orderby'] );
if ( ! $attribute_label )
$attribute_label = ucwords( $attribute_name );
@ -80,7 +83,8 @@ function woocommerce_attributes() {
array(
'attribute_name' => $attribute_name,
'attribute_label' => $attribute_label,
'attribute_type' => $attribute_type
'attribute_type' => $attribute_type,
'attribute_orderby' => $attribute_orderby
),
array(
'attribute_id' => $edit
@ -193,6 +197,7 @@ function woocommerce_edit_attribute() {
$att_type = $attribute_to_edit->attribute_type;
$att_label = $attribute_to_edit->attribute_label;
$att_name = $attribute_to_edit->attribute_name;
$att_orderby = $attribute_to_edit->attribute_orderby;
?>
<div class="wrap woocommerce">
<div class="icon32 icon32-attributes" id="icon-woocommerce"><br/></div>
@ -230,6 +235,19 @@ function woocommerce_edit_attribute() {
<p class="description"><?php _e('Determines how you select attributes for products. <strong>Text</strong> allows manual entry via the product page, whereas <strong>select</strong> attribute terms can be defined from this section. If you plan on using an attribute for variations use <strong>select</strong>.', 'woocommerce'); ?></p>
</td>
</tr>
<tr class="form-field form-required">
<th scope="row" valign="top">
<label for="attribute_orderby"><?php _e('Default sort order', 'woocommerce'); ?></label>
</th>
<td>
<select name="attribute_orderby" id="attribute_orderby">
<option value="menu_order" <?php selected( $att_orderby, 'menu_order' ); ?>><?php _e( 'Custom ordering', 'woocommerce' ) ?></option>
<option value="name" <?php selected( $att_orderby, 'name' ); ?>><?php _e( 'Name', 'woocommerce' ) ?></option>
<option value="id" <?php selected( $att_orderby, 'id' ); ?>><?php _e( 'Term ID', 'woocommerce' ) ?></option>
</select>
<p class="description"><?php _e( 'Determines the sort order on the frontend for this attribute. If using custom ordering, you can drag and drop the terms in this attribute', 'woocommerce' ); ?></p>
</td>
</tr>
</tbody>
</table>
<p class="submit"><input type="submit" name="save_attribute" id="submit" class="button-primary" value="<?php _e('Update', 'woocommerce'); ?>"></p>
@ -265,6 +283,7 @@ function woocommerce_add_attribute() {
<th scope="col"><?php _e('Name', 'woocommerce') ?></th>
<th scope="col"><?php _e('Slug', 'woocommerce') ?></th>
<th scope="col"><?php _e('Type', 'woocommerce') ?></th>
<th scope="col"><?php _e('Order by', 'woocommerce') ?></th>
<th scope="col" colspan="2"><?php _e('Terms', 'woocommerce') ?></th>
</tr>
</thead>
@ -281,6 +300,19 @@ function woocommerce_add_attribute() {
</td>
<td><?php echo esc_html( $tax->attribute_name ); ?></td>
<td><?php echo esc_html( ucwords( $tax->attribute_type ) ); ?></td>
<td><?php
switch ( $tax->attribute_orderby ) {
case 'name' :
_e( 'Name', 'woocommerce' );
break;
case 'id' :
_e( 'Term ID', 'woocommerce' );
break;
default:
_e( 'Custom ordering', 'woocommerce' );
break;
}
?></td>
<td><?php
if (taxonomy_exists($woocommerce->attribute_taxonomy_name($tax->attribute_name))) :
$terms_array = array();
@ -301,7 +333,7 @@ function woocommerce_add_attribute() {
</tr><?php
endforeach;
else :
?><tr><td colspan="5"><?php _e('No attributes currently exist.', 'woocommerce') ?></td></tr><?php
?><tr><td colspan="6"><?php _e('No attributes currently exist.', 'woocommerce') ?></td></tr><?php
endif;
?>
</tbody>
@ -334,6 +366,16 @@ function woocommerce_add_attribute() {
</select>
<p class="description"><?php _e('Determines how you select attributes for products. <strong>Text</strong> allows manual entry via the product page, whereas <strong>select</strong> attribute terms can be defined from this section. If you plan on using an attribute for variations use <strong>select</strong>.', 'woocommerce'); ?></p>
</div>
<div class="form-field">
<label for="attribute_orderby"><?php _e( 'Default sort order', 'woocommerce' ); ?></label>
<select name="attribute_orderby" id="attribute_orderby">
<option value="menu_order"><?php _e( 'Custom ordering', 'woocommerce' ) ?></option>
<option value="name"><?php _e( 'Name', 'woocommerce' ) ?></option>
<option value="id"><?php _e( 'Term ID', 'woocommerce' ) ?></option>
</select>
<p class="description"><?php _e( 'Determines the sort order on the frontend for this attribute. If using custom ordering, you can drag and drop the terms in this attribute', 'woocommerce' ); ?></p>
</div>
<p class="submit"><input type="submit" name="add_new_attribute" id="submit" class="button" value="<?php _e('Add Attribute', 'woocommerce'); ?>"></p>
<?php wp_nonce_field( 'woocommerce-add-new_attribute' ); ?>

View File

@ -54,7 +54,7 @@ function woocommerce_import_start() {
if (!$exists_in_db) :
// Create the taxonomy
$wpdb->insert( $wpdb->prefix . "woocommerce_attribute_taxonomies", array( 'attribute_name' => $nicename, 'attribute_type' => 'select' ), array( '%s', '%s' ) );
$wpdb->insert( $wpdb->prefix . "woocommerce_attribute_taxonomies", array( 'attribute_name' => $nicename, 'attribute_type' => 'select', 'attribute_orderby' => 'menu_order' ), array( '%s', '%s', '%s' ) );
endif;

View File

@ -246,6 +246,7 @@ CREATE TABLE ". $wpdb->prefix . "woocommerce_attribute_taxonomies (
attribute_name varchar(200) NOT NULL,
attribute_label longtext NULL,
attribute_type varchar(200) NOT NULL,
attribute_orderby varchar(200) NOT NULL,
PRIMARY KEY (attribute_id)
) $collate;
";

View File

@ -168,6 +168,7 @@ Yes you can! Join in on our [GitHub repository](http://github.com/woothemes/wooc
* Feature - New product images panel to make working with featured images + galleries easier.
* Feature - Schedule sales for variations.
* Feature - Expanded bulk edit for prices. Change to, increase by, decrease by.
* Feature - Set attribute order (globally, per attribute).
* Templating - email-order-items.php change get_downloadable_file_url() to get_downloadable_file_urls() to support multiple files.
* Templating - loop-end and start for product loops, allow changing the UL's used by default to something else.

View File

@ -20,9 +20,9 @@ global $woocommerce, $product, $post;
<tbody>
<?php $loop = 0; foreach ( $attributes as $name => $options ) : $loop++; ?>
<tr>
<td class="label"><label for="<?php echo sanitize_title($name); ?>"><?php echo $woocommerce->attribute_label($name); ?></label></td>
<td class="label"><label for="<?php echo sanitize_title($name); ?>"><?php echo $woocommerce->attribute_label( $name ); ?></label></td>
<td class="value"><select id="<?php echo esc_attr( sanitize_title($name) ); ?>" name="attribute_<?php echo sanitize_title($name); ?>">
<option value=""><?php echo __('Choose an option', 'woocommerce') ?>&hellip;</option>
<option value=""><?php echo __( 'Choose an option', 'woocommerce' ) ?>&hellip;</option>
<?php
if ( is_array( $options ) ) {
@ -33,8 +33,22 @@ global $woocommerce, $product, $post;
// Get terms if this is a taxonomy - ordered
if ( taxonomy_exists( sanitize_title( $name ) ) ) {
$orderby = $woocommerce->attribute_orderby( $name );
switch ( $orderby ) {
case 'name' :
$args = array( 'orderby' => 'name', 'order' => 'ASC', 'menu_order' => false );
break;
case 'id' :
$args = array( 'orderby' => 'id', 'order' => 'ASC', 'menu_order' => false );
break;
case 'menu_order' :
$args = array( 'menu_order' => 'ASC' );
break;
}
$terms = get_terms( sanitize_title($name), array('menu_order' => 'ASC') );
$terms = get_terms( sanitize_title( $name ), $args );
foreach ( $terms as $term ) {
if ( ! in_array( $term->slug, $options ) ) continue;
@ -48,7 +62,7 @@ global $woocommerce, $product, $post;
?>
</select> <?php
if ( sizeof($attributes) == $loop )
echo '<a class="reset_variations" href="#reset">'.__('Clear selection', 'woocommerce').'</a>';
echo '<a class="reset_variations" href="#reset">' . __( 'Clear selection', 'woocommerce' ) . '</a>';
?></td>
</tr>
<?php endforeach;?>

View File

@ -785,7 +785,7 @@ class Woocommerce {
$label = ( isset( $tax->attribute_label ) && $tax->attribute_label ) ? $tax->attribute_label : $tax->attribute_name;
$show_in_nav_menus = apply_filters('woocommerce_attribute_show_in_nav_menus', false, $name);
$show_in_nav_menus = apply_filters( 'woocommerce_attribute_show_in_nav_menus', false, $name );
register_taxonomy( $name,
array('product'),
@ -1455,6 +1455,25 @@ class Woocommerce {
return apply_filters( 'woocommerce_attribute_label', $label, $name );
}
/**
* Get a product attributes orderby setting.
*
* @access public
* @param mixed $name
* @return string
*/
function attribute_orderby( $name ) {
global $wpdb;
$name = str_replace( 'pa_', '', sanitize_title( $name ) );
$orderby = $wpdb->get_var( $wpdb->prepare( "SELECT attribute_orderby FROM " . $wpdb->prefix . "woocommerce_attribute_taxonomies WHERE attribute_name = %s;", $name ) );
return apply_filters( 'woocommerce_attribute_orderby', $orderby, $name );
}
/**