i have a cart icon on the header.When the page is scrolled this cart icon is floating in a fixed position.when hover over cart icon the cart contents like name price subtotal all are displaying.now when adding a product the cart count is updating correctly.nut cart contents are updating only after refresh.
code of cart in header.php
<li>
<a href="<?php echo get_template_directory_uri(); ?>/cart"
><i class="uil uil-shopping-cart-alt"></i>
<div>Cart</div></a
>
<span class="counter" id="cart-count"><?php
$cart_count = WC()->cart->get_cart_contents_count();
//echo sprintf ( _n( '%d', '%d', $cart_count ), $cart_count );
echo $cart_count;
?></span>
</li>
updating cart count code in functions.php
add_filter( 'woocommerce_add_to_cart_fragments', 'refresh_cart_count', 50, 1 );
function refresh_cart_count( $fragments ){
ob_start();
?>
<span class="counter" id="cart-count"><?php
$cart_count = WC()->cart->get_cart_contents_count();
//echo sprintf ( _n( '%d', '%d', $cart_count ), $cart_count );
echo $cart_count;
?></span>
<?php
$fragments['#cart-count'] = ob_get_clean();
return $fragments;
}
this is the code displaying when hovering over the cart
<div class="miniBasketContent"><div class="miniBasketHeader">Your basket</div><div class="items"><?php
global $woocommerce;
$items = $woocommerce->cart->get_cart();
$total = $woocommerce->cart->total;
foreach($items as $item => $values) { $_product = wc_get_product( $values['data']->get_id() );$getProductDetail = wc_get_product( $values['product_id'] );$price = get_post_meta($values['product_id'] , '_price', true);?><div class="item"><div class="image"><?php echo $getProductDetail->get_image();?></div><div class="info"><?php echo $_product->get_title();?></div><div class="price"><?php echo $price;?> AED</div></div><?php } ?></div><div class="miniBasketFooter"><div class="info"><div class="total"><span><?php echo $total; ?>AED</span>Total</div>Show basketGo to checkout</div></div></div>
what to do to update cart contents inside popup without reloading page.
You can try the below solution
demo_cart_link() Use this function where you want to display cart count HTML.
function.php
if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '2.3', '>=' ) ) {
add_filter( 'woocommerce_add_to_cart_fragments', 'demo_cart_link_fragment', 10 , 1 );
} else {
add_filter( 'add_to_cart_fragments', 'demo_cart_link_fragment', 10 , 1 );
}
if ( ! function_exists( 'demo_cart_link_fragment' ) ) {
function demo_cart_link_fragment( $fragments ) {
global $woocommerce;
ob_start();
demo_cart_link();
$fragments['a.cart-contents'] = ob_get_clean();
return $fragments;
}
}
if ( ! function_exists( 'demo_cart_link' ) ) {
function demo_cart_link() {
?>
<a class="cart-contents" href="<?php echo esc_url( wc_get_cart_url() ); ?>" title="<?php esc_attr_e( 'View your shopping cart', 'paperfig' ); ?>" type="button" data-toggle="modal">
<i class="fas fa-shopping-basket"></i>
<span class="cart-numbers"><?php echo wp_kses_data( sprintf( _n( ' %d', ' %d', WC()->cart->get_cart_contents_count(), 'paperfig' ), WC()->cart->get_cart_contents_count() ) );?></span>
</a>
<?php
}
}
Related
I am trying to create a custom upsell n woocommerce field. This code allowed me to create the custom field in the backend. I can't get the products to show via the echo
It returns me the word Array.
What am I doing wrong?
The value should be displayed in a custom jquery sidebar
// Display a custom select field in "Linked Products" section
add_action( 'woocommerce_product_options_related',
'display_linked_products_data_custom_field' );
function display_linked_products_data_custom_field() {
global $product_object, $post;
?>
<p class="form-field">
<label for="total_look"><?php _e( 'Total Look EleNelen', 'woocommerce' ); ?>.
</label>
<select class="wc-product-search" multiple="multiple" style="width: 50%;"
id="total_look_ids" name="_total_look_ids[]" data-placeholder="<?php esc_attr_e(
'Search for a product…', 'woocommerce' ); ?>" data-
action="woocommerce_json_search_products_and_variations" data-exclude="<?php echo
intval( $post->ID ); ?>">
<?php
$product_ids = $product_object->get_meta( '_total_look_ids' );
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>
</p>
<?php
}
// Save the values to the product
add_action( 'woocommerce_admin_process_product_object',
'save_linked_products_data_custom_field_value', 10, 1 );
function save_linked_products_data_custom_field_value( $product ){
$data = isset( $_POST['_total_look_ids'] ) ? array_map( 'intval', (array)
$_POST['_total_look_ids'] ) : array();
$product->update_meta_data( '_total_look_ids', $data );
}
How to remove the items word in cart woocommerce?
See screenshot: WooCommerce Mini Cart header.php
in fuctions.php
Cart Fragments:
if ( ! function_exists( 'agricola_woocommerce_cart_link_fragment' ) ) {
function agricola_woocommerce_cart_link_fragment( $fragments ) {
ob_start();
agricola_woocommerce_cart_link();
$fragments['a.cart-contents'] = ob_get_clean();
return $fragments;
}
}
add_filter( 'woocommerce_add_to_cart_fragments',
'agricola_woocommerce_cart_link_fragment' );
Cart Link
if ( ! function_exists( 'agricola_woocommerce_cart_link' ) ) {
function agricola_woocommerce_cart_link() {
?>
<a class="cart-contents" href="<?php echo esc_url( wc_get_cart_url() ); ?>" title="<?php esc_attr_e( 'View your shopping cart', 'agricola' ); ?>">
<?php
$item_count_text = sprintf(
/* translators: number of items in the mini cart. */
_n( '%d item', '%d items', WC()->cart->get_cart_contents_count(), 'agricola' ),
WC()->cart->get_cart_contents_count()
);
?>
<span class="amount"><?php echo wp_kses_data( WC()->cart->get_cart_subtotal() ); ?></span> <span class="count"><?php echo esc_html( $item_count_text ); ?></span>
</a>
<?php
}
}
Display Header Cart.
if ( ! function_exists( 'agricola_woocommerce_header_cart' ) ) {
function agricola_woocommerce_header_cart() {
if ( is_cart() ) {
$class = 'current-menu-item';
} else {
$class = '';
}
?>
<ul id="site-header-cart" class="site-header-cart">
<li class="<?php echo esc_attr( $class ); ?>">
<?php agricola_woocommerce_cart_link(); ?>
</li>
<li>
<?php
$instance = array(
'title' => '',
);
the_widget( 'WC_Widget_Cart', $instance );
?>
</li>
</ul>
<?php
}
}
I already remove <?php $item_count_text = sprintf( /* translators: number of items in the mini cart. */ _n( '%d item', '%d items', WC()->cart->get_cart_contents_count(), 'agricola' ), WC()->cart->get_cart_contents_count()); ?> but keep come back with title like '9 items' I want to hide it.
I only want basket icon with price cost.
You can use CSS to hide that particular part. If you can provide the website link, we can help you with the CSS.
OR you can use String Locator plugin to locate the code where it is written inside the files and then you can comment it or remove it from the file itself.
Thanks
Just remove item and items beside %d. like below
if ( ! function_exists( 'agricola_woocommerce_cart_link' ) ) {
function agricola_woocommerce_cart_link() {
?>
<a class="cart-contents" href="<?php echo esc_url( wc_get_cart_url() ); ?>" title="<?php esc_attr_e( 'View your shopping cart', 'agricola' ); ?>">
<?php
$item_count_text = sprintf(
/* translators: number of items in the mini cart. */
_n( '%d', '%d', WC()->cart->get_cart_contents_count(), 'agricola' ),
WC()->cart->get_cart_contents_count()
);
?>
<span class="amount"><?php echo wp_kses_data( WC()->cart->get_cart_subtotal() ); ?></span> <span class="count"><?php echo esc_html( $item_count_text ); ?></span>
</a>
<?php
}
}
Note: after remove or changing anything with WooCommerce min cart , you are not able to see those change by refreshing a page, you have to update the cart item to check the changed preview.
I'm using Dokan Multivendor Plugin by weDevs and have an issue with some custom code..
Here is the code which helps you to display Vendor Name On The Product Thumbnail In Dokan:
//woocommerce
/*
Show Seller name on the product thumbnail
For WooCommerce
*/
add_action( 'woocommerce_after_shop_loop_item_title','sold_by' );
function sold_by(){
?>
</a>
<?php
global $product;
$author = get_user_by( 'id', $product->post->post_author );
$store_info = dokan_get_store_info( $author->ID );
if ( !empty( $store_info['store_name'] ) ) { ?>
<span class="details">
<?php printf( 'Sold by: %s', dokan_get_store_url( $author->ID ), $author->display_name ); ?>
</span>
<?php
}
}
But I need to display Store Name instead of Vendor Name On The Product Thumbnail..
Trying to change this string to achieve this goal:
<?php printf( 'Sold by: %s', dokan_get_store_url( $author->ID ), $author->display_name ); ?>
To this one:
<?php printf( 'Sold by: %s', dokan_get_store_url( $author->ID ), $author->store_name ); ?>
but it doesn't work..
Try this , I am assuming you are using WooCommerce V3.0+
add_action( 'woocommerce_after_shop_loop_item_title','sold_by' );
function sold_by(){
?>
</a>
<?php
global $product;
$seller = get_post_field( 'post_author', $product->get_id());
$author = get_user_by( 'id', $seller );
$store_info = dokan_get_store_info( $author->ID );
if ( !empty( $store_info['store_name'] ) ) { ?>
<span class="details">
<?php printf( 'Sold by: %s', dokan_get_store_url( $author->ID ), $author->display_name ); ?>
</span>
<?php
}
}
I am green in wordpress.
I have create a widget call woocommerce-dropdown-cart.php and placed in the same folder with the theme I am using. But why I cant find this widget in my Appereance-->widget?
On the other hands, my menu bar have not any widget, how can I add this widget into or just before the menu bar?
Many thanks!
Here is my php code:
<?php class Woocommerce_Dropdown_Cart extends WP_Widget {
public function __construct() {
parent::__construct(
'woocommerce-dropdown-cart', // Base ID
'Woocommerce Dropdown Cart', // Name
array( 'description' => __( 'Woocommerce Dropdown Cart', 'jeffho' ), ) // Args
);
}
public function widget( $args, $instance ) {
global $post;
extract( $args );
echo $before_widget;
global $woocommerce;
global $qode_options_proya;
$cart_holder_class = 'header_cart';
if (isset($qode_options_proya['woo_cart_type'])){
$cart_type = $qode_options_proya['woo_cart_type'];
switch ($cart_type) {
case 'font-elegant':
$cart_holder_class = "header_cart cart_icon";
break;
default:
$cart_holder_class = "header_cart";
break;
}
}
?>
<div class="shopping_cart_outer">
<div class="shopping_cart_inner">
<div class="shopping_cart_header">
<a class="<?php echo esc_attr($cart_holder_class);?>" href="<?php echo $woocommerce->cart->get_cart_url(); ?>"><span class="header_cart_span"><?php echo $woocommerce->cart->cart_contents_count; ?></span></a>
<div class="shopping_cart_dropdown">
<div class="shopping_cart_dropdown_inner">
<?php
$cart_is_empty = sizeof( $woocommerce->cart->get_cart() ) <= 0;
$list_class = array( 'cart_list', 'product_list_widget' );
?>
<ul class="<?php echo implode(' ', $list_class); ?>">
<?php if ( !$cart_is_empty ) : ?>
<?php foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $cart_item ) :
$_product = $cart_item['data'];
// Only display if allowed
if ( ! $_product->exists() || $cart_item['quantity'] == 0 ) {
continue;
}
// Get price
$product_price = get_option( 'woocommerce_tax_display_cart' ) == 'excl' ? $_product->get_price_excluding_tax() : $_product->get_price_including_tax();
$product_price = apply_filters( 'woocommerce_cart_item_price_html', woocommerce_price( $product_price ), $cart_item, $cart_item_key );
?>
<li>
<a itemprop="url" href="<?php echo get_permalink( $cart_item['product_id'] ); ?>">
<?php echo $_product->get_image(); ?>
<?php echo apply_filters('woocommerce_widget_cart_product_title', $_product->get_title(), $_product ); ?>
</a>
<?php echo $woocommerce->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 ); ?>
</li>
<?php endforeach; ?>
<?php else : ?>
<li><?php _e( 'No products in the cart.', 'woocommerce' ); ?></li>
<?php endif; ?>
</ul>
</div>
<?php if ( sizeof( $woocommerce->cart->get_cart() ) <= 0 ) : ?>
<?php endif; ?>
<a itemprop="url" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" class="qbutton white view-cart"><?php _e( 'Cart', 'woocommerce' ); ?> <i class="fa fa-shopping-cart"></i></a>
<span class="total"><?php _e( 'Total', 'woocommerce' ); ?>:<span><?php echo $woocommerce->cart->get_cart_subtotal(); ?></span></span>
<?php if ( sizeof( $woocommerce->cart->get_cart() ) <= 0 ) : ?>
<?php endif; ?>
</div>
</div>
</div>
</div>
<?php
echo $after_widget;
}
public function update( $new_instance, $old_instance ) {
$instance = array();
return $instance;
}
}
add_action( 'widgets_init', create_function( '', 'register_widget( "Woocommerce_Dropdown_Cart" );' ) );
?>
<?php
add_filter('add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment');
function woocommerce_header_add_to_cart_fragment( $fragments ) {
global $woocommerce;
ob_start();
?>
<span class="header_cart_span"><?php echo $woocommerce->cart->cart_contents_count; ?></span>
<?php
$fragments['span.header_cart_span'] = ob_get_clean();
return $fragments;
}
?>
I am using theme called "Creativo 5.0" and I have added:
register_sidebar( array(
'name' => __( 'Header Sidebar', 'Creativo' ),
'id' => 'header-sidebar',
'before_widget' => '<div class="header _widget_content">',
'after_widget' => '</div>',
'before_title' => '<h2 class="header-widget-title">',
'after_title' => '</h2>',
) );
into function creativo_widgets_init()
what I want is to add the php widget into the menu bar.
Basicly, I would like to display the coupon used on Woocommerce thankyou.php. Here is the code I have added
$coupons = $order->get_items( 'coupon' );
foreach ( $coupons as $item_id => $item ) {
echo "<span class='coupon-name'><b>".$item['name']."</b></span>";
$post = get_post( $item_id );
echo "<p class='coupon-description'>".$post->post_excerpt."</p>";
}
However, only the coupon code is shown while the description does not.
Here is where I placed my code:
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( $order ) : ?>
<?php if ( $order->has_status( 'failed' ) ) : ?>
<p class="woocommerce-thankyou-order-failed"><?php _e( 'Unfortunately your order cannot be processed as the originating bank/merchant has declined your transaction. Please attempt your purchase again.', 'woocommerce' ); ?></p>
<p class="woocommerce-thankyou-order-failed-actions">
<?php _e( 'Pay', 'woocommerce' ) ?>
<?php if ( is_user_logged_in() ) : ?>
<?php _e( 'My Account', 'woocommerce' ); ?>
<?php endif; ?>
</p>
<?php else : ?>
<?php
$coupons = $order->get_items( 'coupon' );
foreach ( $coupons as $item_id => $item ) {
echo "<span class='coupon-name'><b>".$item['name']."</b></span>";
$post = get_post( $item_id );
echo "<p class='coupon-description'>".$post->post_excerpt."</p>";
}
?>
<p class="woocommerce-thankyou-order-received"><?php echo apply_filters( 'woocommerce_thankyou_order_received_text', __( 'Thank you. Your order has been received.', 'woocommerce' ), $order ); ?></p>
<ul class="woocommerce-thankyou-order-details order_details">
<li class="order">
<?php _e( 'Order Number:', 'woocommerce' ); ?>
<strong><?php echo $order->get_order_number(); ?></strong>
</li>
<li class="date">
<?php _e( 'Date:', 'woocommerce' ); ?>
<strong><?php echo date_i18n( get_option( 'date_format' ), strtotime( $order->order_date ) ); ?></strong>
</li>
<li class="total">
<?php _e( 'Total:', 'woocommerce' ); ?>
<strong><?php echo $order->get_formatted_order_total(); ?></strong>
</li>
<?php if ( $order->payment_method_title ) : ?>
<li class="method">
<?php _e( 'Payment Method:', 'woocommerce' ); ?>
<strong><?php echo $order->payment_method_title; ?></strong>
</li>
<?php endif; ?>
</ul>
<div class="clear"></div>
<?php endif; ?>
<?php do_action( 'woocommerce_thankyou_' . $order->payment_method, $order->id ); ?>
<?php do_action( 'woocommerce_thankyou', $order->id ); ?>
<p class="woocommerce-thankyou-order-received"><?php echo apply_filters( 'woocommerce_thankyou_order_received_text', __( 'Thank you. Your order has been received.', 'woocommerce' ), null ); ?></p>
Could anyone please help me with this?
add_filter('woocommerce_get_order_item_totals','change_discount',10,3);
function change_discount($total_rows, $order, $tax_display){
if ( $order->get_total_discount() > 0 ) {
$coupons = $order->get_used_coupons();
$total_rows['discount'] = array( 'label' => __( 'Coupon code: '.implode(",",$coupons), 'woocommerce' ), 'value' => '-' . $order->get_discount_to_display( $tax_display ), );
}
return $total_rows;
}
you can get like this
if( $order->get_used_coupons() ) {
$coupons_count = count( $order->get_used_coupons() );
echo '<h4>' . __('Coupons used') . ' (' . $coupons_count . ')</h4>';
echo '<p><strong>' . __('Coupons used') . ':</strong> ';
$i = 1;
$coupons_list = '';
foreach( $order->get_used_coupons() as $coupon) {
$coupons_list .= $coupon;
if( $i < $coupons_count )
$coupons_list .= ', ';
$i++;
}
echo '<p><strong>Coupons used (' . $coupons_count . ') :</strong> ' . $coupons_list . '</p>'; }
Please use this code, I hope it will work for you:
<?php
add_action('woocommerce_thankyou', 'apply_product_on_coupon');
function apply_product_on_coupon() {
global $woocommerce;
if ( ! empty( $woocommerce->cart->applied_coupons ) ) {
$my_coupon = $woocommerce->cart->get_coupons() ;
foreach($my_coupon as $coupon){
if ( $post = get_post( $coupon->id ) ) {
if ( !empty( $post->post_excerpt ) ) {
echo "<span class='coupon-name'><b>".$coupon->code."</b></span>";
echo "<p class='coupon-description'>".$post->post_excerpt."</p>";
}
}
}
}
}
?>
If this will not work then use coupon shortcode plugin and call shortcode on the thankyou page
https://wordpress.org/plugins/woocommerce-coupon-shortcodes/
<?php echo do_shortcode('[coupon_shortcode]'); ?>