Last active
September 9, 2017 18:56
-
-
Save amitabhaghosh197/d670f497e87572cb2419 to your computer and use it in GitHub Desktop.
woocommerce-codes #wordpress #woocommerce #important
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
// [Ref:][http://www.remicorson.com/mastering-woocommerce-products-custom-fields/] | |
/*=================================================================== | |
== FUNCTIONS FOR FREE PRODUCT & TOTAL PRODUCT | |
====================================================================*/ | |
/****************CUSTOM FIELD FOR CUSTOM GENERAL FIELD**************************************************/ | |
// Add Free Number Custom Field in Woocommerce | |
//Display Field | |
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' ); | |
// Save Fields | |
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' ); | |
function woo_add_custom_general_fields() { | |
global $woocommerce, $post; | |
echo '<div class="options_group">'; | |
woocommerce_wp_text_input( | |
array( | |
'id' => 'free_offer', | |
'label' => __( '<strong style="color:#239804">Your Free Products</strong>', 'woocommerce' ), | |
'placeholder' => '', | |
'description' => __( 'Please enter a number', 'woocommerce' ), | |
'type' => 'number', | |
'custom_attributes' => array( | |
'step' => 'any', | |
'min' => '0' | |
) | |
) | |
); | |
woocommerce_wp_text_input( | |
array( | |
'id' => 'offer_text_field', | |
'label' => __( '<strong>Your Offer</strong>', 'woocommerce' ), | |
'placeholder' => '', | |
'desc_tip' => 'true', | |
'description' => __( 'Enter Your 1st Offer Info.', 'woocommerce' ) | |
) | |
); | |
woocommerce_wp_text_input( | |
array( | |
'id' => 'next_text_field', | |
'label' => __( '<strong>Your Other Offers</strong>', 'woocommerce' ), | |
'placeholder' => '', | |
'desc_tip' => 'true', | |
'description' => __( 'Enter Your 2nd Offer Info.', 'woocommerce' ) | |
) | |
); | |
echo '</div>'; | |
}//woo_add_custom_general_fields | |
function woo_add_custom_general_fields_save( $post_id ){ | |
//Number Field | |
$woocommerce_number_field = $_POST['free_offer']; | |
if( !empty( $woocommerce_number_field ) ) | |
update_post_meta( $post_id, 'free_offer', esc_attr( $woocommerce_number_field ) ); | |
// Text Field | |
$woocommerce_text_field = $_POST['offer_text_field']; | |
update_post_meta( $post_id, 'offer_text_field', esc_attr( $woocommerce_text_field ) ); | |
// Text Field | |
$woocommerce_text_field = $_POST['next_text_field']; | |
update_post_meta( $post_id, 'next_text_field', esc_attr( $woocommerce_text_field ) ); | |
}//woo_add_custom_general_fields_save( $post_id ) | |
/*================================================================ | |
Function for front-end in cart.php for free products | |
=================================================================*/ | |
function free_products(){ | |
global $woocommerce ,$product, $post; | |
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { | |
$my_var = $cart_item['product_id']; | |
$free_number = get_post_meta( $my_var, 'free_offer', true ); | |
$free_product = $cart_item['quantity'] * $free_number; | |
echo apply_filters( 'woocommerce_cart_item_quantity', $free_product, $cart_item_key ); | |
} | |
} | |
/*================================================================ | |
Function for front-end in cart.php for Total products | |
=================================================================*/ | |
function total_products(){ | |
global $woocommerce; | |
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { | |
$my_var = $cart_item['product_id']; | |
$free_number = get_post_meta( $my_var, 'free_offer', true ); | |
$item_quantity = $cart_item['quantity']; | |
$free_product = $item_quantity * $free_number; | |
$total_product = $item_quantity + $free_product; | |
echo apply_filters( 'woocommerce_cart_item_quantity', $total_product, $cart_item_key ); | |
} | |
} |
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
<?php // This you will find in includes/class-wc-form-handler.php ?> | |
<div> | |
<a href="<?php esc_url( get_permalink( wc_get_page_id( 'myaccount' ) ) ); ?>"> Register</a> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment