WordPress WooCommerce: give another button 'add to cart' function - wordpress

I'd like to give another html element the function of 'add to cart'. Is this possible? I have added some html code into single-product.php and want one of these elements to function as 'add to cart' when you click on them :)
I hope that I've been able to explain myself :P

The following will work as an add to cart button within a product loop, where $product is the post object -
<?php if( $product->is_purchasable() ):?>
<form class="cart" method="post" enctype='multipart/form-data'>
<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<input type="hidden" name="add-to-cart" value="<?php echo esc_attr( $product->id ); ?>" />
<button type="submit">Add to Cart</button>
<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
</form>
<?php endif; ?>

Related

How to add a text before the sorting option in woocommerce

I've been looking around the net but all they have is to teach you how to add/edit/remove an option in the sorting dropdown. But my template requires me to add a 'Sort By' text. Like this:
I tried adding it via Jquery but it keeps being gone after ajax filters.
Please don't rank this down, there might be someone who forgot that there is a format file for this in themes/woocomerce/loop/orderby.php
Then just edit the code like this:
<form class="woocommerce-ordering" method="get">
<span class="sort-name">Sort By</span>
<select name="orderby" class="orderby">
<?php foreach ( $catalog_orderby_options as $id => $name ) : ?>
<option value="<?php echo esc_attr( $id ); ?>" <?php selected( $orderby, $id ); ?>>
<?php echo esc_html( $name ); ?>
</option>
<?php endforeach; ?>
</select>
<input type="hidden" name="paged" value="1" />
<?php wc_query_string_form_fields( null, array( 'orderby', 'submit', 'paged', 'product-page' ) ); ?>

Custom woocommerce coupon fields in custom place

I created a custom checkout page.
I wanted to put the coupon code anywhere and it worked.
Unfortunately after clicking on the "apply coupon" button, instead of validating the coupon and adding it to the order, the system responds as per clicking the "order and pay" button Code used in form-checkout.php:
<?php do_action('woocommerce_checkout_coupon_form', $checkout); ?>
<form name="checkout" method="post" class="checkout woocommerce-checkout" action="<?php echo esc_url( wc_get_checkout_url() ); ?>" enctype="multipart/form-data">
<div class="checkout-form-container">
<?php if ( sizeof( $checkout->checkout_fields ) > 0 ) : ?>
<?php do_action( 'woocommerce_checkout_before_customer_details' ); ?>
<div class="col2-set" id="customer_details">
<div class="col-1">
<?php do_action( 'woocommerce_checkout_billing' ); ?>
</div>
</div>
<?php do_action( 'woocommerce_checkout_after_customer_details'); ?>
<p class="form-row form-row-first">
<label for="coupon_code">Coupon?</label>
<input type="text" name="coupon_code" class="custom-coupon-code-input input-text" placeholder="<?php esc_attr_e( 'Wprowadż kod', 'woocommerce' ); ?>" id="coupon_code" value="" />
</p>
<p class="form-row form-row-last">
<input type="submit"button class="button custom-apply-coupon-btn">
<?php esc_attr_e( 'Apply', 'woocommerce' ); ?>
</button>
</p>
<?php endif; ?>
</div>
Check your checkout page HTML. You are actually putting your coupon form inside checkout form. HTML forms doesn't support nested forms.
Put the form outside of checkout form. If you really need to show them there then take those values by javascript with the click of 'Apply Coupon' button and do the validation. But A form tag inside another form is not the correct way.
The submit button is submitting parent form also. That's why you are getting that.
I know where is the problem. Everything works perfectly on woo 2.6.8 but after update to min 3.0.4 its crash as I described above.

Two Place Order Button in woocommerce Checkout page

