Last active
March 23, 2021 09:20
-
-
Save trongcong/c3ce527ffe691288e3c28d5117dd17e7 to your computer and use it in GitHub Desktop.
Add Custom Product Pricing Form in Woocommerce WordPress https://ntcde.com/wordpress/them-form-tinh-gia-san-pham-trong-woocommerce-wordpress.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//============ | |
//Add woo custom field | |
add_action( 'woocommerce_product_options_advanced', 'ntc_add_custom_field_product_dashboard_advanced_tab' ); | |
function ntc_add_custom_field_product_dashboard_advanced_tab() { | |
global $post; | |
$custom_price_form_product_option = get_post_meta( $post->ID, '_custom_price_form_of_product', true ); | |
echo '<div class="options_group ">'; | |
// Checkbox Field | |
woocommerce_wp_checkbox( array( | |
'id' => '_custom_price_form_of_product', | |
'description' => __( 'Show custom price form of product', 'woocommerce' ), | |
'label' => __( 'Show custom price form of product', 'woocommerce' ), | |
'value' => $custom_price_form_product_option, | |
) ); | |
echo '</div>'; | |
} | |
//Save data | |
add_action( 'woocommerce_process_product_meta', 'ntc_save_custom_product_fields' ); | |
function ntc_save_custom_product_fields( $post_id ) { | |
$_custom_price_form_of_product = isset( $_POST['_custom_price_form_of_product'] ) ? 'yes' : 'no'; | |
//========= | |
update_post_meta( $post_id, '_custom_price_form_of_product', esc_attr( $_custom_price_form_of_product ) ); | |
} | |
//show to frontend | |
add_action( 'woocommerce_before_add_to_cart_button', 'ntc_show_custom_input_field_to_single_product', 0 ); | |
function ntc_show_custom_input_field_to_single_product() { | |
global $post; | |
$custom_price_form_product_option = get_post_meta( $post->ID, '_custom_price_form_of_product', true ); | |
$product = wc_get_product( $post->ID ); | |
//Check is product and form is enable -> show on single product | |
if ( is_product() && $custom_price_form_product_option == 'yes' ) { ?> | |
<table id="ntc_price_calculator"> | |
<tbody> | |
<tr class="price-table-row length-input"> | |
<td> | |
<label for="length_value">Chiều dài</label> | |
</td> | |
<td style="text-align:right;"> | |
<div class="input-wrap"> | |
<input type="number" name="length_value" id="length_value" class="amount_needed" autocomplete="off" required> | |
</div> | |
</td> | |
</tr> | |
<tr class="price-table-row width-input"> | |
<td> | |
<label for="width_value">Chiều rộng</label> | |
</td> | |
<td style="text-align:right;"> | |
<div class="input-wrap"> | |
<input type="number" name="width_value" id="width_value" class="amount_needed" autocomplete="off" required> | |
</div> | |
</td> | |
</tr> | |
<tr class="price-table-row total-row"> | |
<td>Thành tiền</td> | |
<td style="text-align:right;"><span class="standard-total" data-product-price="<?php echo $product->get_price(); ?>">0</span></td> | |
</tr> | |
</tbody> | |
</table> | |
<?php | |
// custom js form | |
echo "<script> | |
jQuery(document).ready(function() { | |
jQuery('#length_value, #width_value').on('change', function (e) { | |
let length = jQuery('#length_value').val(), | |
width = jQuery('#width_value').val(), | |
product_price = jQuery('.standard-total[data-product-price]').data('product-price'); | |
length =length? parseFloat(length):0; | |
width =width? parseFloat(width):0; | |
product_price = parseFloat(product_price); | |
let total = length*width*product_price; | |
jQuery('.standard-total[data-product-price]').html((length*width)+'m<sup>2</sup> -- '+total.toLocaleString('vi-VN') +'₫'); | |
console.log(length, width,product_price,total.toLocaleString('vi-VN')); | |
}) | |
}); | |
</script>"; | |
} | |
} | |
// Set custom field and calculated price as custom cart data in the cart item | |
add_filter( 'woocommerce_add_cart_item_data', 'ntc_save_custom_data_in_cart_object', 30, 3 ); | |
function ntc_save_custom_data_in_cart_object( $cart_item_data, $product_id, $variation_id ) { | |
if ( ! isset( $_POST['length_value'] ) || ! isset( $_POST['width_value'] ) ) { | |
return $cart_item_data; | |
} | |
$length_value = isset( $_POST['length_value'] ) ? floatval( $_POST['length_value'] ) : 0; | |
$width_value = isset( $_POST['width_value'] ) ? floatval( $_POST['width_value'] ) : 0; | |
$lVal = floatval( $length_value < 0 ? $length_value * - 1 : $length_value ); | |
$wVal = floatval( $width_value < 0 ? $width_value * - 1 : $width_value ); | |
$product = wc_get_product( $product_id ); | |
$product_price = floatval( $product->get_price() ); | |
$price_total = $lVal * $wVal * $product_price; | |
$cart_item_data['custom_data']['price'] = $price_total; | |
$cart_item_data['custom_data']['length'] = $length_value . ' (m)'; | |
$cart_item_data['custom_data']['width'] = $width_value . ' (m)'; | |
return $cart_item_data; | |
} | |
// Set the new calculated price of the cart item | |
add_action( 'woocommerce_before_calculate_totals', 'ntc_calculate_product_fee', 99, 1 ); | |
function ntc_calculate_product_fee( $cart ) { | |
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) { | |
return; | |
} | |
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) { | |
return; | |
} | |
foreach ( $cart->get_cart() as $cart_item ) { | |
if ( isset( $cart_item['custom_data']['price'] ) ) { | |
// Get the new calculated price | |
$new_price = (float) $cart_item['custom_data']['price']; | |
// Set the new calculated price | |
$cart_item['data']->set_price( $new_price ); | |
} | |
} | |
} | |
//Show price in cart item | |
add_filter( 'woocommerce_cart_item_product', 'ntc_woo_filter_cart_item_product', 20, 3 ); | |
function ntc_woo_filter_cart_item_product( $cart_data, $cart_item, $cart_item_key ) { | |
if ( isset( $cart_item['custom_data']['price'] ) ) { | |
// Get the new calculated price | |
$new_price = (float) $cart_item['custom_data']['price']; | |
// Set the new calculated price | |
$cart_data->set_price( $new_price ); | |
} | |
return $cart_data; | |
} | |
// Display Custom text in cart and checkout pages | |
add_filter( 'woocommerce_get_item_data', 'ntc_render_meta_on_cart_and_checkout', 99, 2 ); | |
function ntc_render_meta_on_cart_and_checkout( $cart_data, $cart_item = null ) { | |
if ( isset( $cart_item['custom_data']['length'] ) ) { | |
$cart_data[] = array( "name" => "Length", "value" => $cart_item["custom_data"]["length"] ); | |
} | |
if ( isset( $cart_item['custom_data']['width'] ) ) { | |
$cart_data[] = array( "name" => "Width", "value" => $cart_item["custom_data"]["width"] ); | |
} | |
return $cart_data; | |
} | |
// Save the custom text as order item data (displaying it in order and notifications) | |
add_action( 'woocommerce_add_order_item_meta', 'ntc_order_meta_handler', 99, 3 ); | |
function ntc_order_meta_handler( $item_id, $values, $cart_item_key ) { | |
if ( isset( $values['custom_data']['length'] ) ) { | |
wc_add_order_item_meta( $item_id, "Length", $values["custom_data"]["length"] ); | |
} | |
if ( isset( $values['custom_data']['width'] ) ) { | |
wc_add_order_item_meta( $item_id, "Width", $values["custom_data"]["width"] ); | |
} | |
} | |
//End woo custom field |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment