Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tevyaw/78308937c3550e5d7d26 to your computer and use it in GitHub Desktop.
Save tevyaw/78308937c3550e5d7d26 to your computer and use it in GitHub Desktop.
This is a fork of this: https://gist.github.com/ChromeOrange/3200974 which allows you limit individual products in WooCommerce to a quantity of 1 for that item, per order. It also removes the quantity field on any items with this setting turned on. For this fork I simply added the comments to make it a WP plugin instead of needing to be added to…
<?php
/**
* Plugin Name: WooCommerce Disable Quantity Per Product
* Plugin URI: https://gist.github.com/FiddlerStudios/78308937c3550e5d7d26
* Description: Allows you to disable the quantity option on individual products.
* Version: 0.2
* Author: Andrew Benbow
* Author URI: https://gist.github.com/ChromeOrange
*/
add_filter( 'woocommerce_is_sold_individually', 'cj_woocommerce_is_sold_individually', 10, 2 );
function cj_woocommerce_is_sold_individually( $value, $product ) {
$isi = get_post_meta( $product->id, '_custom_sell_individually', TRUE );
if ( $isi == 'individual' ) :
$return = true;
endif;
return $return;
}
if ( is_admin() ) :
add_action( 'woocommerce_product_options_dimensions', 'isi_write_panel');
add_action( 'woocommerce_process_product_meta', 'isi_write_panel_save' );
function isi_write_panel() {
echo '<div class="options_group">';
woocommerce_wp_select( array( 'id' => '_custom_sell_individually',
'label' => __('Sell Individually?', 'woocommerce'),
'options' => apply_filters('woocommerce_product_visibility_options',
array(
'multiple' => __('Multiple purchases allowed', 'woocommerce'),
'individual' => __('Individual Only', 'woocommerce')
)),
'description' => __('Limit the product to one per order?', 'woocommerce') ) );
echo '</div>';
}
function isi_write_panel_save( $post_id ) {
$_custom_sell_individually = esc_attr($_POST['_custom_sell_individually']);
update_post_meta($post_id, '_custom_sell_individually', $_custom_sell_individually);
}
endif;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment