WooCommerce front-end product creation - php

Ok I'll try and ask this in a way that will not confuse everyone with my confusion
I am trying to make a front-end form for WooCommerce so customers can make a product from the front-end.
I have all the other parts I want to implement so far working ie. Title, content and price
all posted in the correct post type. But can't get it to post to a product category
WooCommerce categories is a custom-Taxonomy named product_cat <--almost 100% sure.
product_cat children are Terms, I have three --> Base, Designs and User Designs
I want all posts from this form to automatically be under User Designs. This is what I have now
$post_information = array(
'post_title' => esc_attr(strip_tags($_POST['postTitle'])),
'post_content' => esc_attr(strip_tags($_POST['postContent'])),
'post_type' => 'product',
'post_status' => 'publish',
);
$post_id = wp_insert_post($post_information);
update_post_meta( $post_id, '_regular_price', strip_tags($_POST['_regular_price'] ) ) ;
if($post_id)
{
wp_redirect(home_url());
exit;
}
};
I have tried many variations to set terms like
wp_set_object_terms($post_id, 'designs', 'product_cat', false);
wp_set_post_terms($post_id, 'designs', 'product_cat', false);
wp_set_object_terms($post_id, $designs ,'product_cat');
wp_set_post_terms( $post_id, $designs, 'product_cat', true );
wp_set_object_terms($post_id, $base, 'product_cat');
No go, I know I'm missing something but I have no idea what. I've been looking at my screen so long my eyes are crossing,
Any help will be greatly appreciated thanks in advance

Use the following:
wp_set_post_terms( $product_id, '343', 'product_cat');
Where $product_id is the post_id of the product you're making, and 343 is the taxonomy id (you can get it from one of the taxonomy tables)

Related

Woocommerce show only categories that have products with certain meta value

I am developing b2b section in my Woocommerce shop. I have managed to filter woocommerce_product_query_meta_query to display only products that have been enabled for b2b section for b2b users.
However I cannot find a way to hide product categories that shows 0 results (because no product enabled for b2b section is in that category) in Woocommerce category widget.
I was thinking about overriding default Woocommerce widget code and for each category (and subcategory) run wp query that returns number of products in this category that are enabled for b2b. But with large number of products and categories it seems very inefficient.
Is there a way to hide categories from Woocommerce category widget that are "empty" (no product in this category is enabled for b2b)?
Thanks for any suggestions.
Edit
To clarify my question: here is the function I use to filter product query to display only products that have _eda_display_in_b2b meta set to yes:
function show_only_b2b_products( $meta_query, $query ) {
if ( is_admin() || ! is_user_logged_in() || ! is_b2b_user() ) {
return $meta_query;
}
$meta_query[] = array(
'key' => '_eda_display_in_b2b',
'value' => 'yes',
'compare' => '='
);
return $meta_query;
}
add_filter( 'woocommerce_product_query_meta_query', 'show_only_b2b_products', 10, 2 );
Exaple:
https://klon.vozikyprozivot.cz/kategorie-produktu/pridavne-pohony/
This category is not empty for regular customers and not logged in users. But for b2b customers there is no product to show. So I need to hide this category from the widget for b2b customers.
If you are referring to the product categories widget, there is a setting for hiding empty categories:
If you are referring to something different, could you please share an example page URL as well as your site’s System Status? You can find it via WooCommerce > Status. Select “Get system report” and then “Copy for support”. Once you’ve done that, paste it here in your response.
Hope this will help you.
======Edit======
I think for the above issue you can use the wc category hook and remove the category. Please check the below code
//* Used when the widget is displayed as a dropdown
add_filter( 'woocommerce_product_categories_widget_dropdown_args', 'rv_exclude_wc_widget_categories' );
//* Used when the widget is displayed as a list
add_filter( 'woocommerce_product_categories_widget_args', 'rv_exclude_wc_widget_categories' );
function rv_exclude_wc_widget_categories( $cat_args ) {
//add the logic to check the category have product or not and make the array of ID and replace the below array with that.
$cat_args['exclude'] = array('55','68'); // Insert the product category IDs you wish to exclude
return $cat_args;
}
In the above code I think you can make logic and check if the category have the products or not and make the array of id for the non-product category.
In this way you can exclude the category from the list and dropdown.
How this will help you.
With a big help from Harshit Vaid I have managed to solve it:
add_filter( 'woocommerce_product_categories_widget_dropdown_args', 'eda_exclude_wc_widget_categories' );
add_filter( 'woocommerce_product_categories_widget_args', 'eda_exclude_wc_widget_categories' );
function eda_exclude_wc_widget_categories( $cat_args ) {
$args = array(
'taxonomy' => 'product_cat',
'hide_empty' => 0
);
$all_categories = get_categories( $args );
$category_exclude_list = array();
foreach ( $all_categories as $cat ) {
if ( $cat->category_parent == 0 ) {
$category_id = $cat->term_id;
$product_args = array(
'posts_per_page' => - 1,
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => $category_id,
'field' => 'term_id',
'operator' => 'IN'
)
),
'meta_query' => array(
array(
'key' => '_eda_display_in_b2b',
'value' => 'yes'
)
)
);
$query = new WP_Query( $product_args );
$count = $query->post_count;
if ( $count == 0 ) {
array_push( $category_exclude_list, $category_id );
}
}
}
$cat_args['exclude'] = $category_exclude_list;
return $cat_args;
}