I am having Issue in my checkout page. I am getting Two Place order button(click here). I tried to fix it in form-checkout.php even I checked in form-pay.php, in wp-content/themes/mytheme/woocommerce/checkout.
I checked in wordpress forum I got solution to remove two checkout button from cart page, there is no solution for my problem
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $woocommerce;
wc_print_notices();
do_action( 'woocommerce_before_checkout_form', $checkout );
// filter hook for include new pages inside the payment method
$get_checkout_url = apply_filters( 'woocommerce_get_checkout_url', WC()->cart->get_checkout_url() ); ?>
<form name="checkout" method="post" class="checkout" action="<?php echo esc_url( $get_checkout_url ); ?>">
<?php if ( sizeof( $checkout->checkout_fields ) > 0 ) : ?>
<?php do_action( 'woocommerce_checkout_before_customer_details' ); ?>
<div class="row" id="customer_details">
<div class="col-md-6">
<?php do_action( 'woocommerce_checkout_billing' ); ?>
</div>
<div class="col-md-6">
<?php do_action( 'woocommerce_checkout_shipping' ); ?>
</div>
</div>
<?php do_action( 'woocommerce_checkout_after_customer_details' ); ?>
<h3 id="order_review_heading"><?php _e( 'Your order', 'woocommerce' ); ?></h3>
<?php endif; ?>
<?php do_action( 'woocommerce_checkout_order_review' ); ?>
</form>
<?php do_action( 'woocommerce_after_checkout_form' ); ?>
Go to wp-content/themes/mytheme/woocommerce/checkout/view-order.php. In View-order.php remove id="payment" completle
This issue is because of theme version
Please check your configuration in your WordPress admin.
Woocommers->settings on the Payment Tab check your information details if you have miss an HTML tags.
I have experience on this when I customized or add some html tags on the details text area with closing "div" tag.

Comments not showing after using comment_form in Wordpress

I am using comment_form() in Wordpress, and the comment form is showing up, but the actual comments that have been approved are not:
Here is the code I have
<?php
$comments_args = array(
// remove "Text or HTML to be displayed after the set of comment fields"
'comment_notes_after' => 'Note: Comments are moderated so will not publish immediately.',
// change "Leave a Reply" to "Comment"
'title_reply'=>'What did you think of this story? Please share a comment.',
// change the post comment button
'label_submit' => __( 'Submit your comment' ),
);
comment_form($comments_args);
?>
I have unchecked and checked the right options under Settings -> Discussion
Do I need another call apart from comment_form?
Does it make a difference that this is a custom post type?
Thanks for any help.
Try to use the comments.php file from the twentyfourteen theme. If it works, it is likely that nishant is right with comments probably not being activated for your post type.
TwentyFourteen’s comments.php is also a good example on best practise for comments. You might check it against your other code, if there is some.
It doesn't make any difference if it is a custom post type but you should declare comment support for your custom post type in function.php where you have written arguments for custom post type. e.g. 'supports' => array( 'title' ,'author','comments').
And Use like this in single-post-type.php write <?php comments_template( '', true ); ?> to call comments.php.
And in take your comments.php as backup and replace it with the following -
<?php
/**
* The template for displaying Comments
*
* The area of the page that contains both current comments
* and the comment form. The actual display of comments is
* handled by a callback to twentytwelve_comment() which is
* located in the functions.php file.
*
* #package WordPress
* #subpackage Twenty_Twelve
* #since Twenty Twelve 1.0
*/
/*
* If the current post is protected by a password and
* the visitor has not yet entered the password we will
* return early without loading the comments.
*/
if ( post_password_required() )
return;
?>
<div id="comments" class="comments-area">
<?php // You can start editing here -- including this comment! ?>
<?php if ( have_comments() ) : ?>
<h2 class="comments-title">
<?php
printf( _n( 'One thought on “%2$s”', '%1$s thoughts on “%2$s”', get_comments_number(), 'twentytwelve' ),
number_format_i18n( get_comments_number() ), '<span>' . get_the_title() . '</span>' );
?>
</h2>
<ol class="commentlist">
<?php wp_list_comments( array( 'callback' => 'twentytwelve_comment', 'style' => 'ol' ) ); ?>
</ol><!-- .commentlist -->
<?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : // are there comments to navigate through ?>
<nav id="comment-nav-below" class="navigation" role="navigation">
<h1 class="assistive-text section-heading"><?php _e( 'Comment navigation', 'twentytwelve' ); ?></h1>
<div class="nav-previous"><?php previous_comments_link( __( '← Older Comments', 'twentytwelve' ) ); ?></div>
<div class="nav-next"><?php next_comments_link( __( 'Newer Comments →', 'twentytwelve' ) ); ?></div>
</nav>
<?php endif; // check for comment navigation ?>
<?php
/* If there are no comments and comments are closed, let's leave a note.
* But we only want the note on posts and pages that had comments in the first place.
*/
if ( ! comments_open() && get_comments_number() ) : ?>
<p class="nocomments"><?php _e( 'Comments are closed.' , 'twentytwelve' ); ?></p>
<?php endif; ?>
<?php endif; // have_comments() ?>
<?php //comment_form(); ?>
<?php if ('open' == $post->comment_status) : ?>
<div id="respond" class="comment-respond">
<h3 id="reply-title" class="comment-reply-title"><?php comment_form_title( 'Leave a Reply', 'Leave a Reply to %s' ); ?></h3>
<div id="cancel-comment-reply">
<p class="small"><?php cancel_comment_reply_link( __("Cancel Reply", "bonestheme") ); ?></p>
</div>
<div class="cancel-comment-reply">
<?php cancel_comment_reply_link(); ?>
</div>
<?php if ( get_option('comment_registration') && !$user_ID ) : ?>
<p>You must be logged in to post a comment.</p>
<?php else : ?>
<form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform">
<?php if ( $user_ID ) : ?>
<p>Logged in as <?php echo $user_identity; ?>. Log out »</p>
<?php else : ?>
<p class="comment-notes">Your email address will not be published. Required fields are marked <span class="required">*</span></p>
<p class="comment-form-author"><label for="author">Name <?php if ($req) echo '<span class="required">*</span>'; ?></label>
<input type="text" name="author" id="author" value="<?php //echo $comment_author; ?>" size="22" tabindex="1" /></p>
<p class="comment-form-email"><label for="email">Mail <?php if ($req) echo '<span class="required">*</span>'; ?></label>
<input type="text" name="email" id="email" value="<?php //echo $comment_author_email; ?>" size="22" tabindex="2" /></p>
<p class="comment-form-url"><label for="url">Website</label>
<input type="text" name="url" id="url" value="<?php echo $comment_author_url; ?>" size="22" tabindex="3" /></p>
<?php endif; ?>
<!--<p class="form-allowed-tags"><small><strong>XHTML:</strong> You can use these tags: <code><?php echo allowed_tags(); ?></code></small></p>-->
<p class="comment-form-comment"><p><label for="comment">Comment <?php if ($req) echo '<span class="required">*</span>'; ?></label>
<textarea name="comment" id="comment" cols="100%" rows="10" tabindex="4"></textarea></p>
<p class="form-submit"><input name="submit" type="submit" id="submit" tabindex="5" value="Post Comment" />
<?php comment_id_fields(); ?>
</p>
<?php do_action('comment_form', $post->ID); ?>
</form>
<?php endif; // If registration required and not logged in ?>
</div>
<?php endif; // if you delete this the sky will fall on your head ?>
</div><!-- #comments .comments-area -->
Note- Please change class name and id as per your css

Woocommerce - add to cart on content-product.php

im trying to put add to cart on content-product.php but without success.
I need a button that when the person clicks add to cart automatically.
I tried to put this:
<form class="cart" method="post" enctype='multipart/form-data'>
<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<input type="hidden" name="add-to-cart" value="<?php echo esc_attr( $product->id ); ?>" />
<button type="submit" class="single_add_to_cart_button button alt"><?php echo $product->single_add_to_cart_text(); ?></button>
<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
</form>
The button looks ok but dont add to cart...
Someone can help me?
Im using Woocommerce 2.1.5
Thanks so much
Adding:
do_action( 'woocommerce_after_shop_loop_item' );
to the content-product.php file should add the "Add to cart" button.

Resources