WooCommerce PHP: Display data from custom meta created in functions.php - 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?

Related

How to add a customizable text after WooCommerce add to cart button

I need to add custom text element after the add to cart button in WooCommerce.
I try with this code, that I insert in functions.php
add_action( 'woocommerce_after_add_to_cart_button', 'inserisce_testo_dopobottone' );
function inserisce_testo_dopobottone() {
echo '<div class="second_content">New text</div>';
}
That's works, but now i need that text is customizable for every page.
Is that possible?
You can add a custom field via the woocommerce_product_options_general_product_data action hook which will add a new field to the general tab of product data metabox
Based on this, you can show a different message per product, or a default message
So you get:
// Add to general_product_data tab
function action_woocommerce_product_options_general_product_data() {
// Text field
woocommerce_wp_text_input( array(
'id' => '_my_custom_text',
'label' => __( 'My custom text', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Add your custom text', 'woocommerce' ),
'desc_tip' => true,
));
}
add_action( 'woocommerce_product_options_general_product_data', 'action_woocommerce_product_options_general_product_data', 10, 0 );
// Save custom field
function action_woocommerce_admin_process_product_object( $product ) {
// Isset
if ( isset( $_POST['_my_custom_text'] ) ) {
// Update
$product->update_meta_data( '_my_custom_text', sanitize_text_field( $_POST['_my_custom_text'] ) );
}
}
add_action( 'woocommerce_admin_process_product_object', 'action_woocommerce_admin_process_product_object', 10, 1 );
// Display after add to cart button
function action_woocommerce_after_add_to_cart_button() {
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Get meta
$message = $product->get_meta( '_my_custom_text' );
// When Empty, use default message
if ( empty ( $message ) ) {
$message = __( 'Default text', 'woocommerce' );
}
echo '<div class="second_content">' . $message . '</div>';
}
}
add_action( 'woocommerce_after_add_to_cart_button', 'action_woocommerce_after_add_to_cart_button' );

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

Save and display a custom field in WooCommerce product description tab

