Get Post meta to display on invoice in WordPress - woocommerce

I'm using a plugin called woo-get to add a product attribute called 'prod_hsn_id' which add a filed called HSN code at product edit page,
I'm also using a pdf invoice plugin called woocommerce pdf invoice to generate pdf invoice.
Now I want to display the HSN code on the invoice.
I am having a very hard time to get it to work, I tried searching online and contacting the plugin author, and he said that it can be retrieved using the WordPress get post meta.
But
This the function the plugin use to create the custom field.
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 )
)
);
}
In the Invoice Template file I'm trying to display the HSN Code using
<?php $meta = get_post_meta( $post_id , 'hsn_prod_id', true ); ?>
<?php $meta = get_post_meta( get_the_ID(), 'hsn_prod_id', true ); ?>
Sources :
Support
1. https://wordpress.org/support/topic/print-hsn-in-invoice/
https://wordpress.org/support/topic/which-pdf-plugin-will-display-the-hsn-field-and-gst-number/

Product details will be under line_items of order. Can you share your invoice template code where you are trying to access product id?
You will have order object, using that object you can retrieve all products related to that order. You need to execute on foreach loop (that should be there already).
You can see $product at line 77 of original template file of plugin. You can get id using $product->get_id().

Related

Custom woocommerce product field not working for CSV import

I've created a custom field for my products this way:
// The code for displaying WooCommerce Product Custom Fields
add_action( 'woocommerce_product_options_general_product_data', 'woocommerce_product_custom_fields' );
// Following code Saves WooCommerce Product Custom Fields
add_action( 'woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save' );
function woocommerce_product_custom_fields () {
global $woocommerce, $post;
echo '<div class=" product_custom_field ">';
// Custom Product Number Field
woocommerce_wp_text_input(
array(
'id' => '_shipping_days_field',
'placeholder' => 'Días de entrega',
'label' => __('Días estimados de entrega', 'woocommerce'),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '1'
)
)
);
echo '</div>';
}
function woocommerce_product_custom_fields_save($post_id){
// Custom Product Number Field
$shipping_days = $_POST['_shipping_days_field'];
if (!empty($shipping_days))
update_post_meta($post_id, '_shipping_days_field', esc_attr($shipping_days));
}
This is working ok on the frontend admin it shows the field and saves it, but it is not working for the CSV impornt, I cannot specify the field in my CSV mapping. Is there a way I can create custom field that works for CSV import as well?
I actually solved it, on the mapping you just should select Meta Attributes, and the label on the CSV column must be as follows:
meta:your_custom_field

ACF Front Forms and Multsite

I have a 2x blog WordPress Multisite that contains a front end ACF form for adding dates.
I can then update these dates whenever required from the front end.
I want to load the front end form on the second blog so that I can also update the dates there.
I can load the form but the values aren't saved; whereas they are only the main blog site.
How do I get around this?
<?php
$options = array(
'id' => 'update_dates_form',
'fields' => array(
'field_591c745961034',
'field_591c74a861037',
'field_591c748361035',
'field_591c749a61036',
),
'submit_value' => __('Save', 'augwp'),
'updated_message' => __('Successfully Updated', 'augwp'),
);
acf_form( $options );
?>
In your code, yiu have to switch to blog where data are stored, extract them then return to the current blog:
switch_to_blog(1); // use blog number here, main blog is always 1
// extract data
restore_current_blog();

How to add short description in WooCommerce checkout page

