iTechMedium Uncategorized Create a Woocommerce product sold in units of kilogram and gram

Create a Woocommerce product sold in units of kilogram and gram

// Add a product custom field "grams_quantity" that will update the displayed price
add_action
('woocommerce_before_add_to_cart_button', 'special_product_by_grams', 25);
function special_product_by_grams(){
global $product;

// HERE Define the special product IDs sold by grams
$targeted_product_ids
= array(37);

// or HERE Define a product categories (ids, slugs or names)
$categories
= array('sold-by-gram');

// Only for products sold by gram
$product_id
= $product->get_id();
if ( ! ( in_array( $product_id, $targeted_product_ids ) || has_term( $categories, 'product_cat', $product_id ) ) ) return;
?>
<div class="grams-field">
<label for="grams_quantity"><?php _e('Kilograms: ','woocoomerce'); ?>&nbsp;&nbsp;&nbsp;&nbsp;<?php _e('Grams: ','woocoomerce'); ?><span></span><br>
<select name="kg_quantity" class="kg_quantity" id="kg_quantity">
<option value="0">Select Kilogram</option>
<option value="1000" selected>1 Kg</option>
<option value="2000">2 Kg</option>
<option value="3000">3 Kg</option>
<option value="4000">4 Kg</option>
<option value="5000">5 Kg</option>
<option value="10000">10 Kg</option>
<option value="20000">20 Kg</option>
<option value="50000">50 Kg</option>
</select>
<select name="grams_quantity" class="grams_quantity" id="grams_quantity">
<option value="0">Select Gram</option>
<option value="100">100 Gm</option>
<option value="200">200 Gm</option>
<option value="250">250 Gm</option>
<option value="500">500 Gm</option>
</select>
<!--<input type="number" step="1" name="grams_quantity" class="grams_quantity" id="grams_quantity" value="1">-->
</label>
</div><br>

<script type="text/javascript">
function cal_price(priceByGram){
updatedPrice = ((( parseInt(jQuery('#kg_quantity').val())+parseInt(jQuery('#grams_quantity').val())) * priceByGram)/1000).toFixed(2);
return updatedPrice;
}
(function($){
// variables initialization
var priceByGram = <?php echo wc_get_price_to_display( $product ); ?>,
currencySymbol = $(".woocommerce-Price-currencySymbol").html(),
updatedPrice;

// On live event: imput number fields
$('select#kg_quantity').on( "change", function(){
updatedPrice = cal_price(priceByGram);
$(".woocommerce-Price-amount.amount").html('<span class="woocommerce-Price-amount amount">'+updatedPrice+'&nbsp;'+currencySymbol+'</span>');
console.log("event"); // <== To be removed
});
$('select#grams_quantity').on( "change", function(){
updatedPrice = cal_price(priceByGram);
$(".woocommerce-Price-amount.amount").html('<span class="woocommerce-Price-amount amount">'+updatedPrice+'&nbsp;'+currencySymbol+'</span>');
console.log("event"); // <== To be removed
});
})(jQuery);
</script>

<?php
}

// Save the "grams_quantity" custom product field data in Cart item
add_filter
( 'woocommerce_add_cart_item_data', 'save_in_cart_the_custom_product_field', 10, 2 );
function save_in_cart_the_custom_product_field( $cart_item_data, $product_id ) {
if( isset( $_POST['grams_quantity'] ) ) {
$cart_item_data
[ 'grams_quantity' ] = $_POST['grams_quantity'];

// When add to cart action make an unique line item
$cart_item_data
['unique_key'] = md5( microtime().rand() );
WC
()->session->set( 'custom_data', $_POST['grams_quantity'] );
}
return $cart_item_data;
}

// Update product price by grams in cart and checkout
add_filter
( 'woocommerce_before_calculate_totals', 'update_prices_by_gram', 10, 1 );
function update_prices_by_gram( $cart_object ) {

// HERE Define the special product IDs sold by grams
$targeted_product_ids
= array(37);

// or HERE Define a product categories (ids, slugs or names)
$categories
= array('sold-by-gram');

foreach ( $cart_object->get_cart() as $cart_item ) {

// Only for products sold by gram
$product_id
= $cart_item['product_id'];
if ( in_array( $product_id, $targeted_product_ids ) || has_term( $categories, 'product_cat', $product_id ) ){

// Get an instance of the WC_Product object and the
$product
= $cart_item['data'];

$grams
= $cart_item['grams_quantity'];

// Method is_on_sale() manage everything (dates…)
$product
->set_price( $product->get_price() * $grams);
}
}
}

// Render "grams_quantity" the custom product field in cart and checkout
add_filter
( 'woocommerce_get_item_data', 'render_product_custom_field_meta_on_cart_and_checkout', 10, 2 );
function render_product_custom_field_meta_on_cart_and_checkout( $cart_data, $cart_item ) {
$custom_items
= array();

if( !empty( $cart_data ) )
$custom_items
= $cart_data;

if( isset( $cart_item['grams_quantity'] ) )
$custom_items
[] = array(
'name' => __( 'Grams', 'woocommerce' ),
'value' => sanitize_text_field( $cart_item['grams_quantity'] ),
'display' => sanitize_text_field( $cart_item['grams_quantity'] ),
);

return $custom_items;
}

// Save "grams_quantity" to the order items meta data
add_action
('woocommerce_add_order_item_meta','add_product_custom_fiel_to_order_item_meta', 1, 3 );
function add_product_custom_fiel_to_order_item_meta( $item_id, $item_values, $item_key ) {

if( isset( $item_values['grams_quantity'] ) )
wc_update_order_item_meta
( $item_id, 'Grams', sanitize_text_field( $item_values['grams_quantity'] ) );
}

3 thoughts on “Create a Woocommerce product sold in units of kilogram and gram”

Leave a Reply

Your email address will not be published. Required fields are marked *