How do i use WC_Product Methods outside the loop.
I need to output the price of my product and SKU in the header of my theme.
Many thanks in advance
You have to create a $product object for a particular product product using it's product ID.
$product_id = 99;
$product = wc_get_product( $product_id );
echo $product->get_price_html();
Related
I need help:)
I want to make a paylater message on my product page. For example, I have a product that costs $55, and under it I want to put the "Pay in 4 interest-free payments of $13.75 with us" message which $13.75 is calculated automatically.
I've already found the code (shortcode) because I want the output in the form of a shortcode.
add_shortcode( 'product_price', 'display_product_price' );
function display_product_price( $atts ){
$atts = shortcode_atts( array(
'id' => get_the_id(),
), $atts, 'product_price' );
global $product;
if ( ! is_a( $product, 'WC_Product') )
$product = wc_get_product($atts['id']);
return $product->get_price();
}
But I don't know how to add the formula in that code, and that code doesn't show the currency symbol. Please help me to add a formula to that code, and bring up the currency symbol($).
I would be very grateful for any help :)
I think this case allready solved, I'll answer maybe someone will need something like this...
I found this guy's code praveen from his answer on this question.
This is my modification code to fit in my case
add_shortcode( 'price_calculated', 'display_price_calculated' );
function display_price_calculated( $atts ){ //create shortcode
global $product;
$id = $product->get_id(); //get current product id
$product = wc_get_product( $id );
$a=$product->get_price(); //get current product price
$b = 4; //because I want my price to be divided in 4
$total = $a/$b; //calculated formula
return get_woocommerce_currency_symbol().$total; //Show the price result, and add a currency symbol before price result
}
Now I just need to add this shortcode [price_calculated]
on my page
That's it, thanks stack StackOverflow!
Short version: How do I get upsell for variable product in Woocommerce?
Longer version:
I need the product ids for upsells.
My old code contains depracated code:
$upsells = $product->get_upsells(); // $product is instace of WC_Product_Variable::
The call should be the following:
$upsells = $product->get_upsells_ids(); // $product is WC_Product::
But a different class.
I tried to get the parent instance using wc_get_product($product->get_parent_id()) - but fail.
So, given the instance of WC_Product_Variable how do I get to the parent method WC_Product::$product->get_upsells_ids() ??
Thanks
The get_upsells-ids() method does not exist. Try get_upsell_ids().
The following code will show you all the upsell ids for the product you are currently visiting:
// quick test to check upsell ids
add_action( 'woocommerce_before_single_product', 'echo_upsell_ids' );
function echo_upsell_ids() {
global $product;
$product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();
$product = wc_get_product( $product_id );
$upsell_ids = $product->get_upsell_ids();
echo '<pre>' . print_r( $upsell_ids, true ) . '</pre>';
}
I have tested the code and it works. The snippet goes into your child theme's functions.php file.
I have a Woocommerce variable product. I need to get the attribute (pa_brand) of the parent of that variation? Here is the code I have which is not returning anything. How can I get a product variation from a product id?
global $product;
global $post;
$post_id = $post->ID;
$parent_id = wp_get_post_parent_id( $post_id );
$brand = $product->get_attribute( 'pa_brand' );
#Rob Gelhausen already answered it as a comment for the question. To get more notice, I'm making it as an answer.
To get Main product ID / Parent product from variation product id, we can use below code.
$variation = wc_get_product($variation_id);
$product = wc_get_product( $variation->get_parent_id() );
To get attribute, we can use below code.
$brand = $product->get_attribute( 'pa_brand' );
Have Woocommerce setup with a range of variable products. In the variable tab I've setup a unique price, image, description, weight & dimensions for each item.
All variable data displays as expected on the front-end except the dimensions & weight.
Despite hours of searching, I cannot find any documentation, tutorials, hints on how to hook into it.
Have Woocommerce templates setup and know that I will need to hook into the do_action( 'woocommerce_single_variation' ); in variable.php.
Anyone know how to get each variable's dimensions & weight to display beneath the variable description?
If you have the variation ID, you can use it to create a new WC_Product(). This object will then have properties available on it for the $length, $width, and $height. See the docs here (at the bottom under "Magic Properties").
To get the variations for a given product, you can use the global $product and then the get_available_variations() function.
global $product
$variations = $product->get_available_variations();
foreach ( $variations as $variable_array ){
$variation = new WC_Product( $variable_array['variation_id'] );
echo "The length is {$variation->length}.";
}
If you want to display additional information regarding your variable product add this function to your child theme’s function.php (or plugin). You’ll probably want to alter the html tags to fit your theme:
add_filter( 'woocommerce_product_additional_information', 'tim_additional_tab', 9 );
function tim_additional_tab( $product ){
$variations = $product->get_available_variations();
//print the whole array in additional tab and examine it
//echo '<pre>';
//print_r($variations);
//echo '</pre>';
//html and style to your likings
foreach ( $variations as $key ){
echo $key['image']['title'].'<br>';
echo $key['weight_html'].'<br>';
echo $key['dimensions_html'].'<br>';
}
}
I am trying to get some product prices to show up in one custom template. Is there, perhaps, an integrated woocommerce function that given ID will give me products price?
Something along the lines of: woocommerce_productprice(423)
Basically, I have a custom page template as a catalog and I want to hook product prices up to that template, so whenever something changes, it automatically changes the price without me having to change the custom template.
Help please!
Updated for WooCommerce 2.3:
$product = we_get_product( $post_id );
echo $product->get_price();
$product = wc_get_product( $product_id );
$price = $product->get_price_html();
woocommerce 3.x, it working!
Here try this
$product = new WC_Product( $post_id );
$price = $product->price;
echo $price;
Actually, there's a typo on the above code, see below:
$product = wc_get_product( $post_id );
echo $product->get_price();