cliff
9/23/2016 - 3:09 PM

Make WooCommerce cart Quantity input field non-editable (readonly) ONLY FOR Event Tickets Plus products

Make WooCommerce cart Quantity input field non-editable (readonly) ONLY FOR Event Tickets Plus products

<?php

/**
 * Make WooCommerce cart Quantity input field non-editable (readonly) ONLY FOR Event Tickets Plus products
 *
 * By Matt B and Cliff P
 *
 * From https://gist.github.com/cliffordp/66bf05df61ee269c60ff20d6f39e2cab
 */
function cliff_etplus_woo_cart_quantity_readonly() {

    if ( ! class_exists( 'Tribe__Tickets_Plus__Commerce__WooCommerce__Main' ) ) {
        return;
    }

    $args = array(
        'post_type'      => 'product',
        'posts_per_page' => -1,
        'fields'         => 'ids',
        'meta_query'     => array(
            array(
                'key' => Tribe__Tickets_Plus__Commerce__WooCommerce__Main::get_instance()->event_key,
                'value' => 0,
                'compare' => '>',
            ),
        ),
    );

    $product_ids_readonly = get_posts( $args );
    
    $product_ids_readonly = array_unique( $product_ids_readonly );

    if ( ! count( $product_ids_readonly ) ) {
        return;
    }

    wp_enqueue_script( 'jquery' );
    ?>
    <script>
        jQuery( function( $ ) {
            var product_ids = <?php echo json_encode( $product_ids_readonly ); ?>;
            var $remove_links = $( 'body.woocommerce-cart .woocommerce tr.cart_item td.product-remove a' );
            $remove_links.each( function() {
                var product_id = parseInt( $( this ).data( 'product_id' ), 10 );
                if ( -1 === $.inArray( product_id, product_ids ) ) {
                    return;
                }

                var $row = $( this ).closest( 'tr' );
                $row.find( 'td.product-quantity input' ).attr( 'readonly', true );
            } );
        } );
    </script>
    <?php
}
add_action( 'wp_head', 'cliff_etplus_woo_cart_quantity_readonly' );