Skip to content

Instantly share code, notes, and snippets.

@rayflores
Last active August 29, 2015 14:04
Show Gist options
  • Save rayflores/18b174ed3110e0e16e81 to your computer and use it in GitHub Desktop.
Save rayflores/18b174ed3110e0e16e81 to your computer and use it in GitHub Desktop.
<?php
global $post, $product;
?>
<div class="col-lg-6 col-md-6 toppurchase textaligncenter">
<?php if ( $product->is_in_stock() ) : ?>
<?php
if ( ! $product->is_sold_individually() )
woocommerce_quantity_input( array(
'min_value' => apply_filters( 'woocommerce_quantity_input_min', 1, $product ),
'max_value' => apply_filters( 'woocommerce_quantity_input_max', $product->backorders_allowed() ? '' : $product->get_stock_quantity(), $product )
) );
?>
<button class="btn btn-primary btn-icon alt" data-toggle="modal" data-target="#purchaseWithOptions">Add To Cart</button>
<!-- Modal -->
<div class="modal fade" id="purchaseWithOptions" tabindex="-1" role="dialog" aria-labelledby="myModalLabelPurch" aria-hidden="true">
<div class="modal-dialog modal-med featured-box-secundary featured-box" style="width:310px;">
<div class="modal-content box-content">
<div class="modal-header" >
</div><!--end modal header -->
<div class="modal-body">
<?php //echo do_shortcode('[watcpus_verify_form]'); ?>
<?php
/**
* Simple product add to cart
*
* @author WooThemes
* @package WooCommerce/Templates
* @version 2.1.0
*/
global $woocommerce, $product;
if ( ! $product->is_purchasable() ) return;
?>
<?php if ( $product->is_in_stock() ) : ?>
<?php do_action( 'woocommerce_before_add_to_cart_form' ); ?>
<form id="watcpus_form" method="post">
<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<label><?php echo get_option('woo_watcpus_label'); ?></label>
<input type="text" name="woo_watcpus_search_zips" value="" />
<?php wp_nonce_field('watcpus_nonce_action','watcpus_error_notice'); ?>
<input type="hidden" name="add-to-cart" value="<?php echo esc_attr( $product->id ); ?>" />
<input type="submit" name="woo_watcpus_process_zips" value="<?php _e('Verify'); ?>" class="btn btn-primary btn-icon alt"/>
<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
</form>
<?php do_action( 'woocommerce_after_add_to_cart_form' ); ?>
<?php endif; ?>
</div><!-- end modal body -->
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div><!-- end modal footer -->
</div><!-- end modal content -->
</div><!-- end modal dialog -->
</div><!-- end modal purchaseWithOptions -->
<?php endif; ?>
</div>
<?php
/**
* Variable product add to cart
*
* @author WooThemes
* @package WooCommerce/Templates
* @version 2.1.0
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $woocommerce, $product, $post;
?>
<?php do_action( 'woocommerce_before_add_to_cart_form' ); ?>
<form class="variations_form cart" method="post" enctype='multipart/form-data' data-product_id="<?php echo $post->ID; ?>" data-product_variations="<?php echo esc_attr( json_encode( $available_variations ) ) ?>">
<?php if ( ! empty( $available_variations ) ) : ?>
<div class="variations" cellspacing="0">
<?php $loop = 0; foreach ( $attributes as $name => $options ) : $loop++; ?>
<div class="row">
<div class="form-group">
<div class="col-md-7">
<?php
if ( sizeof( $attributes ) == $loop )
echo '<a class="pull-right reset_variations" href="#reset">' . __( 'Clear selection', 'woocommerce' ) . '</a>';
?>
<label for="<?php echo sanitize_title($name); ?>"><?php echo wc_attribute_label( $name ); ?></label>
<select class="form-control" id="<?php echo esc_attr( sanitize_title( $name ) ); ?>" name="attribute_<?php echo sanitize_title( $name ); ?>">
<option value=""><?php echo __( 'Choose an option', 'woocommerce' ) ?>&hellip;</option>
<?php
if ( is_array( $options ) ) {
if ( isset( $_REQUEST[ 'attribute_' . sanitize_title( $name ) ] ) ) {
$selected_value = $_REQUEST[ 'attribute_' . sanitize_title( $name ) ];
} elseif ( isset( $selected_attributes[ sanitize_title( $name ) ] ) ) {
$selected_value = $selected_attributes[ sanitize_title( $name ) ];
} else {
$selected_value = '';
}
// Get terms if this is a taxonomy - ordered
if ( taxonomy_exists( $name ) ) {
$orderby = wc_attribute_orderby( $name );
switch ( $orderby ) {
case 'name' :
$args = array( 'orderby' => 'name', 'hide_empty' => false, 'menu_order' => false );
break;
case 'id' :
$args = array( 'orderby' => 'id', 'order' => 'ASC', 'menu_order' => false, 'hide_empty' => false );
break;
case 'menu_order' :
$args = array( 'menu_order' => 'ASC', 'hide_empty' => false );
break;
}
$terms = get_terms( $name, $args );
foreach ( $terms as $term ) {
if ( ! in_array( $term->slug, $options ) )
continue;
echo '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $selected_value ), sanitize_title( $term->slug ), false ) . '>' . apply_filters( 'woocommerce_variation_option_name', $term->name ) . '</option>';
}
} else {
foreach ( $options as $option ) {
echo '<option value="' . esc_attr( sanitize_title( $option ) ) . '" ' . selected( sanitize_title( $selected_value ), sanitize_title( $option ), false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) ) . '</option>';
}
}
}
?>
</select>
</div>
</div>
</div>
<?php endforeach;?>
</div>
<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<div class="single_variation_wrap" style="display:none;">
<?php do_action( 'woocommerce_before_single_variation' ); ?>
<div class="single_variation"></div>
<div class="variations_button">
<?php woocommerce_quantity_input(); ?>
<button class="btn btn-primary btn-icon alt" data-toggle="modal" data-target="#purchaseWithOptions">Add To Cart</button>
</div>
<input type="hidden" name="add-to-cart" value="<?php echo $product->id; ?>" />
<input type="hidden" name="product_id" value="<?php echo esc_attr( $post->ID ); ?>" />
<input type="hidden" name="variation_id" value="" />
<?php do_action( 'woocommerce_after_single_variation' ); ?>
</div>
<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
<?php else : ?>
<p class="stock out-of-stock"><?php _e( 'This product is currently out of stock and unavailable.', 'woocommerce' ); ?></p>
<?php endif; ?>
</form>
<?php do_action( 'woocommerce_after_add_to_cart_form' ); ?>
<!-- Modal -->
<div class="modal fade" id="purchaseWithOptions" tabindex="-1" role="dialog" aria-labelledby="myModalLabelPurch" aria-hidden="true">
<div class="modal-dialog modal-med featured-box-secundary featured-box" style="width:310px;">
<div class="modal-content box-content">
<div class="modal-header" >
</div><!--end modal header -->
<div class="modal-body">
<?php //echo do_shortcode('[watcpus_verify_form]'); ?>
<?php
/**
* Variable product add to cart
*
* @author WooThemes
* @package WooCommerce/Templates
* @version 2.1.0
*/
global $woocommerce, $product, $post;
?>
<?php do_action( 'woocommerce_before_add_to_cart_form' ); ?>
<form class="variations_form cart" method="post" enctype='multipart/form-data' data-product_id="<?php echo $post->ID; ?>" data-product_variations="<?php echo esc_attr( json_encode( $available_variations ) ) ?>">
<?php if ( ! empty( $available_variations ) ) : ?>
<table class="variations" cellspacing="0">
<tbody>
<?php $loop = 0; foreach ( $attributes as $name => $options ) : $loop++; ?>
<tr>
<td class="label"><label for="<?php echo sanitize_title($name); ?>"><?php echo wc_attribute_label( $name ); ?></label></td>
<td class="value" style="display:none;"><select id="<?php echo esc_attr( sanitize_title( $name ) ); ?>" name="attribute_<?php echo sanitize_title( $name ); ?>">
<option value=""><?php// echo __( 'Choose an option', 'woocommerce' ) ?>&hellip;</option>
<?php
if ( is_array( $options ) ) {
if ( isset( $_REQUEST[ 'attribute_' . sanitize_title( $name ) ] ) ) {
$selected_value = $_REQUEST[ 'attribute_' . sanitize_title( $name ) ];
} elseif ( isset( $selected_attributes[ sanitize_title( $name ) ] ) ) {
$selected_value = $selected_attributes[ sanitize_title( $name ) ];
} else {
$selected_value = '';
}
// Get terms if this is a taxonomy - ordered
if ( taxonomy_exists( $name ) ) {
$orderby = wc_attribute_orderby( $name );
switch ( $orderby ) {
case 'name' :
$args = array( 'orderby' => 'name', 'hide_empty' => false, 'menu_order' => false );
break;
case 'id' :
$args = array( 'orderby' => 'id', 'order' => 'ASC', 'menu_order' => false, 'hide_empty' => false );
break;
case 'menu_order' :
$args = array( 'menu_order' => 'ASC', 'hide_empty' => false );
break;
}
$terms = get_terms( $name, $args );
foreach ( $terms as $term ) {
if ( ! in_array( $term->slug, $options ) )
continue;
echo '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $selected_value ), sanitize_title( $term->slug ), false ) . '>' . apply_filters( 'woocommerce_variation_option_name', $term->name ) . '</option>';
}
} else {
foreach ( $options as $option ) {
echo '<option value="' . esc_attr( sanitize_title( $option ) ) . '" ' . selected( sanitize_title( $selected_value ), sanitize_title( $option ), false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) ) . '</option>';
}
}
}
?>
</select> <?php
if ( sizeof( $attributes ) == $loop )
echo '<a class="reset_variations" href="#reset">' . __( 'Clear selection', 'woocommerce' ) . '</a>';
?></td>
</tr>
<?php endforeach;?>
</tbody>
</table>
<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<label><?php echo get_option('woo_watcpus_label'); ?></label>
<input type="text" name="woo_watcpus_search_zips" value="" />
<div class="single_variation_wrap" style="display:none;">
<?php do_action( 'woocommerce_before_single_variation' ); ?>
<style>
span.price {
display:none;
}
</style>
<div class="single_variation" style="display:none;"></div>
<div class="variations_button">
<?php //woocommerce_quantity_input(); ?>
<!-- <button type="submit" class="single_add_to_cart_button button alt"><?php// echo $product->single_add_to_cart_text(); ?></button> -->
</div>
<input type="hidden" name="add-to-cart" value="<?php echo $product->id; ?>" />
<input type="hidden" name="product_id" value="<?php echo esc_attr( $post->ID ); ?>" />
<input type="hidden" name="variation_id" value="" />
<input type="submit" name="woo_watcpus_process_zips" value="<?php _e('Verify'); ?>" class="btn btn-primary btn-icon alt"/>
<?php do_action( 'woocommerce_after_single_variation' ); ?>
</div>
<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
<?php else : ?>
<p class="stock out-of-stock"><?php _e( 'This product is currently out of stock and unavailable.', 'woocommerce' ); ?></p>
<?php endif; ?>
</form>
<?php do_action( 'woocommerce_after_add_to_cart_form' );
/**
* Simple product add to cart
*
* @author WooThemes
* @package WooCommerce/Templates
* @version 2.1.0
global $woocommerce, $product;
if ( ! $product->is_purchasable() ) return;
?>
<?php if ( $product->is_in_stock() ) : ?>
<?php do_action( 'woocommerce_before_add_to_cart_form' ); ?>
<form id="watcpus_form" method="post">
<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<label><?php echo get_option('woo_watcpus_label'); ?></label>
<input type="text" name="woo_watcpus_search_zips" value="" />
<?php wp_nonce_field('watcpus_nonce_action','watcpus_error_notice'); ?>
<input type="hidden" name="variation_id" value="" />
<input type="hidden" name="add-to-cart" value="<?php echo esc_attr( $product->id ); ?>" />
<input type="submit" name="woo_watcpus_process_zips" value="<?php _e('Verify'); ?>" class="btn btn-primary btn-icon alt"/>
<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
</form>
<?php do_action( 'woocommerce_after_add_to_cart_form' ); ?>
<?php endif; ?> */ ?>
</div><!-- end modal body -->
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div><!-- end modal footer -->
</div><!-- end modal content -->
</div><!-- end modal dialog -->
</div><!-- end modal purchaseWithOptions -->
<?php
/*
Plugin Name: WooCommerce Zip Code Restriction
Plugin URI: http://www.annexcore.com
Description: This plugin lets you restrict customers from purchasing your products by verifying their zip code when adding an item to a cart.
Version: 1.1
Author: AnnexCore
Author URI: http://www.annexcore.com
Requires at least: 3.8
Tested up to: 3.9
License: GNU General Public License v3.0
License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
function watcpus_woocommerce_fallback_notice() {
echo '<div class="error"><p>' . sprintf( __( 'WooCommerce Zip Code Restriction depends on %s to work!', 'watcpus' ), '<a href="http://wordpress.org/extend/plugins/woocommerce/">' . __( 'WooCommerce', 'watcpus' ) . '</a>' ) . '</p></div>';
}
function watcpus_run_on_plugins_loaded() {
// Checks with WooCommerce is installed.
if ( ! class_exists( 'WooCommerce' ) ) {
add_action( 'admin_notices', 'watcpus_woocommerce_fallback_notice' );
return;
}
}
add_action('plugins_loaded','watcpus_run_on_plugins_loaded');
/**
* Required functions
*/
if ( ! function_exists( 'woothemes_queue_update' ) ) {
require_once( 'woo-includes/woo-functions.php' );
}
if ( is_woocommerce_active() ) {
function watcpus_validation( $passed, $product_id, $quantity, $variation_id, $variations ) {
// Make $woocommerce accessable
global $woocommerce;
$zipsearch = $_POST['woo_watcpus_search_zips'];
$woo_watcpus_zips = explode(",",get_option("woo_watcpus_zips") );
// Check if the zip code is in our settings
if ( !in_array($zipsearch, $woo_watcpus_zips)) {
// Get the product title for the error statement
$product = get_product( $product_id );
$product_title = $product->post->post_title;
$sorrymsg = get_option('woo_watcpus_sorry');
// Add the error
wc_add_notice( $sorrymsg, $notice_type = 'error' );
return false;
// If falil :
} else {
// if succeed :
return true;
}
}
add_filter( 'woocommerce_add_to_cart_validation', 'watcpus_validation', 1, 5 );
// get path for templates used in loop ( like content-product.php )
function watcpus_get_template_file( $template, $slug, $name )
{
// Look in plugin/woocommerce/slug-name.php or plugin/woocommerce/slug.php
if ( $name ) {
$path = plugin_dir_path( __FILE__ ) . WC()->template_path() . "{$slug}-{$name}.php";
} else {
$path = plugin_dir_path( __FILE__ ) . WC()->template_path() . "{$slug}.php";
}
return file_exists( $path ) ? $path : $template;
}
add_filter( 'wc_get_template_part', 'watcpus_get_template_file', 10, 3 );
// get path for all other templates. -> uses woocommerce file directory structure
function watcpus_get_all_other_templates( $template, $template_name, $template_path )
{
$path = plugin_dir_path( __FILE__ ) . $template_path . $template_name;
return file_exists( $path ) ? $path : $template;
}
add_filter( 'woocommerce_locate_template', 'watcpus_get_all_other_templates', 10, 3 );
/* Backend Settings */
class WC_WACPUS {
public function __construct() {
$this->domain = 'woo-watcpus';
$this->current_tab = ( isset( $_GET['tab'] ) ) ? $_GET['tab'] : 'general';
/*
@TODO WPML
load_plugin_textdomain( $this->domain, false, basename( dirname( __FILE__ ) ) . '/languages' );
*/
add_action( 'woocommerce_settings_tabs', array( &$this, 'tab' ), 10 );
add_action( 'woocommerce_settings_tabs_woo_watcpus', array( &$this, 'settings_tab_action' ) , 10 );
add_action( 'woocommerce_update_options_woo_watcpus', array( &$this, 'save_settings' ) , 10 );
}
function tab() {
$class = 'nav-tab';
if ( $this->current_tab == 'woo_watcpus' ) $class .= ' nav-tab-active';
echo '<a href="' . admin_url( 'admin.php?page=woocommerce&tab=woo_watcpus' ) . '" class="' . $class . '">Zip Check</a>';
}
function init_form_fields() {
$this->form_fields = array(
array( 'name' => __( 'WooCommerce Add To Cart Pop Up Search', $this->domain ),
'type' => 'title',
'desc' => __( "This plugin lets you restrict customers from purchasing your products by verifying their zip code when adding an item to a cart.", $this->domain ),
'id' => 'about' ),
array( 'type' => 'sectionend', 'id' => 'about' ),
array( 'name' => __( 'Settings', $this->domain ),
'type' => 'title',
'desc' => ' ',
'id' => 'settings' ),
array(
'name' => __( 'Zip Codes', $this->domain ),
'desc' => __( 'Add zip codes here, seperate them by commas', $this->domain ),
'tip' => '',
'id' => 'woo_watcpus_zips',
'css' => '',
'std' => '',
'type' => 'textarea',
'args' => 'cols=60 rows=5',
'custom_attributes'=> array( 'cols' => 60, 'rows' => 5 ), //for WooCommerce 2.0
),
array(
'name' => __( 'Verification Message', $this->domain ),
'desc' => __( 'Text that will appear next to the verification box', $this->domain ),
'tip' => '',
'id' => 'woo_watcpus_label',
'class' => 'widefat',
'std' => '',
'type' => 'text'
),
array(
'name' => __( 'Zip Not Found Error Message', $this->domain ),
'desc' => __( 'Message that will show when a zip code is not found', $this->domain ),
'tip' => '',
'id' => 'woo_watcpus_sorry',
'class' => 'widefat',
'std' => '',
'type' => 'text'
)
);
}
function settings_tab_action() {
global $woocommerce_settings;
$current_tab = 'woo_watcpus';
// Display settings for this tab ( make sure to add the settings to the tab ).
$this->init_form_fields();
$woocommerce_settings[ $current_tab ] = $this->form_fields;
woocommerce_admin_fields( $woocommerce_settings[ $current_tab ] );
}
function save_settings() {
global $woocommerce_settings;
$current_tab = 'woo_watcpus';
$this->init_form_fields();
$woocommerce_settings[ $current_tab ] = $this->form_fields;
woocommerce_update_options( $woocommerce_settings[ $current_tab ] );
return true;
}
}
$GLOBALS['WC_WACPUS'] = new WC_WACPUS();
}// END PLUGIN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment