Get Woocommerce Variation Parent Attribute - wordpress

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

Related

How to calculated price in woocommerce

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!

Add suffix text to specific product category in Woocommerce [duplicate]

I need to add 'per metre' to the price on most of my online catalogue, I tried the code on this thread in my finctions.php but I cannot get it to omit/include particular categories- it seems to be all or nothing. What am I doing wrong?
I have edited the code as such:
/*add 'per metre' after selected items*/
add_filter( 'woocommerce_get_price_html', 'conditional_price_suffix', 20, 2 );
function conditional_price_suffix( $price, $product ) {
// HERE define your product categories (can be IDs, slugs or names)
$product_categories = array('fabric','haberdashery', 'lining',);
if( ! has_term( $product_categories, 'fasteners', 'patches', 'remnnants', $product->get_id() ) )
$price .= ' ' . __('per metre');
return $price;
}
I want 'fabrics', 'haberdashery', 'lining' to show per metre, and 'fasteners', 'patches', 'remnants' to NOT show the suffix.
I have tried variations of the code -my exclusions in the top bit and the inclusions in the second part, and with/without the "( ! has term" section, but whichever I do takes all the suffix messages away, or applies to all categories.
It would be amazing if I could get this to work as have previously been using a very bloated plug-in. I'm only basically capable in this stuff so please feel free to talk me through it as if I am an idiot.
There is a little mistake in your code in the has_term() function.
To handle parent product categories, we will use a custom conditional function instead of has_tem().
I have also added some code to handle the product variation selected price of variable products, So try this instead:
// Custom conditional function that handle parent product categories too
function has_product_categories( $categories, $product_id = 0 ) {
$parent_term_ids = $categories_ids = array(); // Initializing
$taxonomy = 'product_cat';
$product_id = $product_id == 0 ? get_the_id() : $product_id;
if( is_string( $categories ) ) {
$categories = (array) $categories; // Convert string to array
}
// Convert categories term names and slugs to categories term ids
foreach ( $categories as $category ){
$result = (array) term_exists( $category, $taxonomy );
if ( ! empty( $result ) ) {
$categories_ids[] = reset($result);
}
}
// Loop through the current product category terms to get only parent main category term
foreach( get_the_terms( $product_id, $taxonomy ) as $term ){
if( $term->parent > 0 ){
$parent_term_ids[] = $term->parent; // Set the parent product category
$parent_term_ids[] = $term->term_id; // (and the child)
} else {
$parent_term_ids[] = $term->term_id; // It is the Main category term and we set it.
}
}
return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true : false;
}
add_filter( 'woocommerce_get_price_html', 'conditional_price_suffix', 10, 2 );
function conditional_price_suffix( $price, $product ) {
// Handling product variations
$product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();
// HERE define your product categories (can be IDs, slugs or names)
$product_categories = array('fabric','haberdashery', 'lining');
if( has_product_categories( $product_categories, $product_id ) )
$price .= ' ' . __('per metre');
return $price;
}
Code goes in function.php file of your active child theme (or active theme). tested and works.

wc_price($price) not showing the discount by FILTER "woocommerce_product_get_regular_price"

With WooCommerce, I have the following function that allow me to make a discount on my products prices:
add_filter('woocommerce_product_get_regular_price', 'custom_price' , 99, 2 );
function custom_price( $price, $product )
{
$price = $price - 2;
return $price
}
This is working everywhere (in the shop, in the cart, in the backend), but not in my custom product list plugin:
add_action( 'woocommerce_account_nybeorderlist_endpoint', 'patrickorderlist_my_account_endpoint_content' );
function patrickorderlist_my_account_endpoint_content() {
//All WP_Query
echo wc_price($price);
}
This shows the regular price without the discount. Both pieces of code are in the same plugin.
For info, wc_price() is just a formatting function used to format prices and has nothing to do with the $price itself main argument. Your problem is that in your 2nd function, the variable $price certainly doesn't use the WC_Product method get_regular_price(), which is required in your case… So in your WP_Query loop, you need to get the WC_Product object instance, then from this object get the price using the method get_regular_price()…
So try something like (it's an example as you don't provide your WP_Query in your question):
add_action( 'woocommerce_account_nybeorderlist_endpoint', 'rm_orderlist_my_account_endpoint_content' );
function rm_orderlist_my_account_endpoint_content() {
$products = new WP_Query( $args ); // $args variable is your arguments array for this query
if ( $products->have_posts() ) :
while ( $products->have_posts() ) : $products->the_post();
// Get the WC_Product Object instance
$product = wc_get_product( get_the_id() );
// Get the regular price from the WC_Product Object
$price = $product->get_regular_price();
// Output the product formatted price
echo wc_price($price);
endwhile;
wp_reset_postdata();
endif;
}
Now it should work as expected.

Woocommerce : Why do I get parrent category of a product and not current child category?

I want to make a discount category. But on some categories I get parent category id and not the child id.
I tried getting term_id of product cat like this :
$terms = wp_get_post_terms( $product->get_id(), 'product_cat' );
$term_id = $terms[0]->term_id;
and this version is not a good solution for me:
$category = get_queried_object();
$term_id = $category->term_id;
So far I'm stuck at this:
function category_percentage($product)
{
$terms = wp_get_post_terms( $product->get_id(), 'product_cat' );
$term_id = $terms[0]->term_id; // I belive here is the problem
$discount_category = get_term_meta($term_id, 'discount_category', true);
return $discount_category;
}
add_filter( 'woocommerce_product_get_price', 'custom_sale_price_for_category', 10, 2 );
function custom_sale_price_for_category( $price, $product )
{
if(category_percentage($product)<=0) return $price;
$price *= ( 1 - category_percentage($product)/100 );
return $price;
}
in category_percentage($product) function I want to get the current category of the product, but for some reason in some categories I get the parrent category, and yes, I do have the parent category checked for the product as well. But I have it like that for all the products and still get different results when I recive the current cat id.

WooCommerce: Hooking up product price in custom template

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

Resources