WooCommerce - Open the new product that the category is selected - wordpress

I've been searching all over the internet for hours how to automatically assign a category to new products. When you open the new product that the category is selected.
I have two product categories (Books and FIlms) and I would like that every time we open a new product, Books are selected. In this way it will directly show the custom fields of that product category and we will speed up the creation process.
It's possible?
thanks
UPDATE
I found a code that seems to work with the posts. Any idea how I can adapt the code for products and where should I put the category I care about?
https://wordpress.stackexchange.com/questions/243462/automatically-select-categories-on-new-post-based-on-get-value

When it comes to the checkbox that should already be checked when creating a new product, you can use jQuery
Replace in-product_cat-15 with in-product_cat-{YOUR-CAT-ID}
// Define the admin_head callback
function action_admin_head() {
global $post, $pagenow;
// Only on new product pages
if( $pagenow != 'post-new.php' ) return;
?>
<script>
jQuery(function($){
$( '#in-product_cat-15' ).attr( 'checked', true );
});
</script>
<?php
}
add_action( 'admin_head', 'action_admin_head', 10, 0 );

In product categories click the Make default action of your desired category to make it as default category. For more details refer the image:
You can visit this page using this URL (replace the domain with your domain):
www.example.com/wp-admin/edit-tags.php?taxonomy=product_cat&post_type=product

Related

How do I add product attributes to the product page in WooCommerce?

I'm using WooCommerce and Elementor Pro. Is there any way to add specific product attributes (e.g. gross and net weight) below the Add to cart button?
It seems like an obvious thing but I haven't found options or snippets for it.
First you add attributes to product. Then you use this snippet in functions.php:
// maybe remove tab with attributes
add_filter( 'woocommerce_product_tabs', 'remove_info', 100, 1 );
function remove_info( $tabs ) {
unset($tabs['additional_information']);
return $tabs;
}
// show attributes
add_action( 'woocommerce_single_product_summary', 'summary_attributes', 35 );
function summary_attributes() {
global $product;
if ( $product->has_attributes() ) {
wc_display_product_attributes( $product );
}
}
Setting Up WooCommerce Attributes
go to Products > Attributes
Add the attribute name
Slug will automatically be created, and we can leave the rest of these options untouched.
Click “Add attribute” button and your attribute will be added.
Configure The Attributes
After creating attribute, now we can add different variation on your product. Click on the Configure Terms option.
Enter the variation
Add some short description
Click on the add new tab
To understand it in a better way you can follow this tutorial

Woocommerce disable selling on products published by a specific user role

I'm going crazy for a few hours, and I need help. Please :) I would like to disable the sale of products added by a specific seller in woocommerce. I use Dokan, a plugin that transforms woocommerce into a market place. This is what I did and that seems to me a good approach: I have created a new role "nonsellingshop" which has the same rights as the default seller role created by Dokan. This role can therefore add products and their price. I would like products added by this specific role can not be purchased, and instead of the add to cart button appears a message saying 'This seller does not offer its products for sale online'. Of course for the sellers not having this role, it is necessary that the sale is possible. It is also necessary that if I change the role of a seller "nonsellingshop" to give it the normal role, the sale becomes possible.
Does anyone have an idea of ​​how I can do this?
Thank you a thousand times in advance for your help
So this what I've done so far (and that doesn't work) :
function get_seller_role_and_hide_cart_button_if_non_seller () { //gets the ID of the post $post_id = get_queried_object_id();
//gets the ID of the author using the ID of the post
$author_ID = get_post_field( 'post_author', $post_id );
//Gets all the data of the author, using the ID
$authorData = get_userdata( $author_ID );
//checks if the author has the role of 'subscriber'
if (in_array( 'boutiquenonvendeuse', $authorData->roles)){
add_filter( 'woocommerce_is_purchasable', '__return_false'); // DISABLING PURCHASE FUNCTIONALITY AND REMOVING ADD TO CART BUTTON FROM NORMAL PRODUCTS
remove_action('woocommerce_single_variation', 'woocommerce_single_variation', 10); // REMOVING PRICE FROM VARIATIONS
remove_action('woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20); // REMOVING ADD TO CART BUTTON FROM VARIATIONS
}
} add_action ('init', 'get_seller_role_and_hide_cart_button_if_non_seller');

Woocommerce - Group by first letter

I am trying to display the products in my Shop page like that:
A
Asd
Asddd
B
Beer
Bear
and so on. I managed to do this for the categories by overriding and using the woocommerce_output_product_categories action and for them it works, but I want to do this for the products as well(since Woocommerce gives you the option to show products or categories in the shop page). Thank you!
There can be so many possible solutions. But for me this can be done like this:
add_action( 'woocommerce_shop_loop', 'wc_shop_loop', 30 );
function wc_shop_loop() {
global $product, $last_title_first_letter_95845949545454;
$title = $product->get_title();
if ( $last_title_first_letter_95845949545454 !== $title[0] ) {
$last_title_first_letter_95845949545454 = $title[0];
woocommerce_product_loop_end(); // let's close the loop.
echo '<h3>'.$last_title_first_letter_95845949545454. '</h3>'; // add a letter heading.
woocommerce_product_loop_start(); // open a new loop start.
}
}
Tested to work on shop page and product category page.
You will need to work on its css.

Woocommerce choose grid or list view for each category

I want the possibility to choose between grid and list view for each Woocommerce category. I have found this plugin: https://nl.wordpress.org/plugins/woocommerce-grid-list-toggle/
However, the plugin is meant for the shopper to choose whether to display items in grid view or list view. What I truly want is the ability to assign a view for each category in the back-end.
Example:
Category A is displayed as grid
Category B is displayed as list
Breaking my head over this.
Similar to this question you need to filter template_include. You need to call your custom archive template archive-list-view.php and save it in your theme's woocommerce folder. Obviously, you can name it anything you like, you will just have to adjust the code below to match.
Folder structure:
/theme-folder/functions.php
/theme-folder/woocommerce/archive-list-view.php
On the template_include filter we will check if we are on the term archive for the nussmylch product category. If so, we'll look for and supply the new template. Otherwise, the standard template is used.
EDIT: incorrect WooCommerce function is_product_taxonomy() was used. is_product_category() is needed to check for a specific category.
add_filter( 'template_include', 'so_33615903_custom_category_template', 20 );
function so_33615903_custom_category_template( $template ) {
// check you are on the taxonomy archive for specific category
if ( is_product_category( 'nussmylch' ) ) {
$new_template = locate_template( array( 'woocommerce/archive-list-view.php' ) );
if ( '' != $new_template ) {
$template = $new_template ;
}
}
return $template;
}
Working Example

Woocommerce: only allow products from one tag per transaction

I have a WooCommerce site with the Responsive Supermarket Online Theme - Oswad installed. Each product has one of 4 tags corresponding to a different vendor. What I need to do when adding to cart:
Check the cart for products and get their tag.
If cart is empty, or the product tags match, allow adding to cart as per normal.
If the product has a different tag then that of the cart contents, disable the add to cart button.
Steps 1-3 should only occur once per transaction.
I know that I can check the tag with if ( has_term( 'tag', 'product_tag') ),
and in the add-to-cart.php, $product->isPurchasable() will toggle the visibility of the button.
My stumbling block is finding out how to put all these together to fulfill the requirement. I tried looking for how to restrict the cart items by categories but came up with dead ends. I looked for plugins but those restricted only the minimum/maximum product per order and not checking the category/tags of the items. I even tried to look at the hooks but ended up getting more confused.
Would appreciate it if I could get a pointer in the right direction.
One possible solution - at the add-to-cart.php:
global $woocommerce, $product;
$terms = wp_get_post_terms($product->id, 'product_tag');
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values){
$_product = $values["data"];
$_terms = wp_get_post_terms($_product->id, 'product_tag');
//echo "product slug: " . $terms[0]->slug;
//echo "cart slug: " . $_terms[0]->slug;
if ($terms[0]->slug !== $_terms[0]->slug){
//echo "NO MATCH";
$match = false;
}
}
and then add a if ($match) statement controlling the add to cart button: apply_filters('woocommerce_loop_add_to_cart_link', ...
Also, add the code to woocommerce/single-product/add-to-cart/simple.php and add the if statement to the location of the add to cart button.

Resources