Show one WooCommerce Product meta - php

I am using one plugin for WooCommerce GST implementation. It has one HSN field inside product page which is stored in the database as the product meta.
The plugin does not have any specific code to get the HSN value and use it anywhere.
The code for HSN in the plugin is below:
public function fn_add_product_custom_meta_box() {
woocommerce_wp_text_input(
array(
'id' => 'hsn_prod_id',
'label' => __('HSN Code', 'woocommerce' ),
'description' => __( 'HSN Code is mandatory for GST.', 'woocommerce' ),
'custom_attributes' => array( 'required' => 'required' ),
'value' => get_post_meta( get_the_ID(), 'hsn_prod_id', true )
)
);
}
public function fn_save_license_field( $post_id ) {
$value = ( $_POST['hsn_prod_id'] )? sanitize_text_field( $_POST['hsn_prod_id'] ) : '' ;
update_post_meta( $post_id, 'hsn_prod_id', $value );
}
I want to get the HSN value by importing it from product meta and I have used this code to get that without any result:
<?php $meta = get_post_meta( get_the_ID(), 'hsn_prod_id', true ); ?>
Can anyone please check my code and correct me how to add the HSN value anywhere using the code?
I am using the code inside a different plugin to get the HSN code.
Thank you.

Related

WooCommerce PHP: Display data from custom meta created in functions.php

I recently created a custom field in functions.php to display a custom meta field in the Inventory tab for each product called barcode. What I now want to do is display the data onto my single product template using a PHP call. Here's first of all, the function to create the barcode data (which works):
function add_barcode(){
woocommerce_wp_text_input(
array(
'id' => '_barcode',
'label' => __( 'Barcode', 'your-plugin' ),
'placeholder' => 'Scan Barcode',
'desc_tip' => 'true',
'description' => __( "Scan the product's barcode.", "your-plugin" )
)
);
}
add_action('woocommerce_product_options_inventory_product_data','add_barcode');
function add_barcode_save( $product ){
if( isset( $_POST['_barcode'] ) ) {
$product->update_meta_data( '_barcode', sanitize_text_field( $_POST['_barcode'] ) );
} else {
$product->delete_meta_data( '_barcode' );
}
}
add_action( 'woocommerce_admin_process_product_object', 'add_barcode_save' );
Here's the PHP I'm trying to generate that data onto the page:
$product = wc_get_product();
echo '<strong>Barcode: </strong>' .$product->get_attribute( 'barcode' );
All I get here is a blank area after the HTML portion. So what function will allow me to display the barcode text?

woocommerce product attributes empty

I am having problems understanding the following:
I created a custom field in the woocommerce products by the code below.
The custome field shows up and I can fill it as expected.
I don't know how to access the data though.
if I call $product->get_attributes() it returns an empty array.
when I echo $product though I get an json like return value where all expected values are presented.
But how do I access them?
Thanks in advance
function
woocom_general_product_data_custom_field() {
// Create a custom text field
// Textarea
woocommerce_wp_textarea_input(
array(
'id' => '_textarea',
'label' => __( 'textarea' ),
'placeholder' => '',
'description' => __( '',
'woocommerce' )
)
);
}
add_action(
'woocommerce_process_product_meta',
'woocom_save_general_proddata_custom_field' );
function woocom_save_general_proddata_custom_field( $post_id ) {
// Save Textarea
$textarea = $_POST['_textarea'];
if( ! empty( $textarea ) ) {
update_post_meta( $post_id, '_textarea', esc_html( $textarea ) );
}
}
got it now.
I found the info in
$product->get_meta('_textarea');
not in the attributes

Change the order of custom field for Woocommerce variable products in admin area

