Change payment methods location before order details table in Woocommerce - php

I need to alter the default Woocommerce checkout a little bit. I need to move the payment options ABOVE the order review table, while keeping the "Place Order" button at the bottom below the order review table. I currently have
remove_action('woocommerce_checkout_order_review','woocommerce_checkout_payment', 20 );
add_action( 'woocommerce_checkout_order_review', 'woocommerce_checkout_payment', 5 );
This moves the payment box above the table, but it also moves the button. How can I keep the button at the bottom?

Here below, you will find the necessary code to reorder checkout order review section. This code will put the payment methods and gateways before the checkout review order table and will keep the "Place Order" button at the end.
The code:
add_action( 'woocommerce_checkout_order_review', 'reordering_checkout_order_review', 1 );
function reordering_checkout_order_review(){
remove_action('woocommerce_checkout_order_review','woocommerce_checkout_payment', 20 );
add_action( 'woocommerce_checkout_order_review', 'custom_checkout_payment', 8 );
add_action( 'woocommerce_checkout_order_review', 'custom_checkout_place_order', 20 );
}
function custom_checkout_payment() {
$checkout = WC()->checkout();
if ( WC()->cart->needs_payment() ) {
$available_gateways = WC()->payment_gateways()->get_available_payment_gateways();
WC()->payment_gateways()->set_current_gateway( $available_gateways );
} else {
$available_gateways = array();
}
if ( ! is_ajax() ) {
// do_action( 'woocommerce_review_order_before_payment' );
}
?>
<div id="payment" class="woocommerce-checkout-payment-gateways">
<?php if ( WC()->cart->needs_payment() ) : ?>
<ul class="wc_payment_methods payment_methods methods">
<?php
if ( ! empty( $available_gateways ) ) {
foreach ( $available_gateways as $gateway ) {
wc_get_template( 'checkout/payment-method.php', array( 'gateway' => $gateway ) );
}
} else {
echo '<li class="woocommerce-notice woocommerce-notice--info woocommerce-info">';
echo apply_filters( 'woocommerce_no_available_payment_methods_message', WC()->customer->get_billing_country() ? esc_html__( 'Sorry, it seems that there are no available payment methods for your state. Please contact us if you require assistance or wish to make alternate arrangements.', 'woocommerce' ) : esc_html__( 'Please fill in your details above to see available payment methods.', 'woocommerce' ) ) . '</li>'; // #codingStandardsIgnoreLine
}
?>
</ul>
<?php endif; ?>
</div>
<?php
}
function custom_checkout_place_order() {
$checkout = WC()->checkout();
$order_button_text = apply_filters( 'woocommerce_order_button_text', __( 'Place order', 'woocommerce' ) );
?>
<div id="payment-place-order" class="woocommerce-checkout-place-order">
<div class="form-row place-order">
<noscript>
<?php esc_html_e( 'Since your browser does not support JavaScript, or it is disabled, please ensure you click the <em>Update Totals</em> button before placing your order. You may be charged more than the amount stated above if you fail to do so.', 'woocommerce' ); ?>
<br/><button type="submit" class="button alt" name="woocommerce_checkout_update_totals" value="<?php esc_attr_e( 'Update totals', 'woocommerce' ); ?>"><?php esc_html_e( 'Update totals', 'woocommerce' ); ?></button>
</noscript>
<?php wc_get_template( 'checkout/terms.php' ); ?>
<?php do_action( 'woocommerce_review_order_before_submit' ); ?>
<?php echo apply_filters( 'woocommerce_order_button_html', '<button type="submit" class="button alt" name="woocommerce_checkout_place_order" id="place_order" value="' . esc_attr( $order_button_text ) . '" data-value="' . esc_attr( $order_button_text ) . '">' . esc_html( $order_button_text ) . '</button>' ); // #codingStandardsIgnoreLine ?>
<?php do_action( 'woocommerce_review_order_after_submit' ); ?>
<?php wp_nonce_field( 'woocommerce-process_checkout', 'woocommerce-process-checkout-nonce' ); ?>
</div>
</div>
<?php
if ( ! is_ajax() ) {
do_action( 'woocommerce_review_order_after_payment' );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and work.

Related

How to display order summary on checkout page?

I am working on a woo-commerce theme and it's good but on the checkout page clients want's to change the order of some blocks. so he wanted to move payment methods before order summary. I did that by adding the code in function.php which is given below. The code works fine but after adding the code i can't see order summary on checkout page. I can't see Subtotal, Shipment and Total Cost. I don't know what is wrong with this code. please help me out with this.
add_action( 'woocommerce_checkout_order_review', 'reordering_checkout_order_review', 1 );
function reordering_checkout_order_review(){
remove_action('woocommerce_checkout_order_review','woocommerce_checkout_payment', 20 );
add_action( 'woocommerce_checkout_order_review', 'custom_checkout_payment', 8 );
add_action( 'woocommerce_checkout_order_review', 'after_custom_checkout_payment', 9 );
add_action( 'woocommerce_checkout_order_review', 'custom_checkout_place_order', 20 );
}
function after_custom_checkout_payment() {
?>
<div id="before-order-table" class="woocommerce-checkout-custom-text">
<h3><?php _e("Your order", "woocommerce"); ?></h3>
<p><?php /* _e("I have accepted the terms and bla bla bla", "woocommerce"); */?></p>
</div>
<?php
}
function custom_checkout_payment() {
$checkout = WC()->checkout();
if ( WC()->cart->needs_payment() ) {
$available_gateways = WC()->payment_gateways()->get_available_payment_gateways();
WC()->payment_gateways()->set_current_gateway( $available_gateways );
} else {
$available_gateways = array();
}
if ( ! is_ajax() ) {
// do_action( 'woocommerce_review_order_before_payment' );
}
?>
<div id="payment" class="woocommerce-checkout-payment-gateways">
<?php if ( WC()->cart->needs_payment() ) : ?>
<ul class="wc_payment_methods payment_methods methods">
<?php
if ( ! empty( $available_gateways ) ) {
foreach ( $available_gateways as $gateway ) {
wc_get_template( 'checkout/payment-method.php', array( 'gateway' => $gateway ) );
}
} else {
echo '<li class="woocommerce-notice woocommerce-notice--info woocommerce-info">';
echo apply_filters( 'woocommerce_no_available_payment_methods_message', WC()->customer->get_billing_country() ? esc_html__( 'Sorry, it seems that there are no available payment methods for your state. Please contact us if you require assistance or wish to make alternate arrangements.', 'woocommerce' ) : esc_html__( 'Please fill in your details above to see available payment methods.', 'woocommerce' ) ) . '</li>'; // #codingStandardsIgnoreLine
}
?>
</ul>
<?php endif; ?>
</div>
<?php
}
function custom_checkout_place_order() {
$checkout = WC()->checkout();
$order_button_text = apply_filters( 'woocommerce_order_button_text', __( 'Place order', 'woocommerce' ) );
?>
<div id="payment-place-order" class="woocommerce-checkout-place-order">
<div class="form-row place-order">
<noscript>
<?php esc_html_e( 'Since your browser does not support JavaScript, or it is disabled, please ensure you click the <em>Update Totals</em> button before placing your order. You may be charged more than the amount stated above if you fail to do so.', 'woocommerce' ); ?>
<br/><button type="submit" class="button alt" name="woocommerce_checkout_update_totals" value="<?php esc_attr_e( 'Update totals', 'woocommerce' ); ?>"><?php esc_html_e( 'Update totals', 'woocommerce' ); ?></button>
</noscript>
<?php wc_get_template( 'checkout/terms.php' ); ?>
<?php do_action( 'woocommerce_review_order_before_submit' ); ?>
<?php echo apply_filters( 'woocommerce_order_button_html', '<button type="submit" class="button alt" name="woocommerce_checkout_place_order" id="place_order" value="' . esc_attr( $order_button_text ) . '" data-value="' . esc_attr( $order_button_text ) . '">' . esc_html( $order_button_text ) . '</button>' ); // #codingStandardsIgnoreLine ?>
<?php do_action( 'woocommerce_review_order_after_submit' ); ?>
<?php wp_nonce_field( 'woocommerce-process_checkout', 'woocommerce-process-checkout-nonce' ); ?>
</div>
</div>
<?php
if ( ! is_ajax() ) {
do_action( 'woocommerce_review_order_after_payment' );
}
}
You can also check with:
add_filter( 'woocommerce_checkout_fields' , 'custom_order_comments_checkout_fields' );
function custom_order_comments_checkout_fields( $fields ) {
if ( !WC()->cart->is_empty()):
$output = '';
$count = 0;
$cart_item_count = WC()->cart->get_cart_contents_count();
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ):
$count++;
// Displaying the product title
$output .= 'Product title: ' . $cart_item['data']->post->post_title;
// New line (for next item)
if($count < $cart_item_count)
$output .= '';
endforeach;
$fields['order']['order_comments']['default'] = $output;
endif;
return $fields;
}

How to Get Value from input fields in cart page to admin order details page?

In the cart page, I already created two text fields, but I'm unable to get the value that user input in that fields to admin order details page.
How can I get the values in order details page and I also want to save that details in the database.
Below is the code of my cart page
<?php
do_action( 'woocommerce_before_cart' ); ?>
<section class="checkout_display">
<div class="container">
<div class="row">
<form action="<?php echo esc_url( wc_get_checkout_url() );?>" method="post">
<?php do_action( 'woocommerce_before_cart_table' ); ?>
<div class="col-lg-6 col-md-8 col-sm-12 inset">
<div class="checkout_title">get started</div>
<div class="first_form">
<!--Code which display text field one -->
<div class="form-group" >
<label>Instagram username</label>
<input type="text" name="igusername" required>
</div>
<!--Code which display text field second -->
<div class="form-group">
<label>Email</label>
<input type="email" name="useremail" required>
</div>
<?php do_action( 'woocommerce_before_cart_contents' ); ?>
<?php
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
$product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
$product_permalink = apply_filters( 'woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink( $cart_item ) : '', $cart_item, $cart_item_key );
?>
<div class="form-group">
<label>Your package</label>
<select disabled>
<option> <?php echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', sprintf( '%s', esc_url( $product_permalink ), $_product->get_name() ), $cart_item, $cart_item_key ) );?> For
<span><?php echo apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key ); // PHPCS: XSS ok.
?>
</span>
</option>
</select>
</div>
<div class="checkbox"><input type="checkbox" /> Yes! send me special promotion and discounts</div>
<div class="btn">
<input type="submit" value="next">
</a>
</div>
<?php
}
}
?>
<?php do_action( 'woocommerce_cart_contents' ); ?>
<?php do_action( 'woocommerce_after_cart_contents' ); ?>
</div>
</div>
<?php do_action( 'woocommerce_after_cart_table' ); ?>
</form>
</div>
</div>
</section>
<?php do_action( 'woocommerce_after_cart' ); ?>
Code which display text field one and second are the text box from where i want to get value which user input and want to display and store them in admin order details page after completion of payment
Try the following that will display your posted fields values to Woocommerce session. When order will be placed, it will saved that custom session data as custom order meta data and display it in admin orders:
// Save the posted data to Woocommerce session
add_filter( 'init', 'set_instagram_posted_data_to_wc_sessions', 10, 3 );
function set_instagram_posted_data_to_wc_sessions() {
if ( ( is_cart() || is_checkout() ) && isset($_POST['igusername']) && isset($_POST['useremail']) ) {
// Enable Woocommerce sessions (if not done yet)
if ( isset(WC()->session) && ! WC()->session->has_session() ) {
WC()->session->set_customer_session_cookie( true );
}
$session_data = []; // initializing
if( isset($_POST['igusername']) && ! empty($_POST['igusername']) ) {
// Add the dropdown value as custom cart item data
$session_data['ig_username'] = sanitize_text_field($_POST['igusername']);
}
if( isset($_POST['useremail']) && ! empty($_POST['useremail']) ) {
// Add the dropdown value as custom cart item data
$session_data['ig_useremail'] = sanitize_email($_POST['useremail']);
}
// Set the data to custom wc_sessions
if( sizeof($session_data) > 0 ) {
WC()->session->set('ig_data', $session_data);
}
}
}
// Save the session data as custom order meta data (post meta data)
add_action( 'woocommerce_checkout_create_order', 'action_checkout_create_order_callback', 10, 2 );
function action_checkout_create_order_callback( $order, $data ) {
if( $session_data = WC()->session->get('ig_data') ) {
$order->update_meta_data( '_ig_username', wc_clean($session_data['ig_username']) );
$order->update_meta_data( '_ig_useremail', wc_clean($session_data['ig_useremail']) );
// remove the data from Woocommerce session
WC()->session->__unset('ig_data'):
}
}
// Display custom data in Admin orders, below the billing address
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_after_admin_order_billing_address', 10, 1 );
function display_after_admin_order_billing_address( $order ){
$ig_username = $order->get_meta('_ig_username');
$ig_useremail = $order->get_meta('_ig_useremail');
if( ! empty($ig_username) || ! empty($ig_useremail) ) :
echo '<div class="instagram-userdata">
<h3>'.__('Instagram user data').'</h3>
<table cellpadding="0" cellspacing="0" border="0" style="margin-top:6px;">
<tr><th align="left">'.__('Username').': </th><td> ' . $ig_username . '</td></tr>
<tr><th align="left">'.__('Email').': </th><td> ' . $ig_useremail . '</td></tr>
</table>
</div>';
endif;
}
Code goes in function.php file of your active child theme (or active theme). It should works now.
If it doesn't work with sessions you can use the following that will add the post data to hidden fields in checkout page and will post that data again when order is submitted… Everything else is the same as above…
So you can try alternatvelly:
// Display the posted data values in checkout hidden fields
add_filter( 'woocommerce_after_checkout_billing_form', 'set_instagram_posted_data_in_hidden_field', 10, 3 );
function set_instagram_posted_data_in_hidden_field() {
if ( isset($_REQUEST['igusername'])|| isset($_REQUEST['useremail']) ) {
// Display hidden fields with the Instagram posted values
?><input type="hidden" name="ig_username" value="<?php echo $_REQUEST['igusername']; ?>">
<input type="hidden" name="ig_useremail" value="<?php echo $_REQUEST['useremail']; ?>"><?php
}
}
// Save checkout hidden fields values as custom order meta data (post meta data)
add_action( 'woocommerce_checkout_create_order', 'action_checkout_create_order_callback', 10, 2 );
function action_checkout_create_order_callback( $order, $data ) {
if ( isset($_POST['ig_username']) {
$order->update_meta_data( '_ig_username', sanitize_text_field($_POST['ig_username']) );
}
if ( isset($_POST['ig_useremail']) {
$order->update_meta_data( '_ig_useremail', sanitize_email($session_data['ig_useremail']) );
}
}
// Display custom data in Admin orders, below the billing address
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_after_admin_order_billing_address', 10, 1 );
function display_after_admin_order_billing_address( $order ){
$ig_username = $order->get_meta('_ig_username');
$ig_useremail = $order->get_meta('_ig_useremail');
if( ! empty($ig_username) || ! empty($ig_useremail) ) :
echo '<div class="instagram-userdata">
<h3>'.__('Instagram user data').'</h3>
<table cellpadding="0" cellspacing="0" border="0" style="margin-top:6px;">
<tr><th align="left">'.__('Username').': </th><td> ' . $ig_username . '</td></tr>
<tr><th align="left">'.__('Email').': </th><td> ' . $ig_useremail . '</td></tr>
</table>
</div>';
endif;
}
Code goes in function.php file of your active child theme (or active theme). It should works now.
In Order pages in backend, you will get something like:
To retrieve the data from $order the WC_Order Object (or $order_id the Order Id) use:
$order = wc_get_order( $order_id ); // (optionally if required) with the Order ID
$ig_username = $order->get_meta('_ig_username');
$ig_useremail = $order->get_meta('_ig_useremail');
You can use order meta to save anything to a particular order.
add_action('woocommerce_checkout_create_order',
'before_checkout_create_order', 20, 2);
function before_checkout_create_order( $order, $data )
{
$order->update_meta_data( '_custom_text1', 'value' );
$order->update_meta_data( '_custom_tex2', 'value' );
}
Add your text box values to the value section of the meta key and they will store in the DB when the order is saved.
They will appear at the bottom of the order screen in admin section

Searchable multiple product select custom field for Woocommerce

I am developing a plugin where I need to display some custom select product. So far I can able to make the option field but how can i save them as option field with comma separated product ids like.
45,78,55,48,
here is an example of searchable multiple select option for WooCommerce product.
Here is my code
function crp_select_products() {
global $post, $woocommerce;
$product_ids = array();
?>
<div class="options_group">
<?php if ( $woocommerce->version >= '3.0' ) : ?>
<p class="form-field">
<label for="related_ids"><?php _e( 'Search Products', 'woocommerce' ); ?></label>
<select class="wc-product-search" multiple="multiple" style="width: 50%;" id="related_ids" name="related_ids[]" data-placeholder="<?php esc_attr_e( 'Search for a product…', 'woocommerce' ); ?>" data-action="woocommerce_json_search_products_and_variations">
<?php
foreach ( $product_ids as $product_id ) {
$product = wc_get_product( $product_id );
if ( is_object( $product ) ) {
echo '<option value="' . esc_attr( $product_id ) . '"' . selected( true, true, false ) . '>' . wp_kses_post( $product->get_formatted_name() ) . '</option>';
}
}
?>
</select> <?php echo wc_help_tip( __( 'Select products are for sale product.', 'woocommerce' ) ); ?>
</p>
<?php endif; ?>
</div>
<?php
}
First, there is something missing in your function, to display the saved data in it.
After, this special field need to be displayed inside a form that will have a submit button. So it depends where you are using your function.
Here below is an example displaying that custom field as a custom product setting, save the data and display the saved data in it:
function crp_get_product_related_ids() {
global $post, $woocommerce;
$product_ids = get_post_meta( $post->ID, '_related_ids', true );
if( empty($product_ids) )
$product_ids = array();
?>
<div class="options_group">
<?php if ( $woocommerce->version >= '3.0' ) : ?>
<p class="form-field">
<label for="related_ids"><?php _e( 'Search Products', 'woocommerce' ); ?></label>
<select class="wc-product-search" multiple="multiple" style="width: 50%;" id="related_ids" name="related_ids[]" data-placeholder="<?php esc_attr_e( 'Search for a product…', 'woocommerce' ); ?>" data-action="woocommerce_json_search_products_and_variations">
<?php
foreach ( $product_ids as $product_id ) {
$product = wc_get_product( $product_id );
if ( is_object( $product ) ) {
echo '<option value="' . esc_attr( $product_id ) . '"' . selected( true, true, false ) . '>' . wp_kses_post( $product->get_formatted_name() ) . '</option>';
}
}
?>
</select> <?php echo wc_help_tip( __( 'Select products are for sale product.', 'woocommerce' ) ); ?>
</p>
<?php endif; ?>
</div>
<?php
}
add_action( 'woocommerce_product_options_general_product_data', 'add_custom_fied_in_product_general_fields', 20 );
function add_custom_fied_in_product_general_fields() {
global $post, $woocommerce;
crp_get_product_related_ids();
}
add_action( 'woocommerce_process_product_meta', 'process_product_meta_custom_fied', 20, 1 );
function process_product_meta_custom_fied( $product_id ){
if( isset( $_POST['crosssell_ids'] ) ){
update_post_meta( $product_id, '_related_ids', array_map( 'intval', (array) wp_unslash( $_POST['related_ids'] ) ) );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.

Wordpress - using Ajax on Woocommerce add to cart button shortcodes

I have a pretty strange problem with a custom build I am doing in Wordpress. I am using hooks to overwrite a starter theme's 'add-to-cart' button on a custom page showing products. The weird thing is that when I loop through the add-to-cart button to add quantity options on my products, the original Ajax function disappears. I then implemented another function to add it back in (and cause my custom 'view cart' button's items-in-cart number to update) but although it works in the cart, it doesn't seem to be working for my custom shop page.
I am using this snippet in my header to handle the cart contents:
<?php if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
$count = WC()->cart->cart_contents_count;
?><a class="cart-contents" href="<?php echo WC()->cart->get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php
if ( $count > 0 ) {
?>
<span class="cart-contents-count"><?php echo esc_html( $count ); ?></span>
<?php
}
?></a>
And here are my two functions in my child-theme functions.php:
/**
* Ensure cart contents update when products are added to the cart via AJAX
*/
function my_header_add_to_cart_fragment( $fragments ) {
ob_start();
$count = WC()->cart->cart_contents_count;
?><a class="cart-contents" href="<?php echo WC()->cart->get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php
if ( $count > 0 ) {
?>
<span class="cart-contents-count"><?php echo esc_html( $count ); ?></span>
<?php
}
?></a><?php
$fragments['a.cart-contents'] = ob_get_clean();
return $fragments;
}
add_filter( 'woocommerce_add_to_cart_fragments', 'my_header_add_to_cart_fragment' );
/**
* Add quantity to products in Products Page
*/
add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_woocommerce_loop_add_to_cart_link', 10, 2 );
function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {
if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() ) {
$html = '<form action="' . esc_url( $product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
$html .= woocommerce_quantity_input( array(), $product, false );
$html .= '<button type="submit" class="button alt">' . esc_html( $product->add_to_cart_text() ) . '</button>';
$html .= '</form>';
}
return $html;
}
I think that my second function adding a new add-to-cart button is overwriting the initial Ajax functionality, but everything I try to do to add this functionality back in is not working. I'm not the best at JS/jQuery so it's probably that I'm not implementing my code properly.
Any help with this would be much appreciated.
Need to add another classes to the button i.e add_to_cart_button & ajax_add_to_cart.
Hope this will do for you.

How do I change the woocommerce coupon code?

I want the coupon to appear on my cart total and on my coupon section. Right now coupon code appear on my cart total and on my cart coupon section. But I want to change it a little bit.
For default, it shows like "coupon: couponcode and then $10 with the remove link."
I want it to print the cart % discount "15% off " for cart total and my coupon section. For my cart total I don't want the "remove link" to appear. I attach an image, I want to look something like that.
For example, I want it look like this.
Cart total - 15% off and then $10.00.
Cart coupon - couponcode 15% off and then "remove" link, the remove link is you can remove the couponcode.
Wc-cart-functions.php.
I know this make the coupon work. How do I print the discount type. If I remove the "remove" link, it will remove for carts total and my coupon section. But I just want it to remove in my cart total and I want the "Remove" link appear on my coupon section. How do that?
/**
* Get a coupon label
*
* #access public
* #param string $coupon
*/
function wc_cart_totals_coupon_label( $coupon ) {
if ( is_string( $coupon ) )
$coupon = new WC_Coupon( $coupon );
echo apply_filters( 'woocommerce_cart_totals_coupon_label', esc_html( __( 'Coupon:', 'woocommerce' ) . ' ' . $coupon->code ), $coupon );
}
/**
* Get a coupon value
*
* #access public
* #param string $coupon
*/
function wc_cart_totals_coupon_html( $coupon ) {
if ( is_string( $coupon ) ) {
$coupon = new WC_Coupon( $coupon );
}
$value = array();
if ( $amount = WC()->cart->get_coupon_discount_amount( $coupon->code, WC()->cart->display_cart_ex_tax ) ) {
$discount_html = wc_price( $amount );
} else {
$discount_html = '';
}
$value[] = apply_filters( 'woocommerce_coupon_discount_amount_html', $discount_html, $coupon );
if ( $coupon->enable_free_shipping() ) {
$value[] = __( 'Free shipping coupon', 'woocommerce' );
}
// get rid of empty array elements
$value = array_filter( $value );
$value = implode( ', ', $value ) . ' ' . __( '[Remove]', 'woocommerce' ) . '';
echo apply_filters( 'woocommerce_cart_totals_coupon_html', $value, $coupon );
}
Cart Coupon (cart-totals.php)
<ul class="cart_totals <?php if ( WC()->customer->has_calculated_shipping() ) echo 'calculated_shipping'; ?>">
<?php do_action( 'woocommerce_before_cart_totals' ); ?>
<li>
<span class="label"><?php _e( 'Subtotal', 'woocommerce' ); ?></span>
<span class="value"><?php wc_cart_totals_subtotal_html(); ?></span>
</li>
<?php foreach ( WC()->cart->get_coupons() as $code => $coupon ) : ?>
<li class="cart-discount coupon-<?php echo esc_attr( sanitize_title( $code ) ); ?>">
<span class="label"><?php wc_cart_totals_coupon_label( $coupon ); ?></span>
<span class="value"><?php wc_cart_totals_coupon_html( $coupon ); ?></span>
</li>
<?php endforeach; ?>
<?php do_action( 'woocommerce_after_cart_totals' ); ?>
</ul>
Coupon section (cart.php)
<section class="row-wrap prod-wrap">
<div class="row-inner">
<form action="<?php echo esc_url( WC()->cart->get_cart_url() ); ?>" method="post">
<?php do_action( 'woocommerce_before_cart_table' ); ?>
<div id="coupon">
<?php foreach ( WC()->cart->get_coupons() as $code => $coupon ) : ?>
<li class="cart-discount coupon-<?php echo esc_attr( sanitize_title( $code ) ); ?>">
<span class="label"><?php wc_cart_totals_coupon_label( $coupon ); ?></span>
<span class="value"><?php wc_cart_totals_coupon_html( $coupon ); ?></span>
</li>
<?php endforeach; ?>
</div>
<?php do_action( 'woocommerce_after_cart_table' ); ?>
</form>
</div>
</section>

Categories