Trying to get Product Data on custom Template By product ID, right now i have this code to get Product Title.
$productId = 164;
echo $p_title = get_the_title( $productId );
looking for Short Description, Price, Product Image, Product Url, Product Brand. Or might be loop will be better but that loop should work with product static ID.
Thanks in advance.
You would probably be better served by creating a new product object.
$productId = 164;
$product = wc_get_product( $productId );
echo $product->get_title();
echo $product->get_price_html();
Note, that the short description is merely the post's post_excerpt. If using outside of the loop (where $post is automatically defined) then you would need to get the post directly.
$post = get_post( $productId );
echo apply_filters( 'woocommerce_short_description', $post->post_excerpt );
or alternatively, if you've already defined the product object you could do
echo apply_filters( 'woocommerce_short_description', $product->post->post_excerpt );
since the WooCommerce product class will automatically define the $product->post property with get_post().
apply_filters() means that functions can be attached at this point to modify the content of $product->post->post_content. At a minimum, I know that wpautop() is attached to the woocommerce_short_description filter to create paragraph breaks.
Related
I saw this, WooCommerce - Get the product description by product_id
But I was hoping there was a shorter way to get the product by ID and get the description? Any idea?
I'm using WooCommerce 3.0 and above.
In your question, we don't know what do you mean by "GRAB"… It can be how to SET (or how to GET) the product description.
There is multiple ways to SET the product description.
1) From the product ID using Wordpress wp_update_post();
wp_update_post( array('ID' => $product_id, 'post_content' => 'This is the <strong>product description</strong> content.') );
2) From the WC_Product Object using set_description() method
// Get the WC_Product Object instance (optional if needed)
$product = wc_get_product( $product_id );
// Set the description
$product->set_description('This is the <strong>product description</strong> content.');
$product->save(); // Save product data
There is multiple ways to GET the product description (as explained in this thread).
1) From the product ID, using Wordpress get_post() function:
$product_description = get_post( $product_id )->post_content;
2) From the WC_Product Object using get_description() method
// Get the WC_Product Object instance (optional if needed)
$product = wc_get_product( $product_id );
// Get the product description
$description = $product->get_description();
I am trying to get a product custom field ( ACF ) based on product ID in a post page.
I tried things like this $price[] = get_sub_field('price', $productId); but it doesn't seems to work when taking something else than the post id.
I managed to get the title though by doing like this <?php echo $product->get_title(); ?> but not sure on how to go to get the custom fields.
As we know $product->ID will not work anyway. So I think now your problem is how to get the product ID.
=> In Archive pages like shop or in Single product pages:
Using get_the_id() will give you mostly always the product id (the post ID).
$price= get_post_meta( get_the_id(), 'price', true );
reference link
You can also use global $product; with $product->get_id()
global $product;
$price = get_post_meta( $product->get_id(), 'price', true );
I am trying to show the price of a product in a post by code using HTML.
I am trying to utilize $product->get_price(). however, I need to be able to call the product by identifying it, using its code or something like it.
So Basically, all I want to do is to show the price of a particular product in the post by using a product ID as reference for example.
Any idea?
If you have the product's ID you can use that to create a product object:
$_product = wc_get_product( $product_id );
Then from the object you can run any of WooCommerce's product methods.
$_product->get_regular_price();
$_product->get_sale_price();
$_product->get_price();
Hope this one helps.
you use wc_get_product function :
$_product = wc_get_product( $product_id );
$_product->get_regular_price();
$_product->get_sale_price();
$_product->get_price();
I need to a php file that can show a product's price by id. I have tried doing this with this code in a "price.php" file with this content:
I'm sure that something that links this with wp is missing, but what?
<?php
$price = get_post_meta( '225', '_regular_price', true);
echo $price;
?>
If you have product's ID you can use that to create product object:
$product = new WC_Product($product_id);
After Creating Object you can run any of WooCommerce's product methods.
$product->get_regular_price();
$product->get_sale_price();
$product->get_price();
I want to be able to target certain product-pages with CSS based on the SKU of the product they have on. Therefore I need to add the SKU as a body class for all Woocommerce products. How would I accomplish this?
//Add Woocommerce body classes
add_filter('body_class','sku_woocommerce_body_classes');
function sku_woocommerce_body_classes($classes){
global $post;
$product = get_product( $post->ID );
if ( $product->product_type == 'simple');
$classes[] = $product->get_sku();
return $classes;
}
You weren't clear on what type of pages to target, but the if statement will get you where you need to go in order to target them.