Thanks for reading, Just had a issue regarding WooCommerce, I want to add a short description checkout page of below billing field.
How to add short description in WooCommerce checkout page of below billing field?
I tried add function, custom code but failed with error.
add_filter( 'woocommerce_get_item_data', 'wc_checkout_description_so_27900033', 10, 2 );
function wc_checkout_description_so_27900033( $other_data, $cart_item )
{
$post_data = get_post( $cart_item['product_id'] );
$other_data[] = array( 'name' => 'description', 'value' => $post_data->post_excerpt );
return $other_data;
}
I was used this code but it is showing inner product info table.
There's no real reason to call get_post(). The $product object is stored in the $cart_item array and the $post object is stored inside the $product. This gets the product's excerpt (aka the short description) to show up in the cart and in the checkout. Now, it isn't likely the make the description show up on the order received page, or in the my account area, or in emails, etc since the only place that the woocommerce_get_item_data filter appears is in the cart class.
One thing to take note of, WooCommerce 2.7 is a major rewrite of WooCommerce and $_product->post->post_excerpt will result in PHP notices about directly accessing product properties. So I've suggested both the 2.6 and 2.7 compatible approaches.
add_filter( 'woocommerce_get_item_data', 'wc_checkout_description_so_27900033', 10, 2 );
function wc_checkout_description_so_27900033( $other_data, $cart_item )
{
$_product = $cart_item['data'];
// Use this for WC2.7
//$other_data[] = array( 'name' => 'description', 'value' => $_product->get_short_description() );
// Use this for WC2.6
$other_data[] = array( 'name' => 'description', 'value' => $_product->post->post_excerpt );
return $other_data;
}

WooCommerce: add a few lists to wishlist by custom plugin

I have custom plugin for first adding lists to Wishlist plugin (Woocommerce) by users.
I have custom step by step form, where user can choose number of lists (from 1 to 10) and enter titles and descriptions for these new lists.
There is Ajax request on the last step of my form.
How do I add these lists to database?
I'm trying to add by wp_insert_post( $my_post ) but I should add settings for postmeta table too.
You could save the form results as custom post type with the results as custom fields.
Set the post type as not public and not set to not be in search results.
If they is no logic preformed on the wishlist you could set the data to array, and save it serialized in one field.
To send the data from front end (user page) to back end (server) you could or use wp ajax admin or through wp-rest api
Save fields to custom post type example. Could be the field name is different in your site so set to according to your fields key
// Create post object
$my_post = array(
'post_title' => wp_strip_all_tags( $_POST['post_title'] ),
'post_content' => $_POST['post_content'],
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array( 8,39 ),
'post_type' => 'event'
);
// Insert the post into the database
$post_id = wp_insert_post( $my_post );
// Updating the meta data (custom fields values)
if ( isset( $_POST['_wishlist_email'] ) ) {
update_post_meta( $post_id, '_wishlist_email', sanitize_text_field( $_POST['_wishlist_email'] ) );
}
More info about saving custom fields in docs
If this wishlist is from a ready plugin, you could look in the plugin code to see how the plugin handles the saving wishlist data.
I have found:
WC_Wishlists_Wishlist::create_list($tittle));

How to find your action in admin-ajax.php file?

I am using woo commerce plugin for my eCommerce website. There are some order listed on my dashboard. When I change status of an order from processing to completed we click on button.
Here is my action goes:
if ( in_array( $the_order->post_status, array('wc-pending', 'wc-on-hold', 'wc-processing') ) )
$actions['complete'] = array(
'url' => wp_nonce_url( admin_url( 'admin-ajax.php?action=upen-mark-order-complete&order_id=' . $the_order->id ), 'upen-mark-order-complete' ),
'name' => __( 'Dispatched', 'dokan' ),
'action' => "complete",
'icon' => '<i class="fa fa-truck"> </i>'
);
But when I am looking into admin-ajax.php file there is no order_id getting. I am confused that where these attributes are we getting to change order status like action, order_id etc.
You should read wp_ajax_(action).
based on your given code, there should be something like this implemented somewhere... maybe on your theme files or plugin files..
add_action( 'wp_ajax_upen-mark-order-complete', 'my_action_callback' );
function my_action_callback() {
$order_id = $_GET['order_id'];
wp_die();
}
additionally,
With notepad++ you can search it in your files like this..
Please try this in your themes functions.php
function mysite_woocommerce_order_status_completed( $order_id ) {
echo $order_id; exit; // you will get order ID here , when you are updating order status to completed.
}
add_action( 'woocommerce_order_status_completed','mysite_woocommerce_order_status_completed' );

Resources