Skip to content

Instantly share code, notes, and snippets.

@cliffordp

cliffordp/functions.php

Last active Jun 3, 2019
Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@yodacoin 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

This comment has been minimized.

Copy link

@prikkprikkprikk prikkprikkprikk commented Aug 29, 2018

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

@cliffordp

This comment has been minimized.

Copy link
Owner Author

@cliffordp cliffordp commented Nov 21, 2018

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