I followed this tutorial for adding custom fields into WooCommerce variations.
http://www.remicorson.com/woocommerce-custom-fields-for-variations/
Everything worked great...
However the method for inserting the field into the dashboard (product edit) area doesn't mention how to change the order of the field as seen in the dashboard.
.
// Add Variation Settings
add_action( 'woocommerce_product_after_variable_attributes', 'variation_settings_fields', 10, 3 );
// Save Variation Settings
add_action( 'woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2 );
function variation_settings_fields( $loop, $variation_data, $variation ) {
// Text Field
woocommerce_wp_text_input(
array(
'id' => '_text_field[' . $variation->ID . ']',
'label' => __( 'My Text Field', 'woocommerce' ),
'placeholder' => 'http://',
'desc_tip' => 'true',
'description' => __( 'Enter the custom value here.', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_text_field', true )
)
);
}
function save_variation_settings_fields( $post_id ) {
// Text Field
$text_field = $_POST['_text_field'][ $post_id ];
if( ! empty( $text_field ) ) {
update_post_meta( $post_id, '_text_field', esc_attr( $text_field ) );
}
}
Any help would be much appreciated!
Using woocommerce_variation_options_pricing will put it after the price (it may need some CSS). It's the closest hook I found after price input.
Using woocommerce_variation_options will put it before price input
Following called hook in template is woocommerce_variation_options_inventory
In those cases, you can check yourself what is possible by:
finding the related Woocommerce template (in plugin directory)
studying the code to find do_action (or apply_filter for variables) to see "where and when you can do/update stuff". In your case wp-content/plugins/woocommerce/includes/admin/meta-boxes/views/html-variation-admin.php:159
sometimes your are lucky (merciful devs), sometimes less :)

Woocommerce coupons adding custom checkbox

I've gotten far enough on this simple function in functions.php that let's me add a checkbox to coupons. However, once I save/update a coupon, my checkbox value (check/unchecked) doesn't get committed (so the checkbox is always unchecked). In other words, I can't get it to update the value to yes in the meta_value column in postmetas when I update/save. The checkbox is there, I just can't use it... highly frustrating! Any sugestions on what I'm doing wrong, please :)
function add_coupon_revenue_dropdown_checkbox() {
$post_id = $_GET['post'];
woocommerce_wp_checkbox( array( 'id' => 'include_stats', 'label' => __( 'Coupon check list', 'woocommerce' ), 'description' => sprintf( __( 'Includes the coupon in coupon check drop-down list', 'woocommerce' ) ) ) );
$include_stats = isset( $_POST['include_stats'] ) ? 'yes' : 'no';
update_post_meta( $post_id, 'include_stats', $include_stats );
do_action( 'woocommerce_coupon_options_save', $post_id );
}add_action( 'woocommerce_coupon_options', 'add_coupon_revenue_dropdown_checkbox', 10, 0 );
The part I'm trying to affect is:
wp-content/plugins/woocommerce/includes/admin/meta-boxes/class-wc-meta-box-coupon-data.php
The problem with your code is that you are attempting to save the value of the checkbox in the same function where you generate the html for it. This won't work. You need to break your current function into two parts that run on two different WooCommerce hooks.
The first is to display the actual checkbox:
function add_coupon_revenue_dropdown_checkbox() {
woocommerce_wp_checkbox( array( 'id' => 'include_stats', 'label' => __( 'Coupon check list', 'woocommerce' ), 'description' => sprintf( __( 'Includes the coupon in coupon check drop-down list', 'woocommerce' ) ) ) );
}
add_action( 'woocommerce_coupon_options', 'add_coupon_revenue_dropdown_checkbox', 10, 0 );
The second is to save the value of the checkbox when the submitted form is being processed.
function save_coupon_revenue_dropdown_checkbox( $post_id ) {
$include_stats = isset( $_POST['include_stats'] ) ? 'yes' : 'no';
update_post_meta( $post_id, 'include_stats', $include_stats );
}
add_action( 'woocommerce_coupon_options_save', 'save_coupon_revenue_dropdown_checkbox');

WooComerce, if custom checkbox is checked hide stock status

I have a bit trouble with woocomerce checkbox, i add custom checkbox to product page wit this code:
woocommerce_wp_checkbox(
array(
'id' => '_checkbox',
'wrapper_class' => 'show_if_simple',
'label' => __('My Checkbox Field', 'woocommerce' ),
'description' => __( 'Check me!', 'woocommerce' )
)
);
}
then save value with this:
$woocommerce_checkbox = isset( $_POST['_checkbox'] ) ? 'yes' : 'no';
update_post_meta( $post_id, '_checkbox', $woocommerce_checkbox );
Now i tried to write function which make my stock status hiden when this checkbox is checked but i fail , can i ask you guys for some support ?
If the code for saving the option of checkbox works fine and the saved option for that products reflects in the database then adding the following code will help to to complete your task
add_filter('woocommerce_stock_html','wdm_remove_stock_html',10,3);
function wdm_remove_stock_html($availability_html, $availability, $product)
{
if ( 'yes' === get_post_meta( $product->id,'_checkbox', true) ) {
return '';
}else{
return $availability_html;
}
}

Categories