Futher standardisation of cart and order loops. Closes #3348.

This commit is contained in:
Mike Jolley 2013-06-17 12:21:06 +01:00
parent 97cd97ddda
commit 3b5d8b7d41
8 changed files with 208 additions and 167 deletions

View File

@ -2118,6 +2118,21 @@ class WC_Cart {
return apply_filters( 'woocommerce_cart_subtotal', $cart_subtotal, $compound, $this );
}
/**
* Get the product row price per item.
*
* @params object product
* @return string formatted price
*/
public function get_product_price( $_product ) {
if ( $this->tax_display_cart == 'excl' )
$product_price = $_product->get_price_excluding_tax();
else
$product_price = $_product->get_price_including_tax();
return apply_filters( 'woocommerce_cart_product_price', woocommerce_price( $product_price ), $_product );
}
/**
* Get the product row subtotal.
*

View File

@ -12,8 +12,39 @@
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
function woocommerce_show_messages() {
_deprecated_function( 'woocommerce_show_messages', '2.1', 'wc_print_messages' );
wc_show_messages();
}
/**
* Handle renamed filters
*/
global $wc_map_deprecated_filters;
$wc_map_deprecated_filters = array(
'woocommerce_cart_item_class' => 'woocommerce_cart_table_item_class',
'woocommerce_cart_item_product_id' => 'hook_woocommerce_in_cart_product_id',
'woocommerce_cart_item_thumbnail' => 'hook_woocommerce_in_cart_product_thumbnail',
'woocommerce_cart_item_price' => 'woocommerce_cart_item_price_html',
'woocommerce_cart_item_name' => 'woocommerce_in_cart_product_title',
'woocommerce_order_item_class' => 'woocommerce_order_table_item_class',
'woocommerce_order_item_name' => 'woocommerce_order_table_product_title'
);
foreach ( $wc_map_deprecated_filters as $new => $old )
add_filter( $new, 'woocommerce_deprecated_filter_mapping' );
function woocommerce_deprecated_filter_mapping( $data, $arg_1 = '', $arg_2 = '', $arg_3 = '' ) {
global $wc_map_deprecated_filters;
$filter = current_filter();
if ( isset( $wc_map_deprecated_filters[ $filter ] ) )
if ( has_filter( $wc_map_deprecated_filters[ $filter ] ) ) {
$data = apply_filters( $wc_map_deprecated_filters[ $filter ], $arg_1, $arg_2, $arg_3 );
_deprecated_function( 'The ' . $wc_map_deprecated_filters[ $filter ] . ' filter', '2.1', $filter );
}
return $data;
}

View File

