Woocommerce product brand in productbox - php

I have a woocommerce store and I want to display the product brand in the product box. I have read many topics on stack overflow and tried many suggested solutions as you can see here:
echo "<strong>BRAND: ";
/*foreach ( $brands as $key => $brand ) :
echo $brand->name;
endforeach;
echo "</strong><br/>";*/
$product_id = get_the_id();
$product = wc_get_product( $product_id );
$taxonomy = `product_tag`;
$brand_names = wp_get_post_terms( $product_id, $taxonomy, array( 'fields' => 'names' ) );
// Get the brand name
$brand_name = reset( $brand_names );
echo $brand_name;
echo "</strong><br/>";
In my database in the wp_term_taxonomy table one of my brands get the taxonomy as 'product_tag':

Related

Woocommerce get category id from product page

I am trying to create custom dynamic menu for Woocommerce, and it seem I am stuck.
To mark my categories as active, I need category ID of a page that I am in at the moment.
If I am in a category I get id like this:
if (is_product_category()) {
global $wp_query;
$curent_Cat_ID = $wp_query->get_queried_object()->term_id;
}
But it wont show anything on a product page.
Is there a way to get category id on a product page? What I was able to find wasn't working anymore.
Or somehow get an array like breadcrumbs with IDs?
Ok a good night sleep and eventually I found the answer. If someone will need it here it is:
if ( is_tax( 'product_cat' ) ) {
$current_cat = get_queried_object_id();
$cat_ancestors = get_ancestors( $current_cat, 'product_cat' );
} elseif ( is_singular( 'product' ) ) {
$terms = wc_get_product_terms(
get_queried_object_id(),
'product_cat',
apply_filters(
'woocommerce_product_categories_widget_product_terms_args',
array(
'orderby' => 'parent',
'order' => 'DESC',
)
)
);
if ( $terms ) {
$main_term = apply_filters( 'woocommerce_product_categories_widget_main_term', $terms[0], $terms );
$current_cat = $main_term->term_id;
$cat_ancestors = get_ancestors( $main_term->term_id, 'product_cat' );
}
}

Can't choose variations for WooCommerce product created programmatically

I am using a custom PHP Script to create variations of a WooCommerce product. The user chooses "Size" and "Color" of the product through a select field, and the variations is created based on that.
Works fine, product is created with the correct price, attribute and variations. Except that you cant select the actual variations when trying to shop (they are "disabled").
When I go back-end in WooCommerce and open the product there, and hit the blue "Update"-button on the product, it works as intended and the variations can be selected and bought front-end.
Is there any function to "force update" the product in PHP after variations have been created?
--
Below the code which adds color and size as attributes to the product. Also creates a single variation with "Any color" and "Any size" as the rules.
function save_wc_custom_attributes($post_id, $custom_attributes) {
$i = 0;
$product = wc_get_product( $post_id );
$product_id = $post_id;
$terms = wp_get_post_terms( $post_id, 'pa_farge') ;
// Remove existing colors from the product
foreach ( $terms as $term ) {
wp_remove_object_terms( $post_id, $term->term_id, 'pa_farge' );
}
$terms = wp_get_post_terms( $post_id, 'pa_str') ;
// Remove existing sizes from the product
foreach ( $terms as $term ) {
wp_remove_object_terms( $post_id, $term->term_id, 'pa_str' );
}
foreach ($custom_attributes as $name => $value) {
// Relate post to a custom attribute, add term if it does not exist
wp_set_object_terms($post_id, $value, $name, true);
// Create product attributes array
$product_attributes[$i] = array(
'name' => $name, // set attribute name
'value' => $value, // set attribute value
'is_visible' => 1,
'is_variation' => 1,
'is_taxonomy' => 1
);
$i++;
}
// Now update the post with its new attributes
update_post_meta($post_id, '_product_attributes', $product_attributes);
// Details for the variation
$variation_post = array(
'post_title' => $product->get_name(),
'post_name' => 'product-'.$product_id.'-variation',
'post_status' => 'publish',
'post_parent' => $product_id,
'post_type' => 'product_variation',
'guid' => $product->get_permalink()
);
if( !$product->has_child() ) {
// Creating a single variation if none exists
$variation_id = wp_insert_post( $variation_post );
$variation = new WC_Product_Variation( $variation_id );
$variation->set_regular_price( $product->get_price() );
$variation->save(); // Save the data
$product_variable = new WC_Product_Variable($post_id);
$product_variable->sync($post_id);
wc_delete_product_transients($post_id);
}
else {
// Update the variation if it already exists
$product_variable = new WC_Product_Variable($post_id);
$product_variable->sync($post_id);
wc_delete_product_transients($post_id);
}
$product->save();
}
Your actual code is jus setting product attributes to the main variable product, but it doesn't set any attribute to a product variation… So the question is may be related to this variable product instead…
So try simply to add at the end of your code:
$product = wc_get_product( $post_id );
$product->save();

PHP Woocommerce get product brand name

