Skip to content

Instantly share code, notes, and snippets.

@bepatrickdavid
Last active July 20, 2017 16:40
Show Gist options
  • Save bepatrickdavid/c2c1ed09feca001d710d to your computer and use it in GitHub Desktop.
Save bepatrickdavid/c2c1ed09feca001d710d to your computer and use it in GitHub Desktop.
WP: Woocommerce add new custom field checkout
//example
function add_custom_stock_type() {
?>
<script type="text/javascript">
jQuery(function(){
jQuery('._stock_status_field').not('.custom-stock-status').remove();
});
</script>
<?php
woocommerce_wp_select( array( 'id' => '_stock_status', 'wrapper_class' => 'hide_if_variable custom-stock-status', 'label' => __( 'Stock status', 'woocommerce' ), 'options' => array(
'instock' => __( 'In stock', 'woocommerce' ),
'outofstock' => __( 'Out of stock', 'woocommerce' ),
'onrequest' => __( 'Disponibile su richiesta', 'woocommerce' ), // The new option !!!
'onstore' => __( 'Disponibile in negozio', 'woocommerce' ),
), 'desc_tip' => true, 'description' => __( 'Controls whether or not the product is listed as "in stock" or "out of stock" on the frontend.', 'woocommerce' ) ) );
}
add_action('woocommerce_product_options_stock_status', 'add_custom_stock_type');
function save_custom_stock_status( $product_id ) {
update_post_meta( $product_id, '_stock_status', wc_clean( $_POST['_stock_status'] ) );
}
add_action('woocommerce_process_product_meta', 'save_custom_stock_status',99,1);
function woocommerce_get_custom_availability( $data, $product ) {
$stock_status = get_post_meta($product->id , '_stock_status' , true );
switch( $stock_status ) {
case 'instock':
$data = array( 'availability' => __( 'In stock', 'woocommerce' ), 'class' => 'in-stock' );
break;
case 'outofstock':
$data = array( 'availability' => __( 'Out of stock', 'woocommerce' ), 'class' => 'out-of-stock' );
break;
case 'onrequest':
$data = array( 'availability' => __( 'Disponibile su Richiesta', 'woocommerce' ), 'class' => 'on-request' );
break;
case 'onstore':
$data = array( 'availability' => __( 'Disponibile in Negozio', 'woocommerce' ), 'class' => 'on-store' );
break;
}
return $data;
}
add_action('woocommerce_get_availability', 'woocommerce_get_custom_availability', 10, 2);
function my_woocommerce_notice() {
?>
<div class="error notice">
<p><?php _e( 'Il plugin WooCommerce è stato modificato nel core. Si prega di non aggiornare il plugin, se non da personale esperto. (file modificato class-wc-admin-post-types linea 441)' ); ?></p>
</div>
<?php
}
add_action( 'admin_notices', 'my_woocommerce_notice' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment