• i wanted to display extra (required) textarea in the product pages, so i sed this code and it worked successfully:

     
    
    // Display custom field on single product page
        function d_extra_product_field(){
            $value = isset( $_POST['extra_product_field'] ) ? sanitize_text_field( $_POST['extra_product_field'] ) : '';
            printf( '<div><label>%s</label><br><textarea name="extra_product_field" value="%s"></textarea></div>', __( '<br><br><br> Enter your order notes' ), esc_attr( $value ) );
        }
        add_action( 'woocommerce_after_add_to_cart_button', 'd_extra_product_field', 9 );
    
        // validate when add to cart
        function d_extra_field_validation($passed, $product_id, $qty){
    
            if( isset( $_POST['extra_product_field'] ) && sanitize_text_field( $_POST['extra_product_field'] ) == '' ){
                $product = wc_get_product( $product_id );
                wc_add_notice( sprintf( __( '%s يرجى إدخال ملاحظات الطلب لإضافة المنتج الى السلة.' ), $product->get_title() ), 'error' );
                return false;
            }
    
            return $passed;
    
        }
        add_filter( 'woocommerce_add_to_cart_validation', 'd_extra_field_validation', 10, 3 );
    
         // add custom field data in to cart
        function d_add_cart_item_data( $cart_item, $product_id ){
    
            if( isset( $_POST['extra_product_field'] ) ) {
                $cart_item['extra_product_field'] = sanitize_text_field( $_POST['extra_product_field'] );
            }
    
            return $cart_item;
    
        }
        add_filter( 'woocommerce_add_cart_item_data', 'd_add_cart_item_data', 10, 2 );
    
        // load data from session
        function d_get_cart_data_f_session( $cart_item, $values ) {
    
            if ( isset( $values['extra_product_field'] ) ){
                $cart_item['extra_product_field'] = $values['extra_product_field'];
            }
    
            return $cart_item;
    
        }
        add_filter( 'woocommerce_get_cart_item_from_session', 'd_get_cart_data_f_session', 20, 2 );
    
    
        //add meta to order
        function d_add_order_meta( $item_id, $values ) {
    
            if ( ! empty( $values['extra_product_field'] ) ) {
                woocommerce_add_order_item_meta( $item_id, 'extra_product_field', $values['extra_product_field'] );           
            }
        }
        add_action( 'woocommerce_add_order_item_meta', 'd_add_order_meta', 10, 2 );
    
        // display data in cart
        function d_get_itemdata( $other_data, $cart_item ) {
    
            if ( isset( $cart_item['extra_product_field'] ) ){
    
                $other_data[] = array(
                    'name' => __( 'Your extra field text' ),
                    'value' => sanitize_text_field( $cart_item['extra_product_field'] )
                );
    
            }
    
            return $other_data;
    
        }
        add_filter( 'woocommerce_get_item_data', 'd_get_itemdata', 10, 2 );
    
    
        // display custom field data in order view
        function d_dis_metadata_order( $cart_item, $order_item ){
    
            if( isset( $order_item['extra_product_field'] ) ){
                $cart_item_meta['extra_product_field'] = $order_item['extra_product_field'];
            }
    
            return $cart_item;
    
        }
        add_filter( 'woocommerce_order_item_product', 'd_dis_metadata_order', 10, 2 );
    
    
        // add field data in email
        function d_order_email_data( $fields ) { 
            $fields['extra_product_field'] = __( 'Your extra field text' ); 
            return $fields; 
        } 
        add_filter('woocommerce_email_order_meta_fields', 'd_order_email_data');
    
        // again order
        function d_order_again_meta_data( $cart_item, $order_item, $order ){
    
            if( isset( $order_item['extra_product_field'] ) ){
                $cart_item_meta['extra_product_field'] = $order_item['extra_product_field'];
            }
    
            return $cart_item;
    
        }
        add_filter( 'woocommerce_order_again_cart_item_data', 'd_order_again_meta_data', 10, 3 );
    
    
    
    
    
    add_action( 'wp_enqueue_scripts', 'custom_enqueue_wc_cart_fragments' );
    function custom_enqueue_wc_cart_fragments() {
        wp_enqueue_script( 'wc-cart-fragments' );
    }

    now, i want to hide the textarea from only one category., how to do that?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    Typically, a category page has a class assigned relevant to the category. Look at the <body> tag for that page. If so, you can use it in your CSS selector.

    Thread Starter bodytayb

    (@bodytayb)

    1

    Its not possible with CSS. Even if you use display:none, the element will be still in DOM tree.

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    Right. You asked “how do I hide this”. If you want to eliminate it, you’ll need to add a test for the category to decide whether to output the textarea. This might help: https://njengah.com/get-current-product-category/

    Thread Starter bodytayb

    (@bodytayb)

    thank you

    i tried to use this code:

    global $post;
    $terms = get_the_terms( $post->ID, ‘product_cat’ );
    foreach ($terms as $term) {
    $product_cat_id = $term->term_id;
    break;
    }

    then i used if statement before the code:

    if ( $product_cat_id != 43 ) {

    }

    as i want to execute the code on all categories but the one with id =43

    but it doesn’t work also

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    What is the value of $product_cat_id?

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘[NSFW] how to hide note box from specific category’ is closed to new replies.