woocommerce code for cart button - php

I need to add the woocommerce cart button to one of my pages and was wondering if someone could help with the code required to call the cart button. Here is the current code:
<?php woocommerce_product_loop_start(); ?>
<?php woocommerce_product_subcategories(); ?>
<?php while ( have_posts() ) : the_post(); ?>
<div id="product-image1">
<a href="<?php echo esc_url( get_permalink( $product->id ) ); ?>" title="<?php echo esc_attr( $product->get_title() ); ?>">
<?php echo $product->get_image(); ?>
</a>
</div>
<div id="product-description-container">
<ul>
<a href="<?php echo esc_url( get_permalink( $product->id ) ); ?>" title="<?php echo esc_attr( $product->get_title() ); ?>">
<li><h4><?php echo $product->get_title(); ?></h4></li></a>
<li><?php echo apply_filters( 'woocommerce_short_description', $post->post_excerpt )?></li>
<li><h6><?php echo $product->get_price_html(); ?> **MISSING CODE TO ADD TO CART BUTTON HERE**</h6></li>
</ul>
</div>
<?php endwhile; // end of the loop. ?>

I think may be you need following code.
Add this code to your place = (**MISSING CODE TO ADD TO CART BUTTON HERE**).
global $product;
echo apply_filters( 'woocommerce_loop_add_to_cart_link',
sprintf( '%s',
esc_url( $product->add_to_cart_url() ),
esc_attr( $product->get_id() ),
esc_attr( $product->get_sku() ),
$product->is_purchasable() ? 'add_to_cart_button' : '',
esc_attr( $product->get_type() ),
esc_html( $product->add_to_cart_text() )
),
$product );
Hope this will help.

Related

Hide counter on cart Wordpress

How to hide counter when cart is empty? Wordpress - woocommerce - supro theme
<a href="<?php echo esc_url( wc_get_cart_url() ) ?>" class="cart-contents" id="icon-cart-contents">
<?php echo wp_kses_post( $icon_cart ); ?>
<span class="label-item cart-label"><?php echo wp_kses( $cart_html, wp_kses_allowed_html( 'post' ) ); ?></span>
<span class="mini-cart-counter"><?php echo intval( $woocommerce->cart->cart_contents_count ); ?></span>
</a>
Tnx
Updated: Add this code at your child theme’s functions.php
add_action( 'wp_head', 'x_hide_cart' );
function x_hide_cart(){
if ( WC()->cart->get_cart_contents_count() == 0 ) {
?>
<style type="text/css">.x-menu-item-woocommerce{display: none;}</style>
<?php
}
}

How to change woocommerce mini cart total (based on product name instead of quantity)?