I am building my first woocommerce site, Im learning how to create custom fields for products. I would like to create a text field in general tab, save that field and display on the front end to customers.
Here is the code I used to display the text field in the general tab of products.
function prefix_add_text_input() {
$args = array(
'label' =>__('Serial Number', 'woocommerce'), // Text in the label in the editor.
'placeholder' => '', // Give examples or suggestions as placeholder
'class' => '',
'style' => '',
'wrapper_class' => '',
'value' => '', // if empty, retrieved from post_meta
'id' => 'serial_number', // required, will be used as meta_key
'name' => '', // name will be set automatically from id if empty
'type' => '',
'desc_tip' => 'true',
'data_type' => '',
'custom_attributes' => '', // array of attributes you want to pass
'description' => 'Enter the serial number on your rifle here'
);
woocommerce_wp_text_input( $args );
}
How do I get the field to save, and display on the front end. Ideally display in the tabs with product description?
Here below you will find the way to save your product custom field value and display it in the product description tab section:
// Add a Custom product Admin Field
add_action( 'woocommerce_product_options_general_product_data', 'add_custom_product_general_field' );
function add_custom_product_general_field() {
echo '<div class="options_group">';
woocommerce_wp_text_input( array(
'id' => '_serial_number', // required, will be used as meta_key
'label' =>__('Serial Number', 'woocommerce'), // Text in the label in the editor.
'desc_tip' => 'true',
'description' => __('Enter the serial number on your rifle here', 'woocommerce')
) );
echo '</div>';
}
// Save the field value
add_action( 'woocommerce_admin_process_product_object', 'save_custom_product_general_field' );
function save_custom_product_general_field( $product ){
if( isset($_POST['_serial_number']) )
$product->update_meta_data( '_serial_number', sanitize_text_field( $_POST['_serial_number'] ) );
}
// Display the custom field value below product description tab
add_filter( 'the_content', 'display_product_serial_number' );
function display_product_serial_number( $content ) {
// Only for single product pages
if ( is_product() ) {
global $product;
if( $value = $product->get_meta( '_serial_number' ) ) {
$content .= '<p><strong>' . __("Serial number:", "woocommerce") . '<strong> ' . $value . '<p>';
}
}
return $content;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Show one WooCommerce Product meta

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.

Add and save admin product variations custom field in Woocommerce

So I've got the following code which makes me add a Barcode field to the Inventory Options of a product.
Now I also want to add this to each variations so I can easily add Variation Products when I scan the Barcode of the product via the WooCommerce Point of Sale plugin.
Here is what I got currently:
// Add Barcode field in simple product inventory options
add_action('woocommerce_product_options_sku','add_barcode',10,0);
function add_barcode(){
global $woocommerce,$post;
woocommerce_wp_text_input(
array(
'id' => '_barcode',
'label' => __('Barcode','woocommerce'),
'placeholder' => 'Scan Barcode',
'desc_tip' => 'true',
'description' => __('Scan barcode.','woocommerce'),
'value' => get_post_meta($post->ID,'_barcode',true)
)
);
}
// Save Barcode field value for simple product inventory options
add_action('woocommerce_process_product_meta','save_barcode',10,1);
function save_barcode($post_id){
if(!empty($_POST['_barcode']))
update_post_meta($post_id,'_barcode',sanitize_text_field($_POST['_barcode']));
}
// Add a Barcode field in product variations options
add_action('woocommerce_product_after_variable_attributes','add_barcode_variations',10,3);
function add_barcode_variations($loop,$variation_data,$variation){
woocommerce_wp_text_input(
array(
'id' => '_barcode[' . $variation->ID . ']',
'label' => __('Variation Barcode','woocommerce'),
'placeholder' => 'Scan Barcode',
'desc_tip' => 'true',
'description' => __('Scan barcode.','woocommerce'),
'value' => get_post_meta($variation->ID,'_barcode',true)
)
);
}
// Save Barcode field for product variations options
add_action( 'woocommerce_save_product_variation','save_barcode_variations',10,2);
function save_barcode_variations($post_id){
$barcode = $_POST['_barcode'][$post_id];
if(!empty($barcode)) update_post_meta($post_id,'_barcode',sanitize_text_field($barcode));
}
// Set POS Custom Code
add_filter('woocommerce_pos_barcode_meta_key','pos_barcode_field');
function pos_barcode_field(){
return '_barcode';
}
But the problem here is, that with that I now added a part for the variation, that if I update the product the main barcode field in the Inventory settings shows "Array" instead of the provided barcode.
I assume that this has something to do with the ID being the same for the variations as the original field other than the variationID at the end. The reason the ID requires to be the same as the WooCommerce POS plugin I'm using, is being filtered on that ID when I scan a product.
But currently can't figure out, to what I have to change to make both the Inventory Barcode Field and the Variation Barcode field to be saved properly.
As well as I'd like to add the variation field below the variation SKU field, but can't directly find the proper hook to do this.
Thanks in advance for further information.
In your last hooked function you have a missing argument, which is a similar to $loop argument in your 3rd function. So I have made little changes in your code:
// Add product Barcode custom field
add_action('woocommerce_product_options_sku','add_barcode_custom_field' );
function add_barcode_custom_field(){
woocommerce_wp_text_input( array(
'id' => '_barcode',
'label' => __('Barcode','woocommerce'),
'placeholder' => 'Scan Barcode',
'desc_tip' => 'true',
'description' => __('This is the Scan barcode field for this product.','woocommerce')
) );
}
// Save product Barcode custom field
add_action( 'woocommerce_process_product_meta', 'save_barcode_custom_field', 10, 1 );
function save_barcode_custom_field( $post_id ){
if( isset($_POST['_barcode']) )
update_post_meta( $post_id, '_barcode', esc_attr( $_POST['_barcode'] ) );
}
// Add Variation Barcode custom field
add_action( 'woocommerce_variation_options_pricing', 'add_barcode_variation_custom_field', 10, 3 );
function add_barcode_variation_custom_field( $loop, $variation_data, $variation ){
$variation_barcode = get_post_meta($variation->ID,"_barcode", true );
if( ! $variation_barcode ) $variation_barcode = "";
woocommerce_wp_text_input( array(
'id' => '_barcode['.$loop.']',
'label' => __('Variation Barcode','woocommerce'),
'placeholder' => 'Scan Barcode',
'desc_tip' => 'true',
'description' => __('This is the Scan barcode field for this variation.','woocommerce'),
'value' => get_post_meta($variation->ID,"_barcode", true ),
) );
}
// Save Variation Barcode custom field value
add_action( 'woocommerce_save_product_variation', 'save_barcode_variation_custom_field', 10, 2 );
function save_barcode_variation_custom_field( $variation_id, $i ){
if( isset($_POST['_barcode'][$i]) )
update_post_meta( $variation_id, '_barcode', sanitize_text_field($_POST['_barcode'][$i]) );
}
This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works for WooCommerce version 2.6+ and 3.0+

Categories