PHP Woocommerce get product brand name - php

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

Related

Issue in Wordpress programmatically created product filter on shop page

I am creating products in WordPress programmatically. Everything seems ok but the shop page filter by attribute name is not working with these products.
If we open these products in wp-admin and save attributes then it is working.
I am not sure what is the issue.
function addAttributes($product_id, $attributes){
$product_attributes = array();
foreach( $attributes as $key => $terms ){
$taxonomy = wc_attribute_taxonomy_name($key); // The taxonomy slug
$attr_label = ucfirst($key); // attribute label name
$attr_name = ( wc_sanitize_taxonomy_name($key)); // attribute slug
// NEW Attributes: Register and save them
if( ! taxonomy_exists( $taxonomy ) ){
$crRes = save_product_attribute_from_name( $attr_name, $attr_label );
}
$product_attributes[$taxonomy] = array (
'name' => $taxonomy,
'value' => '',
'position' => '',
'is_visible' => 1,
'is_variation' => 0,
'is_taxonomy' => 1
);
//foreach( $terms as $value ){
echo "value >>>"; print_r($terms);
if(empty($terms)) continue;
$term_name = ucfirst($terms);
$term_slug = sanitize_title($terms);
// Check if the Term name exist and if not we create it.
if( ! term_exists( $terms, $taxonomy ) )
wp_insert_term( $term_name, $taxonomy, array('slug' => $term_slug ) ); // Create the term
// Set attribute values
echo "product_id ".$product_id." ; term_name ".$term_name." ; taxonomy ".$taxonomy;
$upRes = wp_set_post_terms( $product_id, $term_name, $taxonomy, true );
echo "upRes";
}
update_post_meta( $product_id, '_product_attributes', $product_attributes );
$product = new WC_Product_Variable( $product_id );
$product->save();
}

Woocommerce How to Show Related Categories

Good Day, I have the following Code Snippet to help me add custom columns to a plugin the first bit works perfectly but now I would like to add Related Categories to the content (All of this works only in the admin panel) so no shortcode will work
// ADD CUSTOM COLUMN
if( !function_exists('yith_wcbep_add_columns') ) {
function yith_wcbep_add_columns( $columns ) {
$columns['new_column'] = 'Related Categories';
return $columns;
}
add_filter( 'yith_wcbep_default_columns', 'yith_wcbep_add_columns', 99 );
}
this snipped added content to the columns
// ADD CONTECT TO CUSTOM COLUMN
if( !function_exists('yith_wcbep_manage_custom_columns_add_columns_to_new_column') ) {
function yith_wcbep_manage_custom_columns_add_columns_to_new_column( $value, $column_name, $post ) {
if ( 'new_column' == $column_name ) {
$value = 'Custom value'; // This shows the value as Custom value
}
return $value;
}
add_filter( 'yith_wcbep_manage_custom_columns', 'yith_wcbep_manage_custom_columns_add_columns_to_new_column', 10, 3 );
}
as soon as I add this code everything disappears from the columns, this code goes where the Custom value is at
$terms = get_the_terms($product->ID, 'product_cat');
foreach ($terms as $term) {
echo $product_cat = $term->name.', ';
}
UPDATE
if( !function_exists('yith_wcbep_manage_custom_columns_add_columns_to_new_column') ) {
function yith_wcbep_manage_custom_columns_add_columns_to_new_column( $value, $column_name, $post ) {
if ( 'also_column' == $column_name ) {
$prod_cat_args = array(
'taxonomy' => 'product_cat', //woocommerce
'orderby' => 'name',
'empty' => 0
);
//$woo_categories = get_categories( $prod_cat_args );
$woo_categories = get_the_terms( $post->ID, 'product_cat' );
foreach ( $woo_categories as $woo_cat ) {
$woo_cat_id = $woo_cat->term_id; //category ID
$woo_cat_name = $woo_cat->name; //category name
$my_query = "SELECT meta_value FROM wp_termmeta WHERE term_id='$woo_cat_id' AND meta_key=\'crosssell\'";
foreach ($my_query as $row) {
//$value = 'Test - '.$woo_cat->term_id.' - '.$woo_cat->name;
$value = 'Test - '.$row->meta_value;
}
}
}
return $value;
}
but the result keeps being blank, what I'm I doing wrong? if I take out the $my_query section it shows the category and ID but I need to get the CROSSSELL CATEGORY
UPDATED 2 This is the Whole Code
// ADD CUSTOM COLUMN
if( !function_exists('yith_wcbep_add_columns') ) {
function yith_wcbep_add_columns( $columns ) {
$columns['also_column'] = 'Also See...';
return $columns;
}
add_filter( 'yith_wcbep_default_columns', 'yith_wcbep_add_columns', 99 );
}
// ADD CONTECT TO CUSTOM COLUMN
if( !function_exists('yith_wcbep_manage_custom_columns_add_columns_to_new_column') ) {
function yith_wcbep_manage_custom_columns_add_columns_to_new_column( $value, $column_name, $post ) {
if ( 'also_column' == $column_name ) {
$prod_cat_args = array(
'taxonomy' => 'product_cat', //woocommerce
'orderby' => 'name',
'empty' => 0
);
//$woo_categories = get_categories( $prod_cat_args );
$woo_categories = get_the_terms( $post->ID, 'product_cat' );
foreach ( $woo_categories as $woo_cat ) {
$woo_cat_id = $woo_cat->term_id; //category ID
$woo_cat_name = $woo_cat->name; //category name
$my_query = "SELECT meta_value FROM wp_termmeta WHERE term_id='$woo_cat_id' AND meta_key=\'crosssell\'";
foreach ($my_query as $row) {
//$value = 'Test - '.$woo_cat->term_id.' - '.$woo_cat->name;
$value = 'Test - '.$row->meta_value;
}
}
}
return $value;
}
add_filter( 'yith_wcbep_manage_custom_columns', 'yith_wcbep_manage_custom_columns_add_columns_to_new_column', 10, 3 );
}
You can try with the following.
$terms = get_the_terms($product->ID, 'product_cat');
$categories = '';
foreach ($terms as $term) {
$categories .= $product_cat = $term->name.', ';
}
return $categories;
But sill there might some question, in function you used post as parameter, but you use product in this.
after very a long time I got this to work
**$product = wc_get_product($post);
$categories = strip_tags(get_the_term_list( $product->get_id(), 'product_cat', '', ',', '' ));
/*End*/
$value = $categories;**
it shows the categories as it should I added strip_tags to remove the links so now only text remains.
But Now I ran into another problem I cannot get crosssell to show up even if it is there
foreach((get_term_meta( $post->ID, 'crosssell' )) as $category) {
$cat_name = $category->name . '</br>';
$cat_id = $category->term_id . '</br>';
$crosssell = get_term_meta( $cat_id, 'crosssell', true );
}
can display $cat_name and $cat_id no problem but the $crosssell does not display at all

Woocommerce product brand in productbox

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':

Get WooCommerce specific product attribute terms sorted by "menu order"

I want to sort get_the_terms by menu_order of the curent product and till now i have this code:
$colors='';
$terms = get_the_terms( get_the_ID(), 'pa_colors' );
foreach ( $terms as $term ) {
$colors='<pre>'.$term->name.'</pre>';
}
echo $colors;
There is 2 ways to get the product attribute term names sorted by menu order (for a defined product):
1). Using wp_get_post_terms() function (WordPress way)
The WordPress function get_the_terms() doesn't allow to alter the WP_Term_Query…
So instead you will use similar wp_get_post_terms() that allows WP_Term_Query tuning.
$taxonomy = 'pa_color'; // The taxonomy
$query_args = array(
'fields' => 'names',
'orderby' => 'meta_value_num',
'meta_query' => array( array(
'key' => 'order_' . $taxonomy,
'type' => 'NUMERIC'
) )
);
$term_names = wp_get_post_terms( get_the_ID(), $taxonomy, $query_args );
if ( ! empty( $term_names ) ) {
// Output
echo '<pre>' . implode( '</pre><pre>', $term_names ) . '</pre>';
}
2). Simply using WC_Product method get_attribute() (WooCommerce way)
$product = wc_get_product( get_the_ID() ); // The WC_product Object
$taxonomy = 'pa_color'; // The taxonomy
$names_string = $product->get_attribute('color');
if ( ! empty( $names_string ) ) {
$names_array = explode( ', ', $names_string ); // Converting the string to an array of term names
// Output
echo '<pre>' . implode( '</pre><pre>', $names_array ) . '</pre>';
}
Both ways work.

