Last active
July 16, 2021 00:06
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ); |
This works, but the $args needs 'posts_per_page' => -1
to fetch all ticket ids, instead of just the first five.
@yodacoin: This is for https://theeventscalendar.com/product/wordpress-event-tickets-plus/ using WooCommerce.
@prikkprikkprikk: updated. Good catch.
There's a stray .
in line 17 that causes a fatal error.
@sisaacrussell - fixed, good catch, tyvm
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you, can i use somehow to apply this method to my custom type product? i have custom type product "my_photo"