Easily require your customers to buy a minimum / maximum / incremental amount of products to continue with their checkout.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

120 lines
4.0 KiB

<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( ! class_exists( 'WC_Quantities_and_Units_Units_Box' ) ) :
class WC_Quantities_and_Units_Units_Box {
public function __construct() {
add_action( 'save_post', array( $this, 'save_post_unit_meta_data' ) );
add_action( 'woocommerce_product_data_panels', array( $this, 'units_box_create' ) );
add_action( 'woocommerce_product_data_tabs', array($this, 'units_box_tab'), 99 );
add_action( 'bulk_edit_custom_box', array( $this, 'units_box_bulk_edit' ), 10, 2 );
add_action( 'woocommerce_product_bulk_and_quick_edit', array( $this, 'units_box_bulk_edit_save_post' ), 10, 2 );
}
function units_box_tab($tabs) {
$tabs['units'] = array(
'label' => 'Units',
'target' => 'wciu_units_panel',
'class' => array('wciu_units_panel', 'hide_if_grouped'),
'priority' => 100
);
return $tabs;
}
public function units_box_create() {
global $post,$woocommerce;
$unit = get_post_meta($post->ID, 'unit', true);
?>
<div id="wciu_units_panel" class="panel woocommerce_options_panel">
<div class="options_group hide_if_grouped">
<p class="form-field _unit_field">
<label for="unit">Unit of measurement</label>
<input type="text" name="_unit" value="<?php echo $unit ?>" placeholder="Unit ie. kg, lbs, oz">
<input type="hidden" name="_wciu_nonce" value="<?php echo wp_create_nonce(plugin_basename( __FILE__ )) ?>">
</p>
</div>
</div>
<?php
}
function save_unit_meta_data( $post_id, $url_args ) {
// Validate Post Type
if ( ! isset( $url_args['post_type'] ) or $url_args['post_type'] !== 'product' ) {
return;
}
// Validate User
if ( !current_user_can( 'edit_post', $post_id ) ) {
return;
}
// Verify Nonce
if ( ! isset( $url_args["_wciu_nonce"] ) or ! wp_verify_nonce( $url_args["_wciu_nonce"], plugin_basename( __FILE__ ) ) ) {
return;
}
// Update Unit Meta Values
if( isset( $url_args['_unit'] )) {
update_post_meta(
$post_id,
'unit',
strip_tags( $url_args['_unit'] )
);
} else {
update_post_meta(
$post_id,
'unit',
''
);
}
}
public function save_post_unit_meta_data( $post_id ) {
$this->save_unit_meta_data( $post_id, $_POST );
}
public function units_box_bulk_edit( $column_name, $post_type ) {
if ( 'name' != $column_name || 'product' != $post_type ) {
return;
}
$options = array( '-1' => '&mdash; No Change &mdash;',
'1' => 'Change unit to' );
?>
<fieldset class="inline-edit-col-left">
<div class="inline-edit-col">
<div class="inline-edit-group">
<label class="inline-edit-status alignleft">
<span class="title">Unit of measurement</span>
<span class="input-text-wrap">
<select name="_unit_visibility">
<?php
foreach($options as $name => $label){
echo '<option value="'. $name .'">'. $label .'</option>';
}
?>
</select>
</span>
</label>
<label class="change-input">
<input type="text" name="_unit" placeholder="Unit ie. kg, lbs, oz" style="display: none;">
<input type="hidden" name="_wciu_nonce" value="<?php echo wp_create_nonce(plugin_basename( __FILE__ )) ?>">
</label>
</div>
</div>
</fieldset>
<?php
}
public function units_box_bulk_edit_save_post ( $post_id, $post ) {
if ( isset($_REQUEST['_unit_visibility']) && $_REQUEST['_unit_visibility'] > 0 ) {
$this->save_unit_meta_data( $post_id, $_REQUEST );
}
}
}
endif;
return new WC_Quantities_and_Units_Units_Box();