Woocommerce coupons adding custom checkbox - php

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');

Related

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 :)

Replace product "on backorder" to a custom field value in Woocommerce

Firstly, thanks for viewing this question.
I've searched and gone through many similar questions however i've not managed to find a perfect fix.
Im setting up a website using wordpress/woocommerce, however most of our products have a set lead time therefore everything is on "back order - allow" status. Instead of showing "on backorder" on each product page, I wanted to see if it was possible to create a custom field in each product and replace the "on backorder" text to show that custom field.
Currently, i've been using the following code that just changes the text for every product however, not all products are on that specific lead time.
add_filter( 'woocommerce_get_availability', 'backorder_text', 10, 2);
function backorder_text($available) {
return str_replace('Available on backorder', 'Approx lead time: 2-4 working weeks', $available);
}
I appreciate I would need to set up a custom field in each product with the set time, but i'm not entirely sure how to link that specfic custom field per each product to that php code (or rather, whether its actually possible).
Any help would be fantastic - even if its to tell me it can't be done!
This can be done with the following code, that will handle products and product variation too:
// Add a custom field in admin product edit pages - inventory tab
add_action( 'woocommerce_product_options_stock_fields', 'add_product_options_stock_custom_field', 20 );
function add_product_options_stock_custom_field() {
global $product_object, $post;
woocommerce_wp_text_input( array(
'id' => '_backorder_text',
'type' => 'text',
'label' => __( 'Backorders text', 'woocommerce' ),
'description' => __( 'Backorders text. Add a custom backorders text to be displayed when products are on backorders.', 'woocommerce' ),
'desc_tip' => true,
) );
// jQuery: HIDE the fied if backorders are not enabled
?>
<script type="text/javascript">
jQuery( function($){
var a = 'select#_backorders',
b = 'p._backorder_text_field';
if( $(a).val() === 'no' )
$(b).hide();
$(a).on('change blur', function(){
if( $(a).val() === 'no' )
$(b).hide();
else
$(b).show();
});
});
</script>
<?php
}
// Save the custom field value from admin product edit pages - inventory tab
add_action( 'woocommerce_process_product_meta', 'save_product_options_stock_custom_field', 20, 1 );
function save_product_options_stock_custom_field( $product_id ) {
if ( isset( $_POST['_backorder_text'] ) )
update_post_meta( $product_id, '_backorder_text', sanitize_text_field( $_POST['_backorder_text'] ) );
}
// Variations: Add a custom field in admin variation options inventory
add_action( 'woocommerce_variation_options_inventory', 'add_variation_settings_fields', 20, 3 );
function add_variation_settings_fields( $loop, $variation_data, $variation_post ) {
woocommerce_wp_text_input( array(
'id' => '_backorder_text'.$loop,
'name' => '_backorder_text['.$loop.']',
'value' => get_post_meta( $variation_post->ID, '_backorder_text', true ),
'type' => 'text',
'label' => __( 'Backorders text', 'woocommerce' ),
'description' => __( 'Backorders text. Add a custom backorders text to be displayed when products are on backorders.', 'woocommerce' ),
'desc_tip' => true,
'wrapper_class' => 'form-row form-row-first',
) );
}
// Variations: Save a custom field value from admin variation options inventory
add_action( 'woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2 );
function save_variation_settings_fields( $variation_id, $i ) {
if( isset( $_POST['_backorder_text'][$i] ) )
update_post_meta( $variation_id, '_backorder_text', sanitize_text_field( $_POST['_backorder_text'][$i] ) );
}
add_filter( 'woocommerce_get_availability', 'custom_on_backorder_text', 10, 2 );
function custom_on_backorder_text( $availability, $product ) {
$backorder_text = get_post_meta( $product->get_id(), '_backorder_text', true );
if( $availability['class'] === 'available-on-backorder' && ! empty( $backorder_text ) )
$availability['availability'] = $backorder_text;
return $availability;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
For all products (except variable products, see after) you will get:
For product variations (of a variable product):
The code works perfectly for simple products. But for the variations the back order text is available but it does not display on the product page when a customer makes the selection
I modified #LoicTheAztec code slightly to use this for displaying the backorder text on the front end.
add_filter( 'woocommerce_get_availability_text', 'customize_availability_text', 10, 2);
function customize_availability_text( $availability, $product ) {
$backorder_text = get_post_meta( $product->get_id(), '_backorder_text', true );
if (! empty( $backorder_text )){$availability = str_replace('Available on backorder', $backorder_text , $availability);}
return $availability;
}
Note that I'm using the
woocommerce_get_availability_text
filter
rather than the
woocommerce_get_availability
filter
This worked with my products with variations, not tested on simple products

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.

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