• trying to detect a product category when the user adds an item to the cart in woocommerce. So that they get a different ‘added to cart’ message depending on the product they add

    the idea is that if they add a picture from the gallery it points them towards mounting services, but if they add a mounting service it gives the standard message

    this is the code i have so far it just skips the category filter and runs the standard message.

    function wc_add_to_cart_message( $product_id ) {
    
    if ( is_array( $product_id ) ) {
    
    $titles = array();
    
    foreach ( $product_id as $id ) {
        $titles[] = get_the_title( $id );
    }
    
    $added_text = sprintf( __( 'Added "%s" to your cart.', 'woocommerce' ), join( __( '" and "', 'woocommerce' ), array_filter( array_merge( array( join( '", "', array_slice( $titles, 0, -1 ) ) ), array_slice( $titles, -1 ) ) ) ) );
    
    } elseif ( is_product_category( 'gallery') ) {
    $added_text = sprintf( 'add a mount.' );
    }else {
    $added_text = sprintf( __( '"%s" was successfully added to your cart.', 'woocommerce' ), get_the_title( $product_id ) );
    }

    Never really dug this deep into the code before so im at a bit of a loss right now

    any thoughts?

    https://wordpress.org/plugins/woocommerce/

Viewing 1 replies (of 1 total)
  • Thread Starter kennydowuk

    (@kennydowuk)

    worked my way to this point. (
    it just appears to ignore y code and out put the original message

    function wc_add_to_cart_message( $product_id) {
    	if ( is_array( $product_id ) ) {
    
    $categories = get_the_category( $id);
    
    		$titles = array();
    
    		foreach ( $product_id as $id ) {
    			$titles[] = get_the_title( $id );
    		}
    
    		$added_text = sprintf( __( 'Added "%s" to your cart.', 'woocommerce' ), join( __( '" and "', 'woocommerce' ), array_filter( array_merge( array( join( '", "', array_slice( $titles, 0, -1 ) ) ), array_slice( $titles, -1 ) ) ) ) );
    
    	} else {
    		<if($categories == 'gallery') {
    			$added_text = sprintf(__( '"%s" add a mount' );
    		}else{
    			$added_text = sprintf( __( '"%s" was successfully added to your cart.', 'woocommerce' ), get_the_title( $product_id ) );
    	}
    	}
Viewing 1 replies (of 1 total)
  • The topic ‘customizing messages by product category in woocomerce’ is closed to new replies.