@ -4,7 +4,7 @@
*
* @author WooThemes
* @package WooCommerce/Templates
* @version 1.6.4
* @version 2.1.0
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
@ -12,9 +12,8 @@ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $woocommerce;
wc_print_messages();
?>
<?php do_action( 'woocommerce_before_cart' ); ?>
do_action( 'woocommerce_before_cart' ); ?>
<form action="<?php echo esc_url( $woocommerce->cart->get_cart_url() ); ?>" method="post">
@ -35,25 +34,23 @@ wc_print_messages();
<?php do_action( 'woocommerce_before_cart_contents' ); ?>
<?php
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
$product_id = apply_filters('woocommerce_in_cart_product_id', $values['product_id'] );
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $cart_item ) {
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
$product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
if ( $_product->exists() && $values['quantity'] > 0 ) {
if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
?>
<tr class = "<?php echo esc_attr( apply_filters('woocommerce_cart_table_item_class', 'cart_table_item', $values, $cart_item_key ) ); ?>">
<!-- Remove from cart link -->
<tr class="<?php echo esc_attr( apply_filters( 'woocommerce_cart_item_class', 'cart_item', $cart_item, $cart_item_key ) ); ?>">
<td class="product-remove">
<?php
echo apply_filters( 'woocommerce_cart_item_remove_link', sprintf( '<a href="%s" class="remove" title="%s">&times;</a>', esc_url( $woocommerce->cart->get_remove_url( $cart_item_key ) ), __( 'Remove this item', 'woocommerce' ) ), $cart_item_key );
?>
</td>
<!-- The thumbnail -->
<td class="product-thumbnail">
<?php
$thumbnail = apply_filters( 'woocommerce_in_cart_product_thumbnail', $_product->get_image(), $values, $cart_item_key );
$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );
if ( ! $_product->is_visible() )
echo $thumbnail;
@ -62,33 +59,28 @@ wc_print_messages();
?>
</td>
<!-- Product Name -->
<td class="product-name">
<?php
if ( ! $_product->is_visible() )
echo apply_filters( 'woocommerce_in_cart_product_title', $_product->get_title(), $values, $cart_item_key );
echo apply_filters( 'woocommerce_cart_item_name', $_product->get_title(), $cart_item, $cart_item_key );
else
echo apply_filters( 'woocommerce_in_cart_product_title', sprintf( '<a href="%s">%s</a>', get_permalink( $product_id ), $_product->get_title() ), $values, $cart_item_key );
echo apply_filters( 'woocommerce_cart_item_name', sprintf( '<a href="%s">%s</a>', get_permalink( $product_id ), $_product->get_title() ), $cart_item, $cart_item_key );
// Meta data
echo $woocommerce->cart->get_item_data( $values );
echo $woocommerce->cart->get_item_data( $cart_item );
// Backorder notification
if ( $_product->backorders_require_notification() && $_product->is_on_backorder( $values['quantity'] ) )
if ( $_product->backorders_require_notification() && $_product->is_on_backorder( $cart_item['quantity'] ) )
echo '<p class="backorder_notification">' . __( 'Available on backorder', 'woocommerce' ) . '</p>';
?>
</td>
<!-- Product price -->
<td class="product-price">
<?php
$product_price = get_option('woocommerce_tax_display_cart') == 'excl' ? $_product->get_price_excluding_tax() : $_product->get_price_including_tax();
echo apply_filters('woocommerce_cart_item_price_html', woocommerce_price( $product_price ), $values, $cart_item_key );
echo apply_filters( 'woocommerce_cart_item_price', $woocommerce->cart->get_product_price( $_product ), $cart_item, $cart_item_key );
?>
</td>
<!-- Quantity inputs -->
<td class="product-quantity">
<?php
if ( $_product->is_sold_individually() ) {
@ -99,24 +91,22 @@ wc_print_messages();
$min = apply_filters( 'woocommerce_quantity_input_min', '', $_product );
$max = apply_filters( 'woocommerce_quantity_input_max', $_product->backorders_allowed() ? '' : $_product->get_stock_quantity(), $_product );
$product_quantity = sprintf( '<div class="quantity"><input type="number" name="cart[%s][qty]" step="%s" min="%s" max="%s" value="%s" size="4" title="' . _x( 'Qty', 'Product quantity input tooltip', 'woocommerce' ) . '" class="input-text qty text" maxlength="12" /></div>', $cart_item_key, $step, $min, $max, esc_attr( $values['quantity'] ) );
$product_quantity = sprintf( '<div class="quantity"><input type="number" name="cart[%s][qty]" step="%s" min="%s" max="%s" value="%s" size="4" title="' . _x( 'Qty', 'Product quantity input tooltip', 'woocommerce' ) . '" class="input-text qty text" maxlength="12" /></div>', $cart_item_key, $step, $min, $max, esc_attr( $cart_item['quantity'] ) );
}
echo apply_filters( 'woocommerce_cart_item_quantity', $product_quantity, $cart_item_key );
?>
</td>
<!-- Product subtotal -->
<td class="product-subtotal">
<?php
echo apply_filters( 'woocommerce_cart_item_subtotal', $woocommerce->cart->get_product_subtotal( $_product, $values['quantity'] ), $values, $cart_item_key );
echo apply_filters( 'woocommerce_cart_item_subtotal', $woocommerce->cart->get_product_subtotal( $_product, $cart_item['quantity'] ), $cart_item, $cart_item_key );
?>
</td>
</tr>
<?php
}
}
}
do_action( 'woocommerce_cart_contents' );
?>

View File

@ -6,7 +6,7 @@
*
* @author WooThemes
* @package WooCommerce/Templates
* @version 1.6.4
* @version 2.1.0
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
@ -20,35 +20,31 @@ global $woocommerce;
<?php if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) : ?>
<?php foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $cart_item ) :
<?php
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $cart_item ) {
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
$product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
$_product = $cart_item['data'];
if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_widget_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
// Only display if allowed
if ( ! apply_filters('woocommerce_widget_cart_item_visible', true, $cart_item, $cart_item_key ) || ! $_product->exists() || $cart_item['quantity'] == 0 )
continue;
$product_name = apply_filters( 'woocommerce_cart_item_name', $_product->get_title(), $cart_item, $cart_item_key );
$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );
$product_price = apply_filters( 'woocommerce_cart_item_price', $woocommerce->cart->get_product_price( $_product ), $cart_item, $cart_item_key );
// Get price
$product_price = get_option( 'woocommerce_tax_display_cart' ) == 'excl' ? $_product->get_price_excluding_tax() : $_product->get_price_including_tax();
$product_price = apply_filters( 'woocommerce_cart_item_price_html', woocommerce_price( $product_price ), $cart_item, $cart_item_key );
?>
<li>
<a href="<?php echo get_permalink( $cart_item['product_id'] ); ?>">
<?php echo $_product->get_image(); ?>
<?php echo apply_filters('woocommerce_widget_cart_product_title', $_product->get_title(), $_product ); ?>
<a href="<?php echo get_permalink( $product_id ); ?>">
<?php echo $thumbnail . $product_name; ?>
</a>
<?php echo $woocommerce->cart->get_item_data( $cart_item ); ?>
<?php echo apply_filters( 'woocommerce_widget_cart_item_quantity', '<span class="quantity">' . sprintf( '%s &times; %s', $cart_item['quantity'], $product_price ) . '</span>', $cart_item, $cart_item_key ); ?>
</li>
<?php endforeach; ?>
<?php
}
}
?>
<?php else : ?>