I create plugin and want to get brand name from woocommerce.
In fist time i post product no.1 the plugin it's work and get product brand as i wish but when i create product post no.2 and no.3 from brand name get from product no.1
whats wrong ?
if ( class_exists( 'WooCommerce' ) ) {
$kode = $product->get_sku();
$terms = wp_get_post_terms( $product_id, 'brand', array('orderby'=>'name'));
$brands = get_terms( 'brand', array(
'orderby' => 'name'
//,'product__in' => $product->id
// orderby arguments ('name', 'slug','term_group', 'term_id', 'id', 'description')
) );
foreach ( $brands as $key => $brand ) :
$brand_name = $brand->name;
endforeach;
$merk = $brand_name;
//this i want to print the brand name
echo "<strong>STOCK ".$merk." ".$kode."</strong><br/>";
} else {
echo "please active your WooCommerce plugin.";
}
this should work for you.
if ( class_exists( 'WooCommerce' ) ) {
$kode = $product->get_sku();
$tax = array(
'STOCK'=>'brand',
);
foreach ($tax as $mk_label => $mk_value) {
$make_array = wp_get_post_terms(get_the_ID(), $mk_value, array("fields" => "names"));
foreach ($make_array as $value) {
$mk .= $value.", ";
}
echo '<strong>'.$mk_label.' '.rtrim($mk,", ").' '.$kode.'</strong>';
$mk ="";
}
} else {
echo "please active your WooCommerce plugin.";
}
We are taking terms dynamically based on current product id and fetching the brand accordingly. Let me know if you need any more help.
Thanks
Updated code:
foreach ( $brands as $key => $brand ) :
echo "<strong>STOCK ".$brand->name." ".$kode."</strong><br/>";
endforeach;
The echo must be inside the foreach loop.
If you want get terms from product, you should try this:
$terms = wp_get_post_terms( $product_id, 'brand', array('orderby'=>'name'));
//you could use: var_dump($terms); to see what it is.
If you want to concat your brand names, you could do this:
$term_names = wp_get_post_terms( $product_id, 'brand', array('orderby'=>'name', 'fields' => 'names'));
$term_names = implode(' ', $term_names);
echo $term_names;
Instead of taxonomy "brand" it should be "pa_brand".
$term_names = wp_get_post_terms( $product_id, 'pa_brand', array('orderby'=>'name', 'fields' => 'names'));

Get custom taxonomy list for products post type by other taxonomy

I have this situation that I'm trying to figure out.
I've created a custom product taxonomy named cities, each product can belong to a product_cat taxonomy and/or city taxonomy.
On a shop product category I want to create a widget that displays available cities for current category.
I've managed to do this but it's not a optimized code that I'm looking for.
right now this is how it looks like
// get current category
$cate = get_queried_object();
$catID = $cate->term_id;
$catID;
$link = get_term_link( $catID, 'product_cat' );
$args = array(
'post_type' => 'product',
'tax_query' => array(
'taxonomy' => 'product_cat',
'field' => 'id',
'term' => $catID
)
);
$products = new WP_Query( $args );
$cities = array();
foreach ($products->posts as $product) {
echo('<ul>');
$terms = get_the_terms($product->ID,'city');
if (is_array($terms)) {
foreach ($terms as $term) {
if (!in_array($term->slug, $cities)) {
$cities[] = $term->slug;
$city_link = get_term_link( $term->term_id, 'city' );
echo "<li><a href='{$link}city/{$term->slug}'>{$term->name}</a></li>";
}
}
}
echo('</ul>');
}
This if not by the WP_Query function I think it could be done by a SQL query but this is out of my league.

How to get Woocommerce Current Variation ID?

I am trying to create some additional functionality on my Woocommerce backend where I add a custom field to the variations section of backend of Woocommerce. However I cannot for the life of me figure out how to get the current variation id so I can get the post meta.
here is what I have been working with:
<?php
// Get variations
$args = array(
'post_type' => 'product_variation',
'post_status' => array( 'private', 'publish' ),
'numberposts' => -1,
'orderby' => 'menu_order',
'order' => 'asc',
'post_parent' => $post->ID
);
$variations = get_posts( $args );
foreach ( $variations as $variation ) {
$variation_id = absint( $variation->ID );$variable_id = $this['variation_id'];
$variation_post_status = esc_attr( $variation->post_status );
$variation_data = get_post_meta( $variation_id );
$variation_data['variation_post_id'] = $variation_id;
echo get_post_meta( $variation_data['variation_post_id'], '_my_custom_field', true) . ' - TEST';
}
?>
When I check the backend it looks like it is pulling all the post meta into each variation like this:
However if I use the actual variation id like the below then it works for that variation:
echo get_post_meta( 134, '_my_custom_field', true) . ' - Test Variation #134';
#Derek,
Where in your template are you trying to get the custom field data to display?
If it is within the product loop, you can do this:
<?php
// Get the post IDs of all the product variations
$variation_ids = $product->children;
// Each product variation id
foreach( $variation_ids as $var_id ) :
// Get all of the custom field data in an array
$all_cfs = get_post_custom($var_id);
// Show all of the custom fields
var_dump($all_cfs);
// Get specific data from the certain custom fields using get_post_meta( $post_id, $key, $single );
$test = get_post_meta( $var_id, '_text_field', true );
endforeach;
?>
You in your foreach loop you should be able to access it via:
$variation->variation_id or $variation['variation_id']
This would look like:
foreach ( $variations as $variation ) {
$variation_id = $variation->variation_id );
}
You may also be able to use the get_the_ID() function

Categories