-
-
Save sukhikh18/48a85de2734c63ced7f802753d7d470b to your computer and use it in GitHub Desktop.
Display Product Variations in the Shop Loop - With Conditional Apply Filter Logic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Custom Loop Add to Cart. | |
* | |
* Template with quantity and ajax. | |
* @since Woocommerce 3.0.0 | |
*/ | |
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->get_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->get_type() ) { | |
case "variable" : | |
woocommerce_variable_add_to_cart(); | |
// its not filter.. not now. | |
// $link['url'] = apply_filters( 'woocommerce_variable_add_to_cart', get_permalink( $product->get_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->get_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->get_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->get_id() ) ); | |
$link['label'] = apply_filters( 'not_purchasable_text', __( 'Read More', 'woocommerce' ) ); | |
} | |
break; | |
} | |
// If there is a simple product. | |
if ( $product->get_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(); | |
// Display the submit button. | |
echo sprintf( '<button type="submit" data-product_id="%s" data-product_sku="%s" data-quantity="1" class="%s button product_type_simple">%s</button>', esc_attr( $product->get_id() ), esc_attr( $product->get_sku() ), esc_attr( $link['class'] ), esc_html( $link['label'] ) ); | |
?> | |
</form> | |
<?php | |
} else { | |
$button = 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->get_id() ), | |
esc_attr( $product->get_sku() ), | |
esc_attr( $link['class'] ), | |
esc_attr( $product->get_type() ), | |
esc_html( $link['label'] ) | |
), | |
$product, | |
$link | |
); | |
} | |
?> | |
<?php endif; ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment