I have a function that checks if products are in the same category and then if true it allows this product to be added to cart.
If a product does not pass validation I want notification or alert appear(This product in a different category). I do not know how to implement this. Could you please give me some advice.
add_filter('nav_menu_css_class', 'atg_menu_classes', 1, 3);
function is_product_the_same_cat($valid, $product_id, $quantity) {
global $woocommerce;
if($woocommerce->cart->cart_contents_count == 0){
return true;
}
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
$terms = get_the_terms( $_product->id, 'product_cat' );
$target_terms = get_the_terms( $product_id, 'product_cat' );
foreach ($terms as $term) {
$cat_ids[] = $term->term_id;
}
foreach ($target_terms as $term) {
$target_cat_ids[] = $term->term_id;
}
}
$same_cat = array_intersect($cat_ids, $target_cat_ids);
if(count($same_cat) > 0) return $valid;
else {
wp_safe_redirect(get_permalink( get_page_by_path( 'about' ) ) );
exit();;
}
}
add_filter( 'woocommerce_add_to_cart_validation', 'is_product_the_same_cat',10,3);
Maybe you want something like this
echo "<script>alert(\"something\")</script>"
This adds a javascript script in the body of webpage that alerts the user.
Related
I've created a bunch of shortcodes that are either used within posts or echo'd into product pages using wordpress hooks.
I'm trying to create a conditional function so it echos a specific shortcode dependent on the product category. It would need to echo into the 'woocommerce_after_single_product_summary'.
This is the draft so far, can someone give some advice on how to complete this please?
add_action( 'woocommerce_after_single_product_summary', 'add_warranty_notice', 15 );
function add_warranty_notice() {
if ( is_product_category( 'xx-slug' ) ) {
echo do_shortcode("[xx-product-notice]");
} elseif ( is_product_category( 'xy-slug' ) ) {
echo do_shortcode("[xy-product-notice]");
}
}
Hope this help you.
add_action( 'woocommerce_after_single_product_summary', 'add_warranty_notice', 15 );
function add_warranty_notice() {
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
$nterms = get_the_terms( $post->ID, 'product_tag' );
foreach ($terms as $term ) {
$product_cat_id = $term->term_id;
$product_cat_name = $term->name;
break;
}
if ( $product_cat_name == 'parent_one' ) {
echo do_shortcode("[xx-product-notice]");
} elseif ( $product_cat_name == 'parent_two' ) {
echo do_shortcode("[xy-product-notice]");
}else{
}
}
I am trying to add a Sale Price column to admin Products list in Woocommerce.
Here is my code:
add_filter( 'manage_edit-product_columns', 'onsale_product_column', 10);
function onsale_product_column($columns){
$new_columns = [];
foreach( $columns as $key => $column ){
$new_columns[$key] = $columns[$key];
if( $key == 'product_cat' ) {
$new_columns['onsale'] = __( 'Sale Price','woocommerce');
}
}
return $new_columns;
}
add_action( 'manage_product_posts_custom_column',
'onsale_product_column_content', 10, 2 );
function onsale_product_column_content( $column, $product_id ){
global $post;
if( $column == 'onsale' ){
if( $product_id->is_on_sale() ) {
echo $product_id->get_sale_price();
}
echo '-';
}
}
But it doesn't work. What I am doing wrong?
There is some mistakes in your codeā¦ Try the following instead:
add_filter( 'manage_edit-product_columns', 'onsale_product_column', 10);
function onsale_product_column($columns){
$new_columns = [];
foreach( $columns as $key => $column ){
$new_columns[$key] = $columns[$key];
if( $key == 'product_cat' ) {
$new_columns['onsale'] = __( 'Sale Price','woocommerce');
}
}
return $new_columns;
}
add_action( 'manage_product_posts_custom_column', 'onsale_product_column_content', 10, 2 );
function onsale_product_column_content( $column, $post_id ){
if( $column == 'onsale' ){
global $post, $product;
// Excluding variable and grouped products
if( is_a( $product, 'WC_Product' ) && ! $product->is_type('grouped') &&
! $product->is_type('variable') && $product->is_on_sale() ) {
echo strip_tags( wc_price( $product->get_sale_price() ) );
}
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and work.
I have a woocommerce site I use the booster plugin to enable sold individually for each product but now I want to sold individually for each category and if someone add a product to cart on a category then can not add another product of that category to cart
In the code below there is 2 functions:
The 1st one is a conditional function checking categories in cart items
The 2nd one will avoid add to cart when a product category already exist in cart items and will display a custom message.
The code:
// Conditional function: Checking product categories in cart items
function check_cats_in_cart( $product_id ) {
$taxonomy = 'product_cat';
$has_term = true;
foreach( WC()->cart->get_cart() as $item ){
foreach( wp_get_post_terms( $item['product_id'], $taxonomy ) as $term ){
// Allowing add to cart the same product
if( has_term( $term->slug, $taxonomy, $product_id ) ){
$has_term = false;
break; // stops the 2nd loop
}
}
// Allowing the same product to be added (not activated. you can uncomment lines below)
# if ( $item['product_id'] == $product_id )
# $has_term = true;
if( $has_term )
break; // stops the 1st loop
}
return $has_term;
}
// Avoid add to cart when a product category already exist in cart items, displaying a custom error message
add_filter( 'woocommerce_add_to_cart_validation', 'sold_individually_by_cats_valid', 10, 3 );
function sold_individually_by_cats_valid( $passed, $product_id, $quantity) {
$passed = check_cats_in_cart( $product_id );
if( ! $passed ){
// Displaying a custom message
$message = __("This product category is already in cart. Try another product", "woocommerce");
wc_add_notice( $message, 'error' );
}
return $passed;
}
Code goes in function.php file of the active child theme (or active theme).
Tested and works.
If you want to allow adding to cart again the same product (that increases only the quantity of an existing product), you will uncomment this code in the function:
if ( $item['product_id'] == $product_id )
$has_term = true;
You can try it this one
function is_product_the_same_cat($valid, $product_id, $quantity) {
global $woocommerce;
if($woocommerce->cart->cart_contents_count == 0){
return true;
}
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
$terms = get_the_terms( $_product->id, 'product_cat' );
$target_terms = get_the_terms( $product_id, 'product_cat' );
foreach ($terms as $term) {
$cat_ids[] = $term->term_id;
}
foreach ($target_terms as $term) {
$target_cat_ids[] = $term->term_id;
}
}
$same_cat = array_intersect($cat_ids, $target_cat_ids);
if(count($same_cat) > 0) return $valid;
else {
wc_add_notice( 'This product is in another category!', 'error' );
return false;
}
}
add_filter( 'woocommerce_add_to_cart_validation', 'is_product_the_same_cat',10,3);
I found it from here
I'm wanting to stop customers from being able to add items from other categories once an item has been added to the cart. The customer should still be able to add items from the same category.
Here is what I have so far:
function is_product_the_same_cat($valid, $product_id, $quantity) {
global $woocommerce;
// start of the loop that fetches the cart items
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
$terms = get_the_terms( $_product->id, 'product_cat' );
$target_terms = get_the_terms( $product_id, 'product_cat' ); //get the current items
foreach ($terms as $term) {
$cat_ids[] = $term->term_id; //get all the item categories in the cart
}
foreach ($target_terms as $term) {
$target_cat_ids[] = $term->term_id; //get all the categories of the product
}
}
$same_cat = array_intersect($cat_ids, $target_cat_ids); //check if they have the same category
if(count($same_cat) > 0) return $valid;
else {
wc_add_notice( 'This product is in another category!', 'error' );
return false;
}
}
add_filter( 'woocommerce_add_to_cart_validation', 'is_product_the_same_cat',10,3);
This works if I already have an item in my cart but it doesn't let me add anything from an empty cart and always displays the error message.
Thanks!
After some tweaking I think I figured it out:
#
function is_product_the_same_cat($valid, $product_id, $quantity) {
global $woocommerce;
if($woocommerce->cart->cart_contents_count == 0){
return true;
}
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
$terms = get_the_terms( $_product->id, 'product_cat' );
$target_terms = get_the_terms( $product_id, 'product_cat' );
foreach ($terms as $term) {
$cat_ids[] = $term->term_id;
}
foreach ($target_terms as $term) {
$target_cat_ids[] = $term->term_id;
}
}
$same_cat = array_intersect($cat_ids, $target_cat_ids);
if(count($same_cat) > 0) return $valid;
else {
wc_add_notice( 'This product is in another category!', 'error' );
return false;
}
}
add_filter( 'woocommerce_add_to_cart_validation', 'is_product_the_same_cat',10,3);
In your is_product_the_same_cat method, your have not added any code to perform when the cart is empty. May be because of that you cant add when cart is empty.
Add following code after global $woocommerce; line
if ( woocommerce()->cart->get_cart_contents_count() == 0 ) {
return true;
}
I am trying to redirect a user to previous category page when he clicks on Add to Cart on product page. But on redirect I am seeing a blank product_cat attribute. i.e. example.com/?product_cat=
However if I echo it to woocommerce_product_thumbnail it shows the link perfectly. i.e. example.com/?product_cat=shoes
add_filter ('add_to_cart_redirect', 'redirect_to_previousCat');
//add_filter ('woocommerce_product_thumbnails', 'redirect_to_previousCat');
function redirect_to_previousCat() {
global $woocommerce, $post;
$product_cat_slug;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
$product_cat_slug = $term->slug;
break;
}
$url=get_site_url().'?product_cat='.$product_cat_slug;
return $url;
}
As I mentioned in the comments, the global $post is not set up yet when the add_to_cart_action() method runs on the init hook.
Instead, I suggest you follow Wootheme's lead and get the product ID from the $_REQUEST global.
add_filter ('add_to_cart_redirect', 'redirect_to_previousCat');
function redirect_to_previousCat( $url ) {
$product_id = absint( $_REQUEST['add-to-cart'] );
$product_cat_slug = '';
$terms = get_the_terms( $product_id, 'product_cat' );
foreach ( $terms as $term ) {
$product_cat_slug = $term->slug;
break;
}
if( $product_cat_slug ){
$url = add_query_arg( 'product_cat', $product_cat_slug, site_url() );
}
return $url;
}