How to add product to category and term in brand taxonomy Wordpress

Iam using woocommerce Plugin and i created a taxonomy called "brand"
I am creating products manually and i need to set product category and brand .. iam using the below code but its not working in adding categories or brands
$post = array(
'post_author' => 1,
'post_content' => '',
'post_status' => "publish",
'post_title' => $product_name,
'post_parent' => '',
'post_type' => "product",
//'post_category' => 17, // Not working after un comment it
);
$post_id = wp_insert_post( $post, $wp_error );
wp_set_post_categories( $post_id, '17', 'brand', true);
wp_set_post_categories( $post_id, '22', 'product_cat', true);
Product created without assigning to any brand or category how can i fix it ?
i ALso tried
wp_set_object_terms( $post_id, 'electronics', 'product_cat' );
and tried
wp_set_post_terms( $post_id, 'electronics', 'product_cat' );
But both not working too
wp_set_post_categories doesn't accept custom taxonomy, it will save to regular post category https://developer.wordpress.org/reference/functions/wp_set_post_categories/#source
This works for saving into Woocommmerce category, I just used it for my needs. Be sure to check that params are integers, not strings like so "10"
$product_id = 10;
$product_category_id = 25;
wp_set_object_terms($product_id, array($product_category_id), 'product_cat')

How do I create a product with variations?

I'm trying to create a product in php with variations. I've gotten the product creation but I can't seem to figure out how to create variations for it. Googling only returns adding variations to existing products, but I'm trying to create a product completely from scratch, give it an attribute, and using that attribute to make variations from an array of strings.
$team = get_post();
$post_id = wp_insert_post(
array( 'post_title' => $team->post_title,
'post_type' => 'product',
'post_status' => 'publish'
)
);
wp_set_object_terms( $post_id, 'simple', 'variable' );
update_post_meta( $post_id, '_sku', 'FEES-'.$team->ID );
This is what I have so far.
I think you need to use "WC_Product_Variation"class for registering variations and wp_insert_post for creating the product variable.
have a look at this example, it might help you or give an idea.
Create WooCommerce Variable Product with Attributes
See more info in woocommerce docs: WC_Product_Variation

WooCommerce adding product variations programatically with different price will not reflect on the product page

Let me put it simple.
We have products with color and size attributes in database
The products are all variable products
Variations are added for every color and "Any" size
Then we wrote a few functions to add 2 more variations to each product for the sizes 2XL and 3XL (the products are t-shirts) with a higher price!
The variations are added beautifully, however, on the front end, the price will not change when I select the size with the increased price unless I go to a product edit page and click the update button. Only then will the price to be added to the cart change upon selecting that size.
How can make the variations with the increased price reflect on the front-end without having to click the update button for each product?
Maybe you need to run variable_product_sync() on each variable product?
function so_run_once(){
$variable_products = get_posts( array(
'posts_per_page'=> -1,
'post_type' => 'product',
'fields' => 'ids',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'product_type',
'field' => 'slug',
'terms' => 'variable',
),
)
) );
if( $variable_products ) foreach( $variable_products as $product_id ){
$_product = wc_get_product( $product_id );
$_product->variable_product_sync();
}
}
add_action( 'admin_init', 'so_run_once' );
Found a solution! We had to delete the transient meta data of each post after inserting the variation..
$transient_name = 'wc_product_children_ids_' . $proudct_id;
delete_transient( $transient_name );

automatically inserted woocommerce product still needs update?

In Wordpress, I have a function which allows me to automatically add a woocommerce product by adding a new post of a custom post type called 'daily_cartoon'.
This the code of the function:
function convert_daily_cartoon_into_product($post_id){
if( ( $_POST['post_status'] == 'publish' ) && ( $_POST['original_post_status'] != 'publish' ) ) {
// Create post object
$post = array(
'post_title' => $_POST['post_title'],
'post_type' => 'product',
'post_status' => 'publish',
'tax_input' => array( 'product_cat' => 14 )
);
// Insert the post into the database
$new_print = wp_insert_post( $post );
update_post_meta( $new_print, '_thumbnail_id', get_post_thumbnail_id( $post_id ) );
update_post_meta( $new_print, '_regular_price', 5 );
update_post_meta( $new_print, '_price', 5 );
}
}
add_action('publish_daily_cartoon', 'convert_daily_cartoon_into_product');
The new product shows up nicely in the admin area, with all the data there - i.e. price, category, published, etc.
But for some reason it doesn't show up on the website shop page until I open the new product to edit and click update.
Anybody?
Make sure you set the _visibility meta of the product to visible
For those comming here
Don't forget to check your datas, I got a similar problem and it's because WP sanitize datas once you update them manually from the dashboard. That's why it worked for those updated manually.
Into my database, my datas got some hidden spaces (example : "Paris" / " Paris").
In case you need to import products, consider to use the wp function sanitize_text_field()

Categories