Skip to content

Instantly share code, notes, and snippets.

@orionrush
Forked from claudiosanches/add-to-cart.php
Last active August 29, 2015 14:01
Show Gist options
  • Save orionrush/5ff38419699d7c2fe508 to your computer and use it in GitHub Desktop.
Save orionrush/5ff38419699d7c2fe508 to your computer and use it in GitHub Desktop.
A quick plugin to add a quantity to WooCommerce's 'add to cart' button
<?php
/**
* Plugin Name: WooCommerce XNum to cart
* Plugin URI: http://claudiosmweb.com/snippets/woocommerce-loop-de-produtos-com-quantidade/
* Source: http://docs.woothemes.com/document/override-loop-template-and-show-quantities-next-to-add-to-cart-buttons/
* Description: Add x-number of items feild to the 'add to cart' button in WooCommerce
* Version: .10
* Author: orionrush
* Note: You will have to style the button css according to your theme's needes.
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/**
* Add some styles to quantity
*
* Styles should be enqued for sure, but this is here as a proof of principle.
* Add these styles to your theme's css styles, adding salt to taste.
*
* @author orion.rush
*
*/
//add_action( 'wp_head', 'add_styles_to_quantity' );
function add_styles_to_quantity() {
echo "<style media='screen'>.archive.woocommerce-page .quantity.buttons_added { float: left; margin-right: 4px!important; }</style>";
}
/**
* Custom Loop for Add to Cart - modified
*
* Template with quantity.
*
* @author WooThemes
* @package WooCommerce/Templates
* @version 1.6.4
*/
add_filter('woocommerce_loop_add_to_cart_link','xNum');
function xNum() {
global $product;
if ( ! $product->is_in_stock() ) : ?>
<a href="<?php echo apply_filters( 'out_of_stock_add_to_cart_url', get_permalink( $product->id ) ); ?>" class="button"><?php echo apply_filters( 'out_of_stock_add_to_cart_text', __( 'Read More', 'woocommerce' ) ); ?></a>
<?php else : ?>
<?php
$link = array(
'url' => '',
'label' => '',
'class' => ''
);
switch ( $product->product_type ) {
case "variable" :
$link['url'] = apply_filters( 'variable_add_to_cart_url', get_permalink( $product->id ) );
$link['label'] = apply_filters( 'variable_add_to_cart_text', __( 'Select options', 'woocommerce' ) );
break;
case "grouped" :
$link['url'] = apply_filters( 'grouped_add_to_cart_url', get_permalink( $product->id ) );
$link['label'] = apply_filters( 'grouped_add_to_cart_text', __( 'View options', 'woocommerce' ) );
break;
case "external" :
$link['url'] = apply_filters( 'external_add_to_cart_url', get_permalink( $product->id ) );
$link['label'] = apply_filters( 'external_add_to_cart_text', __( 'Read More', 'woocommerce' ) );
break;
default :
if ( $product->is_purchasable() ) {
$link['url'] = apply_filters( 'add_to_cart_url', esc_url( $product->add_to_cart_url() ) );
$link['label'] = apply_filters( 'add_to_cart_text', __( 'Add to cart', 'woocommerce' ) );
$link['class'] = apply_filters( 'add_to_cart_class', 'add_to_cart_button' );
} else {
$link['url'] = apply_filters( 'not_purchasable_url', get_permalink( $product->id ) );
$link['label'] = apply_filters( 'not_purchasable_text', __( 'Read More', 'woocommerce' ) );
}
break;
}
// If there is a simple product.
if ( $product->product_type == 'simple' ) {
?>
<form action="<?php echo esc_url( $product->add_to_cart_url() ); ?>" class="cart" method="post" enctype="multipart/form-data">
<?php
// Displays the quantity box.
woocommerce_quantity_input(); ?>
<button type="submit" class="button alt"><?php echo $link['label']; ?></button>
</form>
<?php
} else {
printf('<a href="%s" rel="nofollow" data-product_id="%s" class="button add_to_cart_button product_type_%s">%s</a>',
$link['url'],
$product->id,
$product->product_type,
$link['label']
);
}
?>
<?php endif;
}
/**
* Add to cart messages.
* Change output texts to include number of items.
*
*
* @author orion.rush
* @package WooCommerce/includes/wc-cart-functions.php
* @version 2.1.9
*
* @access public
* @param int|array $product_id
* @return void
*/
add_filter( 'wc_add_to_cart_message', 'xNum_messages', 10, 2 );
function xNum_messages($message, $product_id) {
if ( is_array( $product_id ) ) {
$titles = array();
foreach ( $product_id as $id ) {
$titles[] = get_the_title( $id );
}
echo print_r($titles);
$added_text = sprintf( __( '&quot;%s&quot; to your cart.', 'woocommerce' ), join( __( '&quot; and &quot;', 'woocommerce' ), array_filter( array_merge( array( join( '&quot;, &quot;', array_slice( $titles, 0, -1 ) ) ), array_slice( $titles, -1 ) ) ) ) );
} else {
$added_text = sprintf( __( '&quot;%s&quot; was successfully added to your cart.', 'woocommerce' ), get_the_title( $product_id ) );
}
// Output success messages
if ( get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) :
$return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wp_get_referer() ? wp_get_referer() : home_url() );
$message = sprintf(
'<a href="%s" class="alert-link">%s &rarr;</a> %s',
$return_to, __( 'Continue Shopping', 'woocommerce' ),
$added_text
);
else :
$message = sprintf(
'<a href="%s" class="alert-link">%s &rarr;</a> %s',
get_permalink( wc_get_page_id( 'cart' ) ),
__( 'View Cart', 'woocommerce' ),
$added_text );
endif;
return $message;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment