I would like to change the Text of the related products, which is at the end of the product detail page. At the moment I'm displaying This could be interesting by using this code
<h2><?php esc_html_e( 'This could be interesting', 'woocommerce' ); ?></h2>
What I would like to display is Our favorite category name
I tried to expand the code with this snippet, without any success
<?php echo wc_get_product_category_list($product->get_id()) ?>
How is it possible to get this function done?
Here is a little helper function that you could put in your functions.php
function get_favorite_category_title_for( $product_id ) {
$title = __('This could be interesting', 'woocommerce');
$cats = wp_get_post_terms( $product_id, 'product_cat' );
if( count($cats) > 0 ) {
$title = __( 'Our favorite ', 'woocommerce' ) . $cats[0]->name;
}
return $title;
}
and then replace the h2 tag with:
<h2><?php echo get_favorite_category_title_for( get_queried_object_id() ); ?></h2>
you can change get_queried_object_id with the $product->get_id() if you have access to $product object.
Related
I have added a custom text field to woocommerce and also displayed it in the frontend of WC Vendor pro. So vendors can add a youtube or vimeo link to embed their movies.
However for some reason I can't get it to save and display in the product page on the front end.
The code I have so far in functions.php:
// Display Fields
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
// Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
// Text Field
woocommerce_wp_text_input(
array(
'id' => 'video_url',
'label' => __( 'Your product video link (youtube/vimeo)', 'woocommerce' ),
'placeholder' => 'https://',
'desc_tip' => 'true',
'description' => __( 'Copy the Youtube or Vimeo link here', 'woocommerce' )
)
);
echo '</div>';
}
function woo_add_custom_general_fields_save( $post_id ){
// Text Field
$woocommerce_text_field = $_POST['video_url'];
if( !empty( $woocommerce_text_field ) )
update_post_meta( $post_id, 'video_url', esc_attr( $woocommerce_text_field ) );
}
And to display the field in the vendor section:
<div class="all-100">
<!-- Media uploader -->
<div class="wcv-product-media">
<?php BuddyBoss_BM_Templates::product_media_uploader( $object_id ); ?>
<?php woo_add_custom_general_fields( $object_id ); ?>
</div>
</div>
So now vendors can enter the field. However saving nor displaying it won't work. I added the following to the product page:
echo $youtubevideo_code = wp_oembed_get( get_field('video_url') );
// tried this one as well:
echo get_post_meta( $post->ID, 'video_url', true );
Thanks any help would be greatly appreciated!
Assuming you use acf to use get_field, i think you miss the second parameter $post_id, the function need to know the post id of the field you want to get.
echo $youtubevideo_code = wp_oembed_get( get_field('video_url') );
You must right it like this
global $post; // I don't know where is your script (in the loop ? in a function ?)
$youtubevideo_code = wp_oembed_get( get_post_meta($post->ID, 'video_url', true )); // can be get_field('video_url, $post->ID);
echo $youtubevideo_code;
I am at my wits end here and have tried every solution I could find online for days and nothing works. The worst part is the problem should be solvable in about 4 lines of code but it just isn't working.
What I need:
When the order completed email goes out I want the order notes (not the customer notes, the actual Order Notes) added into the email. I can filter through them after but I cannot seem to get the notes to appear in the email at all. This is an example of an order note...on an order:
so far I have tried this code:
::PHP::
<?php
$comments = $order->get_customer_order_notes();
if($comments){
echo '<h2>' . _e( 'Order Notes', 'woocommerce' ) . '</h2>';
foreach($comments as $comment) {
echo $comment->comment_content . '<br />';
}
}
?>
which is basically what I need except its targeting the customer_order_notes, which are comments users add into the order when they place it. like: "my dog will pick up my package, his name is lucky"
I have also written a lugin to get the notes based off other peoples, the base is this:
::PHP::
add_action( 'woocommerce_email_order_meta', 'bl_add_order_notes_to_completed_email', 10 );
function bl_add_order_notes_to_completed_email() {
global $woocommerce, $post;
// If the order is not completed then don't continue.
// if ( get_post_status( $post->ID ) != 'wc-completed' ){
// return false;
// }
$args = array(
'post_id' => $post->ID,
'status' => 'approve',
'type' => 'order_note'
);
// Fetch comments
$notes = get_comments( $args );
echo '<h2>' . _e( 'Order Notes', 'woocommerce' ) . '</h2>';
echo '<ul class="order_notes" style="list-style:none; padding-left:0px;">';
// Check that there are order notes
if ( $notes ) {
// Display each order note
foreach( $notes as $note ) {
?>
<li style="padding:0px -10px;">
<div class="note_content" style="background:#d7cad2; padding:10px;">
<?php echo wpautop( wptexturize( $note->comment_content ) ); ?>
</div>
<p class="meta">
<abbr class="exact-date" title="<?php echo $note->comment_date; ?>"><?php printf( __( 'added on %1$s at %2$s', 'woocommerce-customer-order-notes-completed-order-emails' ), date_i18n( wc_date_format(), strtotime( $note->comment_date ) ), date_i18n( wc_time_format(), strtotime( $note->comment_date ) ) ); ?></abbr>
<?php if ( $note->comment_author !== __( 'WooCommerce', 'woocommerce-customer-order-notes-completed-order-emails' ) ) printf( ' ' . __( 'by %s', 'woocommerce-customer-order-notes-completed-order-emails' ), $note->comment_author ); ?>
</p>
</li>
<?php
}
}
echo '</ul>';
}
I don't really know why that isnt working. It looks like it should but it does nothing.
If anyne has a solution that will print those notes into my email...like seen in this image...I will love you forever.
Create a folder called woocommerce in your theme folder. In this folder, create a new folder called emails, and in this folder duplicate the customer-completed-order.php from wp-content/plugins/woocommerce/templates/emails. And add this snippet at line 51: Refer this article
<h2><?php _e( 'Order Notes', 'woocommerce' ); ?></h2>
<?php
$args = array(
'status' => 'approve',
'post_id' => $order->id
);
$comments = get_comments($args);
foreach($comments as $comment) :
echo $comment->comment_content . '<br />';
endforeach;
?>
Try adding this code to your functions.php:
add_action( 'woocommerce_email_before_order_table', 'wc_add_order_notes_to_completed_emails', 10, 1 );
function wc_add_order_notes_to_completed_emails($order) {
if ( $email->id == 'customer_completed_order' ) {
echo '<h2>' . __( 'Order Notes', 'woocommerce' ) . '</h2>';
$order_notes = $order->get_customer_order_notes();
foreach ( $order_notes as $order_note ) {
echo '<p>' . $order_note->comment_content . '<p>';
}
}
}
I have been trying to achieve the same thing as Ecolis and came across a solution which may help other users. I simply wanted to echo an Order Note into an email. In my email template in my child theme folder I wrote the following code:
<?php echo wpautop( wptexturize( make_clickable( $customer_note ) ) ); ?>
Source of code - https://github.com/woocommerce/woocommerce/blob/master/templates/emails/customer-note.php
Answering this much later; but better than never. The final code I ended up using that worked and is still functioning as of today is the following:
?>
<h2><?php _e( 'Tracking ID', 'woocommerce' ); ?></h2>
<?php
$comments = $order->get_customer_order_notes();
$customer_comments = $order->get_order_notes();
foreach( $comments as $comment ){
if ( strpos( $comment -> comment_content, "MyTracking" ) !== false ){
echo $comment -> comment_content . '<br />';
}
}
foreach( $customer_comments as $comment_2 ){
if ( strpos( $comment_2 -> comment_content, "filterText" ) !== false ){
echo $comment_2 -> comment_content . '<br />';
}
}
This was put inside the email template customer-completed-order.php, taken from the woocommerce plugin's templates/emails/ directory. I placed the code in at the desired location and put the customer-completed-order.php file in my child theme in it's root directory within the following folders: woocommerce/emails/customer-completed-order.php
It then added both the customer and admin notes. I also added a filter, because my main goal was to get the tracking data posted to the admin order notes sent to the customer in their completed order email.
I stumbled across this older thread looking for a way to include the note that the customer places in "Order notes" on the checkout page in the Woocommerce New Order email received by the admin.
The code that #Martyn Gray posted above worked perfectly:
echo wpautop( wptexturize( make_clickable( $customer_note ) ) );
I copied the Woocommerce admin-new-order.php email template into my child theme and added it inside the existing code "user-defined additional content".
This is the original code:
/**
* Show user-defined additional content - this is set in each email's settings.
*/
if ( $additional_content ) {
echo wp_kses_post( wpautop( wptexturize( $additional_content ) ) );
}
This is the updated code:
/**
* Show user-defined additional content - this is set in each email's settings.
*/
if ( $additional_content ) {
echo wp_kses_post( wpautop( wptexturize( $additional_content ) ) );
echo wpautop( wptexturize( make_clickable( $customer_note ) ) );
}
The customer's note now appears on all Woocommerce emails including new order, invoice, etc.
WP v5.6.2
WC v5.0.0
Hope that helps anyone that follows.
I want to be able to display a product title by using PHP to echo the product name by the product ID (this is to be displayed within a Page, not the product page itself). I am using Wordpress and I have a plugin for PHP so I can include PHP code using [php]echo 'example';[/php]
One product example is; http://ukcctvinstallations.co.uk/product/1-camera-residential-system-hi-res/
When I edit the product, you can see in the URL that the 'Post' = 129 so am I right in saying this is the product ID?
If anyone has a solution for this, that would be much appreciated. I am using WooCommerce.
Assuming you get the product as an object
$product = wc_get_product( id );
echo $product->get_title();
(WC version 2.5.2)
Found a solution:-
echo get_the_title( 'ID' );
Take a look here:
http://docs.woothemes.com/wc-apidocs/package-WooCommerce.Functions.Product.html
2017 - 2020 - Since WooCommerce 3, use the WC_Product method get_name()
global $product;
// If the WC_product Object is not defined globally
if ( ! is_a( $product, 'WC_Product' ) ) {
$product = wc_get_product( get_the_id() );
}
echo $product->get_name();
On cart items:
foreach( WC()->cart->get_cart() as $cart_item ) {
echo $cart_item['data']->get_name();
}
On Order items:
foreach( $order->get_items() as $cart_item ) {
echo $item->get_name();
// Or
$product = $item->get_product(); // Get the WC_Product Object
echo $product->get_name();
}
Here is a bit of PHP code from a page in my CCTV wordpress site:
<header class="entry-header">
<?php
if ( is_singular() ) :
the_title( '<h1 class="entry-title">', '</h1>' );
else :
the_title( sprintf( '<h2 class="entry-title">', esc_url( get_permalink() ) ), '</h2>' );
endif;
?>
For my WC product pages, I need to add a class to the body tag so that I can perform some custom styling. Here's the function I'm creating for this...
function my_add_woo_cat_class($classes) {
$wooCatIdForThisProduct = "?????"; //help!
// add 'class-name' to the $classes array
$classes[] = 'my-woo-cat-id-' . $wooCatIdForThisProduct;
// return the $classes array
return $classes;
}
//If we're showing a WC product page
if (is_product()) {
// Add specific CSS class by filter
add_filter('body_class','my_add_woo_cat_class');
}
...but how do I get the WooCommerce cat ID?
A WC product may belong to none, one or more WC categories. Supposing you just want to get one WC category id.
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
$product_cat_id = $term->term_id;
break;
}
Please look into the meta.php file in the "templates/single-product/" folder of the WooCommerce plugin.
<?php echo $product->get_categories( ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', sizeof( get_the_terms( $post->ID, 'product_cat' ) ), 'woocommerce' ) . ' ', '.</span>' ); ?>
$product->get_categories() is deprecated since version 3.0! Use wc_get_product_category_list instead.
https://docs.woocommerce.com/wc-apidocs/function-wc_get_product_category_list.html
I literally striped out this line of code from content-single-popup.php located in woocommerce folder in my theme directory.
global $product;
echo $product->get_categories( ', ', ' ' . _n( ' ', ' ', $cat_count, 'woocommerce' ) . ' ', ' ' );
Since my theme that I am working on has integrated woocommerce in it, this was my solution.
Thanks Box. I'm using MyStile Theme and I needed to display the product category name in my search result page. I added this function to my child theme functions.php
Hope it helps others.
/* Post Meta */
if (!function_exists( 'woo_post_meta')) {
function woo_post_meta( ) {
global $woo_options;
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
$product_cat = $term->name;
break;
}
?>
<aside class="post-meta">
<ul>
<li class="post-category">
<?php the_category( ', ', $post->ID) ?>
<?php echo $product_cat; ?>
</li>
<?php the_tags( '<li class="tags">', ', ', '</li>' ); ?>
<?php if ( isset( $woo_options['woo_post_content'] ) && $woo_options['woo_post_content'] == 'excerpt' ) { ?>
<li class="comments"><?php comments_popup_link( __( 'Leave a comment', 'woothemes' ), __( '1 Comment', 'woothemes' ), __( '% Comments', 'woothemes' ) ); ?></li>
<?php } ?>
<?php edit_post_link( __( 'Edit', 'woothemes' ), '<li class="edit">', '</li>' ); ?>
</ul>
</aside>
<?php
}
}
?>
<?php
$terms = get_the_terms($product->ID, 'product_cat');
foreach ($terms as $term) {
$product_cat = $term->name;
echo $product_cat;
break;
}
?>
To add custom classes to the body tag you can use the body_class hook.
To add one or more classes on the product page based on specific product categories you can use the Wordpress has_term function (to check if a product belongs to that specific product category).
For this I created the array $classes_to_add where:
key: It can be the product category id (or the slug or the name). Or an array of them. Read the documentation.
value: a string containing the classes to add to the body tag. If you want to add more than one class create a string with multiple values separated by a space (see my example below)
So:
// adds a class to the body element based on the product category
add_filter( 'body_class', 'add_body_class_based_on_the_product_category' );
function add_body_class_based_on_the_product_category( $classes ) {
// only on the product page
if ( ! is_product() ) {
return $classes;
}
// create an array with the ids (or slugs) of the product categories and the respective class (or classes) to add
$classes_to_add = array(
30 => 'class-1',
'cat_slug' => 'class-2 class-3',
32 => 'class-4 class-5',
);
// if the product belongs to one of the product categories in the array it adds the respective class (or classes)
foreach ( $classes_to_add as $product_cat => $new_classes ) {
if ( has_term( $product_cat, 'product_cat', get_the_ID() ) ) {
$classes[] = $new_classes;
}
}
return $classes;
}
The code has been tested and works. Add it to your active theme's functions.php.
<?php
switch ( $product->product_type ) {
case "variable" :
$link = apply_filters( 'variable_add_to_cart_url', get_permalink( $product->id ) );
$label = apply_filters( 'variable_add_to_cart_text', __('Select options', 'woocommerce') );
break;
case "grouped" :
$link = apply_filters( 'grouped_add_to_cart_url', get_permalink( $product->id ) );
$label = apply_filters( 'grouped_add_to_cart_text', __('View options', 'woocommerce') );
break;
case "external" :
$link = apply_filters( 'external_add_to_cart_url', get_permalink( $product->id ) );
$label = apply_filters( 'external_add_to_cart_text', __('Read More', 'woocommerce') );
break;
default :
$link = apply_filters( 'add_to_cart_url', esc_url( $product->add_to_cart_url() ) );
$label = apply_filters( 'add_to_cart_text', __('Add to cart', 'woocommerce') );
break;
}
printf('%s', $link, $product->id, $product->product_type, $label);
?>
I'm trying to get variations to display inside the loop so customers can add variable products to their cart from the shop page (please see screenshot below)...
http://cl.ly/image/42401k0X0X2I
I know I need to include the function-
get_available_variations();
And i'm pretty sure this already returns an array, it's just putting that array into a select dropdown + listing the variations (S,M,L,XL) and having a link to add that variation to the basket.
Cheers!
I found your post while trying to solve the same problem. I finally found...
function woocommerce_variable_add_to_cart() {
global $product;
// Enqueue variation scripts
wp_enqueue_script( 'wc-add-to-cart-variation' );
// Load the template
woocommerce_get_template( 'single-product/add-to-cart/variable.php', array(
'available_variations' => $product->get_available_variations(),
'attributes' => $product->get_variation_attributes(),
'selected_attributes' => $product->get_variation_default_attributes()
) );
}
}
in
woocommerce-template.php
This works for me in loop/add-to-cart.php
switch ( $product->product_type ) {
case "variable" :
$link = apply_filters( 'variable_add_to_cart_url', get_permalink( $product->id ) );
$label = woocommerce_variable_add_to_cart();
break;
Let me know if this helps :)
The variations dropdown template file for single post pages is located here:
woocommerce\templates\single-product\add-to-cart\variable.php
Which requires the following script to pass the product variable information:
<script type="text/javascript">
var product_variations_<?php echo $post->ID; ?> = <?php echo json_encode( $available_variations ) ?>;
</script>
as well as the following hidden field:
<input type="hidden" name="variation_id" value="" /> - where the value is the variation ID
I hope that is a start others can help build upon.
I found on Remi Corson's blog a simple way to do that.
Change the href value of the cart button with the following:
http://example.com/cart/?add-to-cart=[PRODUCT_ID]&variation_id=[VARIATION_ID]&attribute_pa_[ATTRIBUTE_SLUG]=[ATTRIBUTE_SLUG_VALUE]
Example:
http://example.com/cart/?add-to-cart=123&variation_id=456&attribute_pa_colour=black
With the get_available_variations(); function is easy to get the variation values. For the product ID, you can use get_the_ID(); function.