Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@cliffordp
Last active July 16, 2021 00:06
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cliffordp/66bf05df61ee269c60ff20d6f39e2cab to your computer and use it in GitHub Desktop.
Save cliffordp/66bf05df61ee269c60ff20d6f39e2cab to your computer and use it in GitHub Desktop.
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' );
@yodacoin
Copy link

yodacoin commented Mar 1, 2017

thank you, can i use somehow to apply this method to my custom type product? i have custom type product "my_photo"

@prikkprikkprikk
Copy link

This works, but the $args needs 'posts_per_page' => -1 to fetch all ticket ids, instead of just the first five.

@cliffordp
Copy link
Author

@sisaacrussell
Copy link

There's a stray . in line 17 that causes a fatal error.

@cliffordp
Copy link
Author

@sisaacrussell - fixed, good catch, tyvm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment