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;
?>
Related
This code is outputting the Product (Brand) Category in (almost) the right spot - above the product title - on the Single Product Page.
How can I hyperlink it (Product Category) to the Product Category page? I essentially need to make this <?php echo $single_cat->name; ?> a dynamic hyperlink.
/** Output Product (Brand) Category on single product page **/
function add_brand_category(){
$product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );
if ( $product_cats && ! is_wp_error ( $product_cats ) ){
$single_cat = array_shift( $product_cats ); ?>
<h3 itemprop="name" class="product_category_title"><span><?php echo $single_cat->name; ?></span></h3>
<?php }
}
add_action( 'woocommerce_single_product_summary', 'add_brand_category', 2 );
You can use wc_get_product_category_list() – which returns the product categories in a list + adding a hyperlink to the product category page.
So you get:
function action_woocommerce_single_product_summary() {
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
echo '<h3 itemprop="name" class="product_category_title">';
echo wc_get_product_category_list( $product->get_id(), ', ', '<span>' . _n( 'Category:', 'Categories:', count( $product->get_category_ids() ), 'woocommerce' ) . ' ', '</span>' );
echo '</h3>';
}
}
add_action( 'woocommerce_single_product_summary', 'action_woocommerce_single_product_summary', 2 );
I have added badges to my product images to display important information. If a product has a specific tag, it will display that specific badge. I got this to work in the shop page and single product page, but having trouble with the cart page. I can load one badge to all the items in the cart, but can't seem to load the other badges conditionally.
These are the conditional statements I used for the shop page and single product page:
global $product;
if ( $product->is_on_sale() ) {
echo '<span class="product-badge">SALE</span>';
}elseif ( has_term( 'reversible', 'product_tag' ) ) {
echo '<span class="product-badge">REVERSIBLE</span>';
} elseif ( has_term( 'hard-sole', 'product_tag' ) ) {
echo '<span class="product-badge">HARD SOLE</span>';
} elseif ( has_term( 'soft-sole', 'product_tag' ) ) {
echo '<span class="product-badge">SOFT SOLE</span>';
}
This is the code I have so far for the cart page:
add_filter( 'woocommerce_cart_item_thumbnail', 'cart_badge' );
function cart_badge( $thumbnail ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if ( has_term( 'reversible', 'product_tag', $cart_item['product_id'] ) )
{
printf( '%s<span class="cart-badge">REVERSIBLE</span>',
esc_url( $product_permalink ), $thumbnail ); // PHPCS: XSS ok.
}
}
}
How do I add apply my conditional statements to my current code to show the specific product badge on specific product thumbnail, or else echo $thumbnail if conditions are false?
I tried searching online and contacting the plugin author, and he said that it can be retrieved using the WordPress get post meta.
I'm using a plugin called woo-gst to add a product attribute called 'prod_hsn_id' which add a filed called HSN code at product edit page, I'm also using a pdf invoice plugin called woocommerce pdf invoice to generate pdf invoice. Now I want to display the HSN code on the invoice.
<?php foreach ( $this->order->get_items( 'line_item' ) as $item_id => $item ) {
$product = $this->order->get_product_from_item( $item ); ?>
<tr class="product-row">
<td>
<?php echo esc_html( $item['name'] );
global $wpdb;
$hidden_order_itemmeta = apply_filters( 'woocommerce_hidden_order_itemmeta', array(
'_qty',
'_tax_class',
'_product_id',
'_variation_id',
'_line_subtotal',
'_line_subtotal_tax',
'_line_total',
'_line_tax',
'_wc_cog_item_cost',
'_wc_cog_item_total_cost',
'_reduced_stock',
) );
$hidden_order_itemmeta = apply_filters( 'bewpi_hidden_order_itemmeta', $hidden_order_itemmeta );
foreach ( $this->order->has_meta( $item_id ) as $meta ) {
// Skip hidden core fields.
if ( in_array( $meta['meta_key'], $hidden_order_itemmeta, true ) ) {
continue;
}
// Skip serialised meta.
if ( is_serialized( $meta['meta_value'] ) ) {
continue;
}
// Get attribute data.
if ( taxonomy_exists( wc_sanitize_taxonomy_name( $meta['meta_key'] ) ) ) {
$term = get_term_by( 'slug', $meta['meta_value'], wc_sanitize_taxonomy_name( $meta['meta_key'] ) );
$meta['meta_key'] = wc_attribute_label( wc_sanitize_taxonomy_name( $meta['meta_key'] ) );
$meta['meta_value'] = isset( $term->name ) ? $term->name : $meta['meta_value'];
} else {
$meta['meta_key'] = apply_filters( 'woocommerce_attribute_label', wc_attribute_label( $meta['meta_key'], $product ), $meta['meta_key'] );
}
echo '<div class="item-attribute"><span style="font-weight: bold;">' . wp_kses_post( rawurldecode( $meta['meta_key'] ) ) . ': </span>' . wp_kses_post( rawurldecode( $meta['meta_value'] ) ) . '</div>';
}
$field_name = 'hsn_prod_id';
// then loop through items in order and print each custom field
foreach ( $this->order->get_items() as $item_id => $item ) {
if ( $product = $this->order->get_product_from_item( $item ) ) {
$location = $product->get_meta( $field_name );
if ( !empty($hsn_prod_id) ) {
echo '<div class="product-location">HSN Code: '.$hsn_prod_id.'</div>';
}
}
}
?>
</td>```
above is the code In the Invoice Template file I'm trying to display the HSN Code.
I was working on the same today, did not find a solution, and finally here is what I did and it worked well.
We need to pull the data from postmeta table, and to get that you need to have the post id for the product.
If you do $product_id = $product->get_id() this will return the id (they call it Variation ID). The post id and product id are not same.
To get the post id for the product you need to subtract -1 form the $product_id.
This is the line you can use to get the HSN code :
$hsn_prod_id = get_post_meta( $product->get_id()-1, 'hsn_prod_id', true );
UPDATE:
The logic for subtract -1 depends on the Wordpress,WooCommerce version may be. I have tried the same in a fresh setup where it didn't work. In there, I just used this $product_id = $product->get_id() value and it worked.
You can figure it by looking at the id for the product. The id it shows in the UI and the ID you see in the URL when you edit an order. If both are not the same you need to see the sequence difference and take a call.
I have made a filter to update how order is displayed on woocommerce.
Basically I need the shop owner to be able to click the name of each product (linked now to the featured image) and also him to be able to see the URL (because the image file name is useful for them to track the product)
I need this to ONLY affect the NEW ORDER email sent to the shop owner.
My code placed in functions.php does update BUT in ALL emails and also order confirmation table at the website.
Question? How can I ONLY affect the new order email? I think I'm missing something here.
// item name link to product
add_filter( 'woocommerce_order_item_name', 'display_product_title_as_link', 10, 2 );
function display_product_title_as_link( $item_name, $item ) {
$_product = get_product( $item['variation_id'] ? $item['variation_id'] : $item['product_id'] );
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $_product->post->ID ), 'full' );
return ''. $item_name .'
<div style="color:blue;display:inline-block;clear:both;">'.$image[0].'</div>';
}
#LoicTheAztec - it could not work! href as image? Full image as a thumbnail?(big email) Missing permalink to product... Repair function display_product_title_as_link:
function display_product_title_as_link( $item_name, $item ) {
$product = wc_get_product( $item['variation_id'] ? $item['variation_id'] : $item['product_id'] );
$image = wp_get_attachment_image_src( $product->get_image_id(), 'thumbnail' );
$product_name = $product->get_name();
$product_link = get_permalink( $product->get_id() );
return '<img width="70" height="70" src="'.$image[0].'" alt="'. $product_name .'">'. $product_name .' ';
}
First there is some errors in your code like:
The function get_product() is clearly outdated and has been replaced by wc_get_product()
Since Woocommerce 3+ WC_Product properties can be accessed directly, instead use available methods.
Here is the right way to get what you are expecting (in "New Order" admin notification only):
// Your custom function revisited
function display_product_title_as_link( $item_name, $item ) {
$product = wc_get_product( $item['variation_id'] ? $item['variation_id'] : $item['product_id'] );
$image = wp_get_attachment_image_src( $product->get_image_id(), 'full' );
$product_name = $product->get_name();
return ''. $product_name .'
<div style="color:blue;display:inline-block;clear:both;">'.$image[0].'</div>';
}
// The hooked function that will enable your custom product title links for "New Order" notification only
add_action( 'woocommerce_email_order_details', 'custom_email_order_details', 1, 4 );
function custom_email_order_details( $order, $sent_to_admin, $plain_text, $email ){
// Only for "New Order" and admin email notification
if ( 'new_order' != $email->id && ! $sent_to_admin ) return;
// Here we enable the hooked function
add_filter( 'woocommerce_order_item_name', 'display_product_title_as_link', 10, 3 );
}
Code goes in function.php file of your active child theme (active theme or in any plugin file).
Tested and Works in WooCommerce 3+
I would like to display SKU on cart (Under product column ) and checkout page.
I searched SO, but all answers are for old versions of WooCommerce and non of them is for 3.x.
How can I show SKU on cart and checkout pages in Woocommerce 3?
2021 Update
You can do it with a custom unction hooked in woocommerce_cart_item_name action hook, this way:
// Display the sku below cart item name
add_filter( 'woocommerce_cart_item_name', 'display_sku_after_item_name', 5, 3 );
function display_sku_after_item_name( $item_name, $cart_item, $cart_item_key ) {
$product = $cart_item['data']; // The WC_Product Object
if( is_cart() && $product->get_sku() ) {
$item_name .= '<br><span class="item-sku">'. $product->get_sku() . '</span>';
}
return $item_name;
}
// Display the sku below under cart item name in checkout
add_filter( 'woocommerce_checkout_cart_item_quantity', 'display_sku_after_item_qty', 5, 3 );
function display_sku_after_item_qty( $item_quantity, $cart_item, $cart_item_key ) {
$product = $cart_item['data']; // The WC_Product Object
if( $product->get_sku() ) {
$item_quantity .= '<br><span class="item-sku">'. $product->get_sku() . '</span>';
}
return $item_quantity;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works on WooCommerce 3+. You will get:
And
Related similar: How to Show SKU with product title in Order received page and Email order
You'll need to do some template overrides.
Cart
Copy plugins/woocommerce/templates/cart/cart.php into your theme file at my_theme/woocommerce/cart/cart.php if it isn't already there. Then add the following at approx line #85
// Meta data
// (this is already in cart.php; look for it for where to place the next snippet)
echo WC()->cart->get_item_data( $cart_item );
// Add SKU below product name
if ( !empty($_product->get_sku()) ) { ?>
<div class="sku">
<small><?php echo "SKU: ". $_product->get_sku(); ?></small>
</div>
<?php }
Checkout
Copy plugins/woocommerce/templates/checkout/review-order.php into your theme file at my_theme/woocommerce/checkout/review-order.php if it isn't already there. Then add the following at approx line #43
<?php
// (this is already in review-order.php; look for it for where to place the next snippet)
echo WC()->cart->get_item_data( $cart_item ); ?>
<?php
// Add SKU below product name
if ( !empty($_product->get_sku()) ) { ?>
<div class="sku">
<small><?php echo "SKU: ". $_product->get_sku(); ?></small>
</div>
<?php } ?>
You can do it with this way:
/**
* #snippet Show SKU # WooCommerce Cart
* #author Khalid Almallahi
*/
add_action( 'woocommerce_after_cart_item_name', 'abukotsh_sku_below_cart_item_name', 11, 2 );
function abukotsh_sku_below_cart_item_name( $cart_item, $cart_item_key ) {
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
$sku = $_product->get_sku();
if ( ! $sku ) return;
echo '<p><small>SKU: ' . $sku . '</small></p>';
}