Created
October 10, 2014 06:47
-
-
Save vishalbasnet23/5178697c48981ea3bdfd to your computer and use it in GitHub Desktop.
Creating add to cart button for simple products in Archive Page Woocommerce
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
function custom_add_to_cart_archieve($post_id_as_product) { | |
global $woocommerce; | |
$product_factory_custom = new WC_Product_Factory(); | |
$product_object_by_id = $product_factory_custom->get_product($post_id_as_product); | |
// var_dump($product_object_by_id); | |
if ( ! $product_object_by_id->is_purchasable() ) return; | |
?> | |
<?php | |
// Availability | |
$availability = $product_object_by_id->get_availability(); | |
if ( $availability['availability'] ) | |
echo apply_filters( 'woocommerce_stock_html', '<p class="stock ' . esc_attr( $availability['class'] ) . '">' . esc_html( $availability['availability'] ) . '</p>', $availability['availability'] ); | |
?> | |
<?php if ( $product_object_by_id->is_in_stock() ) : ?> | |
<?php do_action( 'woocommerce_before_add_to_cart_form' ); ?> | |
<form class="cart" method="post" enctype='multipart/form-data'> | |
<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?> | |
<td class="checkout_upsell_column4"><?php | |
if ( ! $product_object_by_id->is_sold_individually() ) | |
woocommerce_quantity_input( array( | |
'min_value' => apply_filters( 'woocommerce_quantity_input_min', 1, $product_object_by_id ), | |
'max_value' => apply_filters( 'woocommerce_quantity_input_max', $product_object_by_id->backorders_allowed() ? '' : $product_object_by_id->get_stock_quantity(), $product_object_by_id ) | |
) ); | |
?></td> | |
<input type="hidden" name="add-to-cart" value="<?php echo esc_attr( $product_object_by_id->id ); ?>" /> | |
<td class="checkout_upsell_column5"> | |
<button type="submit" class="checkout_btn single_add_to_cart_button button alt"><?php echo $product_object_by_id->add_to_cart_text(); ?></button> | |
</td> | |
<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?> | |
</form> | |
<?php do_action( 'woocommerce_after_add_to_cart_form' ); ?> | |
<?php endif; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment