WooCommerce Bulk Attributes plugin code example

Example 1: add custom field to variation woocommerce

/**
 * @snippet       Add Custom Field to Product Variations - WooCommerce
 * @how-to        Get CustomizeWoo.com FREE
 * @sourcecode    https://businessbloomer.com/?p=73545
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.5.6
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */
 
// -----------------------------------------
// 1. Add custom field input @ Product Data > Variations > Single Variation
 
add_action( 'woocommerce_variation_options_pricing', 'bbloomer_add_custom_field_to_variations', 10, 3 );
 
function bbloomer_add_custom_field_to_variations( $loop, $variation_data, $variation ) {
woocommerce_wp_text_input( array(
'id' => 'custom_field[' . $loop . ']',
'class' => 'short',
'label' => __( 'Custom Field', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, 'custom_field', true )
)
);
}
 
// -----------------------------------------
// 2. Save custom field on product variation save
 
add_action( 'woocommerce_save_product_variation', 'bbloomer_save_custom_field_variations', 10, 2 );
 
function bbloomer_save_custom_field_variations( $variation_id, $i ) {
$custom_field = $_POST['custom_field'][$i];
if ( isset( $custom_field ) ) update_post_meta( $variation_id, 'custom_field', esc_attr( $custom_field ) );
}
 
// -----------------------------------------
// 3. Store custom field value into variation data
 
add_filter( 'woocommerce_available_variation', 'bbloomer_add_custom_field_variation_data' );
 
function bbloomer_add_custom_field_variation_data( $variations ) {
$variations['custom_field'] = '<div class="woocommerce_custom_field">Custom Field: <span>' . get_post_meta( $variations[ 'variation_id' ], 'custom_field', true ) . '</span></div>';
return $variations;
}

Example 2: bulk cancel order woocommerce

/*
 * Bulk action handler
 * Make sure that "action name" in the hook is the same like the option value from the above function
 */
add_action( 'admin_action_mark_change_status_to_cancelled', 'my_bulk_process_custom_status' ); // admin_action_{action name}

function my_bulk_process_custom_status() {


// if an array with order IDs is not presented, exit the function
if( !isset( $_REQUEST['post'] ) && !is_array( $_REQUEST['post'] ) )
    return;

foreach( $_REQUEST['post'] as $order_id ) {

    $order = new WC_Order( $order_id );
    $order_note = 'That\'s what happened by bulk edit:';
    $order->update_status( 'cancelled', $order_note, true ); // "my-shipment" is the order status name
}

// of course using add_query_arg() is not required, you can build your URL inline
$location = add_query_arg( array(
        'post_type' => 'shop_order',
    'mark_change_status_to_cancelled' => 1, // mark_change_status_to_cancelled=1 is just the $_GET variable for notices
    'changed' => count( $_REQUEST['post'] ), // number of changed orders
    'ids' => join( $_REQUEST['post'], ',' ),
    'post_status' => 'all'
), 'edit.php' );

wp_redirect( admin_url( $location ) );
exit;


}

/*
 * Notices
 */
add_action('admin_notices', 'my_custom_order_status_notices');


function my_custom_order_status_notices() {

    global $pagenow, $typenow;


if( $typenow == 'shop_order'
 && $pagenow == 'edit.php'
 && isset( $_REQUEST['mark_change_status_to_cancelled'] )
 && $_REQUEST['mark_change_status_to_cancelled'] == 1
 && isset( $_REQUEST['changed'] ) ) {

    $message = sprintf( _n( 'Order status changed.', '%s order statuses changed.', $_REQUEST['changed'] ), number_format_i18n( $_REQUEST['changed'] ) );
    echo "<div class=\"updated\"><p>{$message}</p></div>";

}
}

Tags:

Misc Example