WooCommerce: Hooking up product price in custom template - wordpress

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();

Related

Wordpress add action on product publish

I want to add an action when a new product is created. Many people recommend save_post_product but the problem is that when i use it it runs only when i go to products and click add new which is completely useless because $post returns an object with empty properties. When i publish my product it doesn't do anything. Am i doing something wrong? Below is the code that i used
add_action('save_post_product', 'mp_sync_on_product_save', 10, 3);
function mp_sync_on_product_save( $post_id, $post, $update ) {
$product = wc_get_product( $post_id );
echo "<script>console.log('" . $product . "');</script>";
// do something with this product
}

Woocommerce Updating products price with php does not trigger price change notification

I was playing around with php code where i change price for a certain product. Also i am using a plugin that notify's by email once price fells below some amount. The product price updates with no issues also appears with new price in the product list view, but sadly does not trigger the notification plugin. The strange thing is when i go to product edit and click update i immediately get the notification email.
The php code i am using currently:
$ppt = $value / $divider;
$product = wc_get_product($product_id);
print_r($product);
// Mark product as updated
update_post_meta($product_id, '_price', $ppt );
update_post_meta($product_id, '_regular_price', $ppt );
update_post_meta($product_id, '_sync_updated', true );
$product->save();
}
wp_reset_query();
Thank you in advance!
You should use WC_Product setter instead update_post_meta.
Try this (not tested) :
$ppt = $value / $divider;
$product = new WC_Product( $product_id );
// Mark product as updated
$product->set_price( $ppt );
$product->set_regular_price( $ppt );
$product->save();
}
wp_reset_query();

How do I get upsell for variable product in Woocommerce?

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.

Get Woocommerce Variation Parent Attribute

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' );

Using Woocommerce get_price() in header

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();

Resources