as per title mention, right now the minicart is showing total based on the total quantity of the products, but not product name (ID). I assume it should be changed under "mini-cart.php" (correct me if I'm wrong). Can anyone tell me how and where to change this code? Thanks
refer here: https://shop.cheesang.com/wp-content/uploads/2019/09/sample.jpg
<?php
/**
* Mini-cart
*
* Contains the markup for the mini-cart, used by the cart widget.
*
* This template can be overridden by copying it to yourtheme/woocommerce/cart/mini-cart.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you (the theme developer).
* will need to copy the new files to your theme to maintain compatibility. We try to do this.
* as little as possible, but it does happen. When this occurs the version of the template file will.
* be bumped and the readme will list any important changes.
*
* #see http://docs.woothemes.com/document/template-structure/
* #author WooThemes
* #package WooCommerce/Templates
* #version 2.5.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
?>
<?php do_action( 'woocommerce_before_mini_cart' ); ?>
<ul class="cart_list products-list product_list_widget <?php echo $args['list_class']; ?>">
<?php if ( ! WC()->cart->is_empty() ) : ?>
<?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_widget_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
$product_name = apply_filters( 'woocommerce_cart_item_name', $_product->get_title(), $cart_item, $cart_item_key );
$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );
$product_price = apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key );
?>
<li>
<?php if ( ! $_product->is_visible() ) : ?>
<?php echo str_replace( array( 'http:', 'https:' ), '', $thumbnail ) . $product_name . ' '; ?>
<?php else : ?>
<a class="title" href="<?php echo esc_url( $_product->get_permalink( $cart_item ) ); ?>">
<?php echo str_replace( array( 'http:', 'https:' ), '', $thumbnail ) . $product_name . ' '; ?>
</a>
<?php endif; ?>
<?php echo WC()->cart->get_item_data( $cart_item ); ?>
<?php echo apply_filters( 'woocommerce_widget_cart_item_quantity', '<span class="quantity">' . sprintf( '%s × %s', $cart_item['quantity'], $product_price ) . '</span>', $cart_item, $cart_item_key ); ?>
<?php echo apply_filters( 'woocommerce_cart_item_remove_link', sprintf( '<i class="fa fa-times-circle"></i>', esc_url( WC()->cart->get_remove_url( $cart_item_key ) ), __( 'Remove this item', 'snssimen' ) ), $cart_item_key ); ?>
</li>
<?php
}
}
?>
<?php else : ?>
<li class="empty"><?php _e( 'No products in the cart.', 'snssimen' ); ?></li>
<?php endif; ?>
</ul><!-- end product list -->
<?php if ( sizeof( WC()->cart->get_cart() ) > 0 ) : ?>
<p class="total"><?php _e( 'Total', 'snssimen' ); ?>: <strong><?php echo WC()->cart->get_cart_subtotal(); ?></strong></p>
<?php do_action( 'woocommerce_widget_shopping_cart_before_buttons' ); ?>
<p class="buttons">
<?php _e( 'Go to Cart', 'snssimen' ); ?>
<?php _e( 'Checkout', 'snssimen' ); ?>
</p>
<span style="display:none" class="sns-cart-number">
<?php echo sizeof( WC()->cart->get_cart() ) ?>
<?php //echo $woocommerce->cart->cart_contents_count; ?>
</span>
<?php endif; ?>
<?php do_action( 'woocommerce_after_mini_cart' ); ?>
add this code in functions.php file
//This function will display total qty ni mini-cart
function filter__woocommerce_add_to_cart_fragments($fragments) {
ob_start(); ?>
<div class="number bold">
<?php echo sprintf( '%d', WC()->cart->cart_contents_count ); ?>
</div>
<?php
$fragments['#minicart .number'] = ob_get_clean();
return $fragments;
}
add_filter( 'woocommerce_add_to_cart_fragments', 'filter__woocommerce_add_to_cart_fragments' );
And to show mini-cart in header add this code in header.php file
<div class="mini-cart">
<a id="minicart" href="<?php echo WC()->cart->get_cart_url(); ?>" class="cart icon red relative">
<div class="number boldd">
<?php echo sprintf('%d', WC()->cart->cart_contents_count); ?>
</div>
<div id="cartcontents">
<div class="widget_shopping_cart_content">
<?php woocommerce_mini_cart(); ?>
</div>
</div>
</a>
</div>

Trouble adding coupons and dynamic updating of totals to WooCommerce Mini-Cart

UPDATE: This gets me closer, but the applying a coupon takes me to the cart page instead of updating within the mini-cart.
<form class="woocommerce-cart-form" action="<?php echo esc_url( wc_get_cart_url() ); ?>" method="post">
<table class="shop_table shop_table_responsive cart woocommerce-cart-form__contents" cellspacing="0">
<tbody>
<tr>
<td colspan="6" class="actions">
<?php if ( wc_coupons_enabled() ) { ?>
<div class="coupon">
<label for="coupon_code"><?php esc_html_e( 'Coupon:', 'woocommerce' ); ?></label> <input type="text" name="coupon_code" class="input-text" id="coupon_code" value="" placeholder="<?php esc_attr_e( 'Coupon code', 'woocommerce' ); ?>" /> <input type="submit" class="button" name="apply_coupon" value="<?php esc_attr_e( 'Apply coupon', 'woocommerce' ); ?>" />
<?php do_action( 'woocommerce_cart_coupon' ); ?>
</div>
<?php } ?>
</td>
</tr>
</tbody>
</table>
</form>
<div class="cart-collaterals">
<?php do_action( 'woocommerce_cart_collaterals' ); ?>
</div>
I'm trying to add the coupon functionality to the mini-cart.php template and running into issues. If I copy the core coupon functionality from cart.php, nothing happens when I apply a coupon. The expected behavior is for the coupon to apply and the total updated (all within the mini-cart). Here's my mini-cart.php file:
<?php
/**
* Mini-cart
*
* Contains the markup for the mini-cart, used by the cart widget.
*
* This template can be overridden by copying it to yourtheme/woocommerce/cart/mini-cart.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* #see https://docs.woocommerce.com/document/template-structure/
* #author WooThemes
* #package WooCommerce/Templates
* #version 3.3.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
do_action( 'woocommerce_before_mini_cart' ); ?>
<?php if ( ! WC()->cart->is_empty() ) : ?>
<div class="mini-cart-header">
<div>Your Cart</div>
<i class="icon-close mini-cart-close"></i>
</div>
<ul class="woocommerce-mini-cart cart_list product_list_widget <?php echo esc_attr( $args['list_class'] ); ?>">
<?php
do_action( 'woocommerce_before_mini_cart_contents' );
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_widget_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
$product_name = apply_filters( 'woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key );
$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );
$product_price = apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $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 );
?>
<li class="woocommerce-mini-cart-item <?php echo esc_attr( apply_filters( 'woocommerce_mini_cart_item_class', 'mini_cart_item', $cart_item, $cart_item_key ) ); ?>">
<?php if ( ! $_product->is_visible() ) : ?>
<?php echo str_replace( array( 'http:', 'https:' ), '', $thumbnail ) . '<div class="product-name">' . $product_name . '</div>' . '<div class="product-price"' . $product_price . '</div>' ?>
<?php else : ?>
<a href="<?php echo esc_url( $product_permalink ); ?>" class="product-info">
<?php echo str_replace( array( 'http:', 'https:' ), '', $thumbnail ) . '<div><div class="product-name">' . $product_name . '</div>' . '<div class="product-price"' . $product_price . '</div></div>' ?>
</a>
<?php endif; ?>
<?php echo wc_get_formatted_cart_item_data( $cart_item ); ?>
<?php
echo apply_filters( 'woocommerce_cart_item_remove_link', sprintf(
'<i class="icon-trash"></i>',
esc_url( wc_get_cart_remove_url( $cart_item_key ) ),
__( 'Remove this item', 'woocommerce' ),
esc_attr( $product_id ),
esc_attr( $cart_item_key ),
esc_attr( $_product->get_sku() )
), $cart_item_key );
?>
</li>
<?php
}
}
do_action( 'woocommerce_mini_cart_contents' );
?>
</ul>
<p class="woocommerce-mini-cart__total total"><strong><?php _e( 'Subtotal', 'woocommerce' ); ?>:</strong> <?php echo WC()->cart->get_cart_subtotal(); ?></p>
<?php if ( wc_coupons_enabled() ) { ?>
<div class="coupon">
<label for="coupon_code"><?php esc_html_e( 'Coupon:', 'woocommerce' ); ?></label> <input type="text" name="coupon_code" class="input-text" id="coupon_code" value="" placeholder="<?php esc_attr_e( 'Coupon code', 'woocommerce' ); ?>" /> <input type="submit" class="button" name="apply_coupon" value="<?php esc_attr_e( 'Apply coupon', 'woocommerce' ); ?>" />
<?php do_action( 'woocommerce_cart_coupon' ); ?>
</div>
<?php } ?>
<?php foreach ( WC()->cart->get_coupons() as $code => $coupon ) : ?>
<tr class="cart-discount coupon-<?php echo esc_attr( sanitize_title( $code ) ); ?>">
<th><?php wc_cart_totals_coupon_label( $coupon ); ?></th>
<td data-title="<?php echo esc_attr( wc_cart_totals_coupon_label( $coupon, false ) ); ?>"><?php wc_cart_totals_coupon_html( $coupon ); ?></td>
</tr>
<?php endforeach; ?>
<p class="woocommerce-mini-cart__total total"><strong><?php esc_attr_e( 'Total', 'woocommerce' ); ?>"><?php wc_cart_totals_order_total_html(); ?></p>
<?php do_action( 'woocommerce_widget_shopping_cart_before_buttons' ); ?>
<p class="woocommerce-mini-cart__buttons buttons"><?php do_action( 'woocommerce_widget_shopping_cart_buttons' ); ?></p>
<div>Related Products/Upsell</div>
<?php else : ?>
<p class="woocommerce-mini-cart__empty-message"><?php _e( 'No products in the cart.', 'woocommerce' ); ?></p>
<?php endif; ?>
<?php do_action( 'woocommerce_after_mini_cart' ); ?>

Creating custom credits using add_filter

As far as I understand I can use add_filter('hook', 'my_custom_credits'); to replace default credits with a custom one. I know how to create my_custom_credits function and it's working.
But I'm not sure how to find hook in someone's code. Do I need to create it or I just use some id?
<?php
/**
* The template for displaying the footer credits
*
*/
?>
<div id="footer__credits" class="footer__credits" <?php czr_fn_echo('element_attributes') ?>>
<p class="czr-copyright">
<span class="czr-copyright-text">© <?php echo esc_attr( date('Y') ) ?> </span><a class="czr-copyright-link" href="<?php echo esc_url( home_url() ) ?>" title="<?php echo esc_attr( get_bloginfo() ) ?>"><?php echo esc_attr( get_bloginfo() ) ?></a><span class="czr-rights-text"> – <?php _e( 'All rights reserved', 'customizr') ?></span>
</p>
<p class="czr-credits">
<span class="czr-designer">
<span class="czr-wp-powered"><span class="czr-wp-powered-text"><?php _e( 'Powered by', 'customizr') ?> </span><a class="czr-wp-powered-link fab fa-wordpress" title="<?php _e( 'Powered by WordPress', 'customizr' ) ?>" href="<?php echo esc_url( __( 'https://wordpress.org/', 'customizr' ) ); ?>" target="_blank"></a></span><span class="czr-designer-text"> – <?php printf( __('Designed with the %s', 'customizr'), sprintf( '<a class="czr-designer-link" href="%1$s" title="%2$s">%2$s</a>', esc_url( CZR_WEBSITE . 'customizr' ), __('Customizr theme', 'customizr') ) ); ?></span>
</span>
</p>
</div>

Woocommerce - Show Page's Total as Quantity is Adjusted

After moving to Woocommerce, we've built a page that showing several categories and all the products of each of these categories. The idea is to allow a customer to build their own product.
We've got the categories and products showing with their name, image, a price, add to cart and quantity field aside each product.
We'd like to add the flexibility for the page to display a total calculation of all their selections and provide an 'Add All Selections to Cart' type of button, is this possible?
I'll outline what we've done so far in case more details are needed. To get the 'add to cart' to appear next to each product we used a modified version of the following 'override loop template...' as follows:
<?php
/**
* Loop Add to Cart
*
* #author WooThemes
* #package WooCommerce/Templates
* #version 2.2.0
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $product;
$mode = get_option('catalog_mode');
?>
<?php if ( ! $product->is_in_stock() || $mode == 'on' ) : ?>
<?php echo apply_filters( 'out_of_stock_add_to_cart_text', __( 'Read More', 'woocommerce' ) ); ?>
<?php else : ?>
<?php
$link = array(
'url' => '',
'label' => '',
'class' => ''
);
$handler = apply_filters( 'woocommerce_add_to_cart_handler', $product->product_type, $product );
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;
}
//EDIT for Add to Cart
//printf('%s', $link, $product->id, $product->product_type, $label);
if ( $product->product_type == 'simple' ) {
?>
<form action="<?php echo esc_url( $product->add_to_cart_url() ); ?>" class="cart" method="post" enctype='multipart/form-data'>
<?php woocommerce_quantity_input(); ?>
<button type="submit" class="button alt"><?php echo $label; ?></button>
</form>
<?php
} else {
printf('%s', $link, $product->id, $product->product_type, $label);
}
//END Add to Cart
echo apply_filters( 'woocommerce_loop_add_to_cart_link', sprintf('%s', 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 );
?>
Finally, we added the categories to the page and showed their products by adding a few of the following to a page template:
<ul class="products">
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'product_cat' => 'INSERT CATEGORY HERE', 'orderby' => 'rand' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<li class="product">
<a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
<?php woocommerce_show_product_sale_flash( $post, $product ); ?>
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />'; ?>
<h3><?php the_title(); ?></h3>
<span class="price"><?php echo $product->get_price_html(); ?></span>
</a>
<?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
</li>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</ul><!--/.products-->
<!--End Display Products-->
This could apply to anyone building out separate products for a brand. We're not into building cars, but say we were, the desired workflow is relatively the same for any series of products - if cars were the case, what we'd be trying to complete is a functional place a build your own car for each model on their own pages.
To break down each model, we've categorized parts of the car, that might interest that car consumer, into categories; so we might have a category that is composed of wheels and tires for X car and another category for wheels and tires for Y car.
Let's say we're writing the page build car x. We would like them to be able to select all the quantities of all applicable products that compose 'car x'. To do so, we have displayed all the categories that apply to car x, and all the products in said categories. We have a quantity field and add to cart next to the quantity.
We would like to get that to total and allow for an add all selections to cart button at the bottom of the page.

Categories