View File

@ -4,7 +4,7 @@
*
* @author WooThemes
* @package WooCommerce/Templates
* @version 1.6.4
* @version 2.1.0
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
@ -115,22 +115,24 @@ $available_methods = $woocommerce->shipping->get_available_shipping_methods();
<?php
do_action( 'woocommerce_review_order_before_cart_contents' );
if (sizeof($woocommerce->cart->get_cart())>0) :
foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) :
$_product = $values['data'];
if ($_product->exists() && $values['quantity']>0) :
echo '
<tr class="' . esc_attr( apply_filters('woocommerce_checkout_table_item_class', 'checkout_table_item', $values, $cart_item_key ) ) . '">
<td class="product-name">' .
apply_filters( 'woocommerce_checkout_product_title', $_product->get_title(), $_product ) . ' ' .
apply_filters( 'woocommerce_checkout_item_quantity', '<strong class="product-quantity">&times; ' . $values['quantity'] . '</strong>', $values, $cart_item_key ) .
$woocommerce->cart->get_item_data( $values ) .
'</td>
<td class="product-total">' . apply_filters( 'woocommerce_checkout_item_subtotal', $woocommerce->cart->get_product_subtotal( $_product, $values['quantity'] ), $values, $cart_item_key ) . '</td>
</tr>';
endif;
endforeach;
endif;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $cart_item ) {
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_checkout_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
?>
<tr class="<?php echo esc_attr( apply_filters( 'woocommerce_cart_item_class', 'cart_item', $cart_item, $cart_item_key ) ); ?>">
<td class="product-name">
<?php echo apply_filters( 'woocommerce_cart_item_name', $_product->get_title(), $cart_item, $cart_item_key ); ?>
<?php echo apply_filters( 'woocommerce_checkout_cart_item_quantity', ' <strong class="product-quantity">' . sprintf( '&times; %s', $cart_item['quantity'] ) . '</strong>', $cart_item, $cart_item_key ); ?>
<?php echo $woocommerce->cart->get_item_data( $cart_item ); ?>
</td>
<td class="product-total">
<?php echo apply_filters( 'woocommerce_cart_item_subtotal', $woocommerce->cart->get_product_subtotal( $_product, $cart_item['quantity'] ), $cart_item, $cart_item_key ); ?>
</td>
</tr>
<?php
}
}
do_action( 'woocommerce_review_order_after_cart_contents' );
?>

View File

@ -4,7 +4,7 @@
*
* @author WooThemes
* @package WooCommerce/Templates/Emails
* @version 2.0.3
* @version 2.1.0
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
@ -12,24 +12,22 @@ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $woocommerce;
foreach ( $items as $item ) :
// Get/prep product data
$_product = $order->get_product_from_item( $item );
$_product = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );
$item_meta = new WC_Order_Item_Meta( $item['item_meta'] );
$image = ($show_image) ? '<img src="'. current(wp_get_attachment_image_src( get_post_thumbnail_id( $_product->id ), 'thumbnail')) .'" alt="Product Image" height="'.$image_size[1].'" width="'.$image_size[0].'" style="vertical-align:middle; margin-right: 10px;" />' : '';
?>
<tr>
<td style="text-align:left; vertical-align:middle; border: 1px solid #eee; word-wrap:break-word;"><?php
// Show title/image etc
echo apply_filters( 'woocommerce_order_product_image', $image, $_product, $show_image);
if ( $show_image )
echo apply_filters( 'woocommerce_order_item_thumbnail', '<img src="' . current( wp_get_attachment_image_src( get_post_thumbnail_id( $_product->id ), 'thumbnail') ) .'" alt="Product Image" height="' . $image_size[1] . '" width="' . $image_size[0] . '" style="vertical-align:middle; margin-right: 10px;" />', $item );
// Product name
echo apply_filters( 'woocommerce_order_product_title', $item['name'], $_product );
echo apply_filters( 'woocommerce_order_item_name', $item['name'], $item );
// SKU
echo ($show_sku && $_product->get_sku()) ? ' (#' . $_product->get_sku() . ')' : '';
if ( $show_sku && $_product->get_sku() )
echo ' (#' . $_product->get_sku() . ')';
// File URLs
if ( $show_download_links && $_product->exists() && $_product->is_downloadable() ) {
@ -55,7 +53,8 @@ foreach ($items as $item) :
}
// Variation
echo ($item_meta->meta) ? '<br/><small>' . nl2br( $item_meta->display( true, true ) ) . '</small>' : '';
if ( $item_meta->meta )
echo '<br/><small>' . nl2br( $item_meta->display( true, true ) ) . '</small>';
?></td>
<td style="text-align:left; vertical-align:middle; border: 1px solid #eee;"><?php echo $item['qty'] ;?></td>

