Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

WooCommerce Category Shortcode WordPress

Hi there,

I need a new shortcode based on the WooCommerce product_category Shortcode, that does two things differently:

1. In the current product_category shortcode, it says select options if it's a variable product and then takes you to the single product with the variation choice. I need that variable choice visible in the category shortcode rather than taking me to the single product with the ajax and resulting add to cart. If it's a non-variable product, it should just have the 'add to cart' button.

2. I need it to include the product definition after the title.

For a more visual sense - I'm trying to get the top section on http://aspenware.wpengine.com/about/product-line/
to match the bottom (which is mostly just CSS and there's no need to worry about that) and the Bulk Forks (as an example) to not say select options, but instead offer the option to choose the package quantity (as seen on its single product page).

CW

Answers (2)

2013-07-16

Arnav Joy answers:

can you explain your question more it is not clear


Christopher comments:

Sorry, I'm not sure what's unclear about the question.

I need to make a modified version of a woo commerce shortcode, that does two things differently.

2013-07-16

John Cotton answers:

Do you always want to show the drop down list or only when it's used in this shortcode?


Christopher comments:

It could function this way on the 'store' page as well? That would be preferable.


John Cotton comments:

<blockquote>It could function this way on the 'store' page as well? That would be preferable.</blockquote>
..by which I assume you mean "all the time".

In which case overriding the default template file is the cleanest option.

In your theme folder, create this folder structure:

woocommerce/loop/

and in that folder create a file called add-to-cart.php with these contents:


<?php
/**
* Loop Add to Cart
*
* @author WooThemes
* @package WooCommerce/Templates
* @version 1.6.4
*/

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

global $product;
?>

<?php 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' => ''
);

$handler = apply_filters( 'woocommerce_add_to_cart_handler', $product->product_type, $product );

// This is the change - insert the full add to cart rather than the action commented out below
if( $handler == "variable" ) {
woocommerce_variable_add_to_cart();
} else {
switch ( $handler ) {
// 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;
}

echo apply_filters( 'woocommerce_loop_add_to_cart_link', sprintf('<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" class="%s button product_type_%s">%s</a>', esc_url( $link['url'] ), esc_attr( $product->id ), esc_attr( $product->get_sku() ), esc_attr( $link['class'] ), esc_attr( $product->product_type ), esc_html( $link['label'] ) ), $product, $link );
}

?>

<?php endif; ?>


Woo will use that file instead of it's own and that should give you what you want.


Christopher comments:

Thanks John. That works well, aside from the fact that it doesn't do the Ajax checkmark and view cart and instead reloads the page, without a link to the cart. The direct 'add to cart' buttons give a checkmark and link to 'view cart'.

See: http://aspenware.wpengine.com/about/product-line/


John Cotton comments:

I'm not certain, but I don't think ajax add to cart for variants is supported yet.

There's some discussion [[LINK href="http://ideas.woothemes.com/forums/133476-woocommerce/suggestions/3777548-product-variations-on-category-product-list-page-w"]]on it here[[/LINK]].


John Cotton comments:

...I think the problem arises from the extra info required (and the fact that variants could vary by several different things eg colour AND size) whereas the ajax add to cart just sends back the product id.