Automatically assign products to categories based on their product tags in Woocommerce

I currently have a function which successfully adds products to Woocommerce using wp_insert_post().
I'm now trying to assign products to relevant categories based on their product tags. For example, if the product is added with the product tags 'ring' or 'necklace' then it is auto assigned to the 'Jewellery' category.
Using the following function, I'm able to achieve the correct functionality on posts, but have had no luck in attempting to make this work for the products post type used in woocommerce.
Works for posts:
function auto_add_category ($post_id = 0) {
if (!$post_id) return;
$tag_categories = array (
'ring' => 'Jewellery',
'necklace' => 'Jewellery',
'dress' => 'Clothing',
);
$post_tags = get_the_tags($post_id);
foreach ($post_tags as $tag) {
if ($tag_categories[$tag->name] ) {
$cat_id = get_cat_ID($tag_categories[$tag->name]);
if ($cat_id) {
$result = wp_set_post_terms( $post_id, $tags = $cat_id, $taxonomy = 'category', $append = true );
}
}
}
}
add_action('publish_post','auto_add_category');
I've tried to repurpose the code to work for products as follows:
function auto_add_category ($product_id = 0) {
if (!$product_id) return;
$tag_categories = array (
'ring' => 'Jewellery'
'necklace' => 'Jewellery',
'dress' => 'Clothing',
);
$product_tags = get_terms( array( 'taxonomy' => 'product_tag') );
foreach ($product_tags as $tag) {
if ($tag_categories[$tag->name] ) {
$cat = get_term_by( 'name', $tag_categories[$tag->name], 'product_cat' );
$cat_id = $cat->term_id;
if ($cat_id) {
$result = wp_set_post_terms( $product_id, $tags = $cat_id, $taxonomy = 'product_cat', $append = true );
}
}
}
}
add_action('publish_product','auto_add_category');
However, it doesn't assign the relevant categories upon product creation. Any assistance would be greatly appreciated by this novice coder muddling his way through wordpress!
Try this code:
function auto_add_category ($product_id = 0) {
if (!$product_id) return;
// because we use save_post action, let's check post type here
$post_type = get_post_type($post_id);
if ( "product" != $post_type ) return;
$tag_categories = array (
'ring' => 'Jewellery'
'necklace' => 'Jewellery',
'dress' => 'Clothing',
);
// get_terms returns ALL terms, so we have to add object_ids param to get terms to a specific product
$product_tags = get_terms( array( 'taxonomy' => 'product_tag', 'object_ids' => $product_id ) );
foreach ($product_tags as $term) {
if ($tag_categories[$term->slug] ) {
$cat = get_term_by( 'name', $tag_categories[$term->slug], 'product_cat' );
$cat_id = $cat->term_id;
if ($cat_id) {
$result = wp_set_post_terms( $product_id, $cat_id, 'product_cat', true );
}
}
}
}
add_action('save_post','auto_add_category');

Categories