View File

@ -4,7 +4,7 @@
*
* @author WooThemes
* @package WooCommerce/Templates/Emails/Plain
* @version 2.0.0
* @version 2.1.0
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
@ -12,14 +12,15 @@ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $woocommerce;
foreach ( $items as $item ) :
// Get/prep product data
$_product = $order->get_product_from_item( $item );
$_product = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );
$item_meta = new WC_Order_Item_Meta( $item['item_meta'] );
// Title, sku, qty, price
echo apply_filters( 'woocommerce_order_product_title', $item['name'], $_product );
echo $show_sku && $_product->get_sku() ? ' (#' . $_product->get_sku() . ')' : '';
// Title
echo apply_filters( 'woocommerce_order_item_name', $item['name'], $item );
// SKU
if ( $show_sku && $_product->get_sku() )
echo ' (#' . $_product->get_sku() . ')';
// Variation
echo $item_meta->meta ? "\n" . nl2br( $item_meta->display( true, true ) ) : '';

View File

@ -38,19 +38,20 @@ $order = new WC_Order( $order_id );
if ( sizeof( $order->get_items() ) > 0 ) {
foreach( $order->get_items() as $item ) {
$_product = get_product( $item['variation_id'] ? $item['variation_id'] : $item['product_id'] );
echo '<tr class = "' . esc_attr( apply_filters( 'woocommerce_order_table_item_class', 'order_table_item', $item, $order ) ) . '">
<td class="product-name">';
if ( $_product && ! $_product->is_visible() )
echo apply_filters( 'woocommerce_order_table_product_title', $item['name'], $item );
else
echo apply_filters( 'woocommerce_order_table_product_title', sprintf( '<a href="%s">%s</a>', get_permalink( $item['product_id'] ), $item['name'] ), $item );
echo apply_filters( 'woocommerce_order_table_item_quantity', ' <strong class="product-quantity">&times; ' . $item['qty'] . '</strong>', $item );
$_product = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );
$item_meta = new WC_Order_Item_Meta( $item['item_meta'] );
?>
<tr class="<?php echo esc_attr( apply_filters( 'woocommerce_order_item_class', 'order_item', $item, $order ) ); ?>">
<td class="product-name">
<?php
if ( $_product && ! $_product->is_visible() )
echo apply_filters( 'woocommerce_order_item_name', $item['name'], $item );
else
echo apply_filters( 'woocommerce_order_item_name', sprintf( '<a href="%s">%s</a>', get_permalink( $item['product_id'] ), $item['name'] ), $item );
echo apply_filters( 'woocommerce_order_item_quantity', ' <strong class="product-quantity">' . sprintf( '&times; %s', $item['qty'] ) . '</strong>', $item );
$item_meta->display();
if ( $_product && $_product->exists() && $_product->is_downloadable() && $order->is_download_permitted() ) {
@ -71,15 +72,21 @@ $order = new WC_Order( $order_id );
echo implode( '<br/>', $links );
}
?>
</td>
<td class="product-total">
<?php echo $order->get_formatted_line_subtotal( $item ); ?>
</td>
</tr>
<?php
echo '</td><td class="product-total">' . $order->get_formatted_line_subtotal( $item ) . '</td></tr>';
// Show any purchase notes
if ($order->status=='completed' || $order->status=='processing') {
if ($purchase_note = get_post_meta( $_product->id, '_purchase_note', true))
echo '<tr class="product-purchase-note"><td colspan="3">' . apply_filters('the_content', $purchase_note) . '</td></tr>';
if ( in_array( $order->status, array( 'processing', 'completed' ) ) && ( $purchase_note = get_post_meta( $_product->id, '_purchase_note', true ) ) ) {
?>
<tr class="product-purchase-note">
<td colspan="3"><?php echo apply_filters( 'the_content', $purchase_note ); ?></td>
</tr>
<?php
}
}
}