I need to show the product's excerpt in the Orders page. I spent several hours trying to find a solution but nothing.
I already show the image & the title of the product (Thanks to #helgatheviking & this thread) , but I can't get the excerpt to show. This is my code:
<div id="order-column" class="my_account_orders">
<div class="wrap">
<?php
foreach ( $customer_orders as $customer_order ) {
$order = wc_get_order( );
$order->populate( $customer_order );
foreach( $order->get_items() as $item_id => $item ) {
$product = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );
$product->get_image();
$product->get_title();
}
$item_count = $order->get_item_count();
?>
<div class="orders-wrap">
<div class="preview">
<div class="image">
<div class="image-wrap"><?php echo $product->get_image(); ?></div>
</div>
<div class="bottom">
<div class="details">
<h3 class="name"><a title="View Order" href="<?php echo $order->get_view_order_url(); ?>"><?php echo $product->get_title(); ?></a></h3>
<h4 class="subtitle"><?php the_excerpt(); ?></h4>
</div>
</div>
The excerpt should appear in subtitle.
I have checked and tried the suggestions in these threads:
Woocommerce - description in products page
Adding a product description to the woocommerce cart page
This should do it. the_excerpt can only be used in combination with the_post() as it depends on the global $post object. But this pretty much reassembles what happens inside it.
<h4 class="subtitle"><?php echo apply_filters( 'the_excerpt', $product->post->post_excerpt ); ?></h4>
A bit late but always use this here:
get_the_excerpt( $product->get_id() );
The other solution from Johan Palmfjord will work but WooCommerce says that you never should access the product data directly.
Related
I'm new in wordpress and woocommerce development part. I'm trying to integrate woocommerce for my products, everything was fine. But when i click on product permalink for product details or product single page it's redirect to the same page. Interesting thing is when i click on product, the url changed but not go through to the details page.
Here is the page link- https://dev.shopvitalsleep.com/collections/all/
I'm using loop inside woocommerce.php with my custom designed page.
Here is the code sample-
<div class="row justify-content-center equal-box">
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 12
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
$product = wc_get_product($loop->post->ID);
?>
<div class="col-lg-6 mb-4">
<div class="product-content text-center">
<div class="product-images">
<a href="<?php the_permalink(); ?>" class="product-details-link">
<?php the_post_thumbnail('full'); ?>
</a>
</div>
<div class="product-info">
<h4 class="product-title"><?php echo get_the_title(); ?></h4>
<div class="price">
<p>
<span class="reg-price">
<del>$<?php echo get_post_meta( get_the_ID(), '_regular_price', true ); ?></del>
</span>
<span class="sell-price">now $<?php echo get_post_meta( get_the_ID(), '_sale_price', true ); ?> USD</span>
</p>
</div>
</div>
</div>
<div class="row mt-auto product-content text-center mb-2">
<div class="col-6">
<a class="button style-blue view-detail-button" href="<?php the_permalink(); ?>">View Details</a>
</div>
<div class="col-6">
<div class="add-to-cart "><?php
echo sprintf( '<a href="%s" data-quantity="1" class="%s" %s>%s</a>',
esc_url( $product->add_to_cart_url() ),
esc_attr( implode( ' ', array_filter( array(
'button', 'product_type_' . $product->get_type(),
$product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
$product->supports( 'ajax_add_to_cart' ) ? 'ajax_add_to_cart' : '',
) ) ) ),
wc_implode_html_attributes( array(
'data-product_id' => $product->get_id(),
'data-product_sku' => $product->get_sku(),
'aria-label' => $product->add_to_cart_description(),
'rel' => 'nofollow',
) ),
esc_html( $product->add_to_cart_text() )
);
?>
</div>
</div>
</div>
</div>
<?php
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</div>
Folder structure:
../themes/theme-name/woocommerce.php
Please help me to findout where i make mistakes.
Looks like your WooCommerce single page is not working.
Take a look at their official docs for the correct file structure. I think you have a problem here referencing incorrect file or you have overwritten core file, like woocommerce.php.
You should use a child theme and then do your edits there.
WooCommerce Docs Template Structure
I figured it out!
The problem is woocommerce.php file. This file overwrite the other directory.
In my case, I'm developing a custom theme that's why I need to create a directory inside my ../themes/my-theme nameed- /woocommerce/archive-product.php and delete woocommerce.php form ../themes/my-theme
So, moral of the topic is-
If your theme has a woocommerce.php file, you will be unable to override the woocommerce/archive-product.php custom template in your theme, as woocommerce.php has priority over other template files. This is intended to prevent display issues.
I have a component called cart checkout that is displayed on a the woocommerce archive-product.php page. When someone clicks add to card on one of the products on this page I want this component to update to show the amount of items, the name and the price. I am currently using the following code to display this information, but you have to refresh the page to see it updated after hitting add to cart.
<div class="cart-wrap">
<p class="cart-title">Currently in Cart</p>
<?php
global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$_product = wc_get_product( $values['data']->get_id());
echo "<p>" . $_product->get_title().'<span class="amount"> x '.$values['quantity'];
$price = get_post_meta($values['product_id'] , '_price', true);
echo " $".$price."</span></p>";
}
?>
<a class="cart-checkout" href="<?php echo wc_get_cart_url(); ?>" >Checkout</a>
</div>
I want to use ajax I would assume but don't even know where to start to figure out how to get it to work.
Ok thanks ahead of time.
To ajaxify cart components (or called in Woocommerce fragments), you will use the following:
1) your revisited code component:
<div class="cart-wrap">
<p class="cart-title"><?php _e("Currently in Cart", "woocommerce"); ?></p>
<div id="cart-items-wrap">
<?php
foreach( WC()->cart->get_cart() as $cart_item ) {
printf( '<p>%s<span class="amount"> x %s %s</span></p>',
$cart_item['data']->get_title(),
$cart_item['quantity'],
wc_price( wc_get_price_to_display( $cart_item['data'] ) )
);
}
?>
</div>
<a class="cart-checkout" href="<?php echo wc_get_cart_url(); ?>"><?php _e("Checkout", "woocommerce"); ?></a>
</div>
2) The hooked function that will ajax refresh your component:
add_filter( 'woocommerce_add_to_cart_fragments', 'ajaxify_components', 10, 1 );
function ajaxify_components( $fragments ) {
ob_start();
?>
<div id="cart-items-wrap">
<?php
foreach( WC()->cart->get_cart() as $cart_item ) {
printf( '<p>%s<span class="amount"> x %s %s</span></p>',
$cart_item['data']->get_title(),
$cart_item['quantity'],
wc_price( wc_get_price_to_display( $cart_item['data'] ) )
);
}
?>
</div>
<?php
$fragments['#cart-items-wrap'] = ob_get_clean();
return $fragments;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Following the guide here http://www.w3bdeveloper.com/how-to/how-to-get-regular-price-of-a-product-in-wordpress-woocommerce/
Using the following code to call the price inside the loop, the price is displaying the wrong price per item.
The first item has no price, the second item has the first items price, the third item has the second price and so on.
<div class='price'>
<?php echo $product->regular_price; ?>
</div>
If I use <?php echo $product->get_price_html(); ?> it displays correctly, but I'd like to be able to display the sale price and the regular price separately. Also for some reason, if I use this code before the button code, I get a fatal error.
The loop code is as follows:
<div class='post'>
<a class='oxy-post-image' href='<?php the_permalink(); ?>'>
<div class='oxy-post-image-fixed-ratio' style='background-image: url(<?php echo get_the_post_thumbnail_url(); ?>);'></div>
<div class='price-overlay'>
<?php echo $product->regular_price; ?>
</div>
</a>
<div class='post-wrapper'>
<a class='oxy-post-title' href='<?php the_permalink(); ?>'><?php the_title(); ?></a>
<div class='oxy-post-meta'>
<div class='cart-button'>
<?php 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 ); ?>
</div>
</div>
</div>
</div>
A screenshot of the items in the loop:
Try to declare global $product before using echo $product->regular_price; so :
<div class='price-overlay'>
<?php global $product; echo $product->regular_price; ?>
</div>
I want to be able to show the current price and sale price if it exists within my WP_Query products loop. At the moment my code just replaces the current price with the sale price. I want an if statement that checks if the sale price is there or not. If it is I want to display both;
Here's my code;
<?php
$args = array(
'post_type' => 'product',
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num',
'posts_per_page' => 4,
);
$query = new WP_Query( $args );
?>
<?php if($query->have_posts()): ?>
<div class="container" id="best_sellers">
<div class="row">
<?php while( $query->have_posts() ): $query->the_post(); ?>
<?php $product = wc_get_product(get_the_ID());?>
<div class="col-md-3">
<div class="cont">
<a href="<?php the_permalink(); ?>">
<div class="image_cont">
<?php the_post_thumbnail(); ?>
</div>
<p class="sku"><?php echo $product->get_sku(); ?></p>
<p class="title"><?php the_title(); ?></p>
</a>
<div class="quantity">
<p>Quantity: </p>
<button class="increment">+</button>
<input type="text" value="1" min="1" max="<?php echo $product->get_stock_quantity(); ?>">
<button class="decrement">-</button>
</div>
<p class="price"><?php echo wc_price( wc_get_price_including_tax( $product ) ); ?></p>
Add to Basket
</div>
</div>
<?php endwhile; ?>
</div>
</div>
<?php endif; ?>
If you look at the price class you can see my code for getting the price.
update: I have been looking through the WooCommerce templates files and found this;
<?php if ( $product->is_on_sale() ) : ?>
This works, I just need to be able to grab both prices separately as at the moment
<?php echo wc_price( wc_get_price_including_tax( $product ) ); ?>
just gets overwritten by the sale price
Do you mean regular and sale prices?
You can work with below code but this will only with simple product, it won't work on product variation.
global $product;
if( $product->is_on_sale() ) {
$sale_price = $product->get_sale_price();
}
$regular_price = $product->get_regular_price();
I managed to find this
<?php echo $product->get_price_html(); ?>
It does everything I wanted without having to make an if statement
If you want to get the sale price with taxes, try this:
$sale_price = wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price()) );
You will have to prefix the currency which can be achieved via get_woocommerce_currency_symbol()
Within woo commerce I am pulling all of the featured items into the home page. Within the featured Items I have an if statement nested so that it checks to see what category each featured item is in.
I am doing so so that if its in the category flower I can ad the suffix OZ to the price and within concentrates I can add the suffix GM to the price.
It works great with 9 items or less. Once more than 9 items are applied they are all displaying OZ no matter what the category.
There are a few other things in there checking for logged in vs not logged in. but here is the whole snippet
<!------ check cat -->
<?php
global $post;
$terms = wp_get_post_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ) $categories[] = $term->slug;
?>
<?php if ( in_array( 'flower', $categories ) ) { ?>
<div class="price_home">
<div class="product__inside__price price-box">
<?php if ( $price_html = $product->get_price_html() ) : ?>
<span class="price"><?php echo wp_kses_post($price_html); ?> oz</span>
<?php endif; ?>
</div>
</div>
<?php } elseif ( in_array( 'concentrates', $categories ) ) { ?>
<div class="price_home">
<div class="product__inside__price price-box">
<?php if ( $price_html = $product->get_price_html() ) : ?>
<span class="price"><?php echo wp_kses_post($price_html); ?> gm</span>
<?php endif; ?>
</div>
</div>
<?php } else { ?>
<div class="price_home">
<div class="product__inside__price price-box">
<?php if ( $price_html = $product->get_price_html() ) : ?>
<span class="price"><?php echo wp_kses_post($price_html); ?
</span>
<?php endif; ?>
</div>
</div>
<? } ?>
I simplified this code but as I stated in the comments. This is used to split out the featured products. So I'm not 100% sure this is the issue, but people were having an issue viewing so much code.