Skip to content

Instantly share code, notes, and snippets.

@LiamBailey
Created March 17, 2016 20:30
Show Gist options
  • Save LiamBailey/9e363313493e1883f54d to your computer and use it in GitHub Desktop.
Save LiamBailey/9e363313493e1883f54d to your computer and use it in GitHub Desktop.
Woocommerce ID Numbers
jQuery(function($) {
$("a.add_to_cart_button").after("<span class='item_cost_holder'></span>");
$(".single_add_to_cart_button").after("<span id='item_cost_holder'></span>");
$(".summary form.cart .quantity").remove();
$(".wswp_id_numbers_field").keypress(function(event) {
if ( $(this).closest('li').find(".add_to_cart_button").length > 0) {
$(this).closest('li').find(".add_to_cart_button").hide();
}
if(event.which != 8 && event.which != 32 && isNaN(String.fromCharCode(event.which))){
event.preventDefault(); //stop character from entering input
}
});
function get_cost_and_numbers(e,type) {
var __this = (type == 'target') ? $(e.target) : e;
var price = (__this.parent().prev().find('.amount').length > 0) ? parseFloat(__this.parent().prev().find('.amount').text().replace(wooIDNumVars.currency_symbol,'')) : parseFloat($(".summary .price .amount").text().replace(wooIDNumVars.currency_symbol,''));
var $return = {};
$return['number_string'] = __this.val();
$return['numbers'] = $return['number_string'].split(" ");
var cost = price * $return['numbers'].length;
$return['cost'] = (Math.round(cost * 100) / 100).toFixed(2);
$return['cost_string'] = wooIDNumVars.currency_symbol + $return['cost'];
return $return;
}
$(".wswp_id_numbers_field").change(function(event) {
if ($(this).val().match(/[0-9][0-9]/)) {
alert("Please enter single numbers seperated by space");
$(this).val($(this).val().replace(/[0-9][^\s]/g,''));
}
var vals = get_cost_and_numbers(event,'target');
if ($(".single_add_to_cart_button").length > 0) {
$("#item_cost_holder").text(vals['cost_string']);
$("input#item_cost").val(vals['cost']);
}
else if ($(".add_to_cart_button").length > 0) {
$(this).closest('li').find(".add_to_cart_button").show();
$(this).closest('li').find(".add_to_cart_button").data('item_cost',vals['cost']);
$(this).closest('li').find(".add_to_cart_button").data('id_numbers',vals['number_string']);
$(this).closest('li').find(".item_cost_holder").text(vals['cost_string']);
}
});
})
<?php
/*
Plugin Name: Woocommerce ID Numbers
Plugin URI: Custom functions for selling multiple numbers
Description:
Version: 1.0.0
Author: Webby Scots
Author URI: http://webbyscots.com/
*/
add_filter('wp_footer','wswp_custom_footer');
function wswp_custom_footer() {
?><style type="text/css">
#id_numbers_field .description {
font-size:0.5em;
display:block;width:145px
}
#id_numbers_field {
line-height:1;
margin-bottom:0
}
#id_numbers_field input, #item_cost {
color:red
}
.summary form.cart .quantity {
display:none
}
</style><?php
}
add_action('woocommerce_form_field_hidden','wswp_hidden_field',10,4);
function wswp_hidden_field($field,$key,$args,$value) {
$field .= '<input type="hidden" class="input-text ' . esc_attr( implode( ' ', $args['input_class'] ) ) .'" name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" '.$args['maxlength'].' value="' . esc_attr( $value ) . '" />';
return $field;
}
//add_filter('loop_shop_columns','wswp_shop_loop_columns');
function wswp_shop_loop_columns($no) {
return 3;
}
add_action('woocommerce_after_shop_loop_item','wswp_add_input_field',8);
add_action('woocommerce_before_add_to_cart_button','wswp_add_input_field',29);
function wswp_add_input_field() {
echo woocommerce_form_field('id_numbers', array(
'type' => 'text',
'input_class' => array('wswp_id_numbers_field'),
'placeholder' => 'Enter your numbers here',
'description' => 'Seperate numbers with a space'
));
echo woocommerce_form_field('item_cost', array(
'type' => 'hidden',
));
}
add_action('wp_enqueue_scripts','wswp_add_scripts');
function wswp_add_scripts() {
wp_enqueue_script('id_numbers',plugins_url('/js/script.js',__FILE__),array('jquery'));
wp_localize_script('id_numbers','wooIDNumVars',array(
'currency_symbol' => get_woocommerce_currency_symbol(),
'ajax_url' => admin_url('admin-ajax.php')
));
}
add_action('woocommerce_add_to_cart','wswp_add_to_cart',50,6);
function wswp_add_to_cart($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) {
session_start();
if (isset($_SESSION['id_numbers'])) {
$_REQUEST['id_numbers'] = $_SESSION['id_numbers'];
unset($_SESSION['id_numbers']);
$_REQUEST['item_cost'] = $_SESSION['item_cost'];
unset($_SESSION['item_cost']);
}
if (isset($_REQUEST['item_cost'])) {
$item_cost = floatval($_REQUEST['item_cost']);
$price = WC()->cart->cart_contents[$cart_item_key]['data']->price;
$quantity = round($item_cost / $price);
WC()->cart->cart_contents[$cart_item_key]['quantity'] = $quantity;
WC()->cart->cart_contents[$cart_item_key]['chosen_numbers'] = $_REQUEST['id_numbers'];
WC()->cart->calculate_totals();
}
}
//add_filter('woocommerce_add_to_cart_validation','wswp_ajax_add_to_cart');
function wswp_ajax_add_to_cart($bool) {
session_start();
if (isset($_REQUEST['id_numbers']) || isset($_SESSION['id_numbers']))
return $bool;
$_REQUEST['id_numbers'] = $_SESSION['id_numbers'];
$_REQUEST['item_cost'] = $_SESSION['item_cost'];
return $bool;
}
add_filter('woocommerce_get_item_data','wswp_get_item_data',100,2);
function wswp_get_item_data($data,$cart_item) {
if (isset($cart_item['chosen_numbers'])) {
$data[]['name'] = __("Numbers","woocommerce");
$data[]['value'] = $cart_item['chosen_numbers'];
}
return $data;
}
add_action('woocommerce_add_order_item_meta','wswp_order_item_meta',10,3);
function wswp_order_item_meta($item_id, $values, $cart_item_key) {
if (isset($values['chosen_numbers'])) {
wc_add_order_item_meta($item_id,'chosen_numbers',$values['chosen_numbers'],true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment