web hosting uk

Woocommerce Hooks

Here are a few woocommerce code snippets we want to share with you.

 

Create a product in woocommerce programmatically

Create the post as a product


 $new_post = array(
 'post_id' => '',
 'post_title' => "My Product",
 'post_content' => '',
 'post_status' => 'publish',
 'post_date' => date('Y-m-d H:i:s'),
 'post_author' => $user_ID,
 'post_type' => 'product',
 'post_category' => array(0)
 );
$post_id = wp_insert_post($new_post);
 $new_post_id = $post_id;

Create the various post_meta’s for the product


wp_set_object_terms($new_post_id, 'simple', 'product_type');
wp_set_object_terms( $new_post_id, 'My Catogory', 'product_cat' );    
     update_post_meta( $new_post_id, '_visibility', $visible );
     update_post_meta( $new_post_id, '_stock_status', 'instock');
     update_post_meta( $new_post_id, 'total_sales', '0');
     update_post_meta( $new_post_id, '_downloadable', 'no');
     update_post_meta( $new_post_id, '_virtual', 'yes');
     update_post_meta( $new_post_id, '_regular_price', '7.99' );
     update_post_meta( $new_post_id, '_sale_price', "" );
     update_post_meta( $new_post_id, '_purchase_note', "");
     update_post_meta( $new_post_id, '_featured', "no" );
     update_post_meta( $new_post_id, '_weight', "" );
     update_post_meta( $new_post_id, '_length', "" );
     update_post_meta( $new_post_id, '_width', "" );
     update_post_meta( $new_post_id, '_height', "" );
     update_post_meta( $new_post_id, '_sku', "");
     update_post_meta( $new_post_id, '_product_attributes', array());
     update_post_meta( $new_post_id, '_sale_price_dates_from', "" );
     update_post_meta( $new_post_id, '_sale_price_dates_to', "" );
     update_post_meta( $new_post_id, '_price', "" );
     update_post_meta( $new_post_id, '_sold_individually', "yes" );
     update_post_meta( $new_post_id, '_manage_stock', "no" );
     update_post_meta( $new_post_id, '_backorders', "no" );
     update_post_meta( $new_post_id, '_stock', "1" );
     update_post_meta( $new_post_id, '_type' , "computer software");

and that’s it, look at your woocommerce product page and your new product should be there.

Create a automatic password

Password is 14 characters long and made up from letters and numbers


function wh_filter_password_auto( $args, $username ) {

    $woo_new_password = wp_generate_password( 14, false);
     $args['user_pass'] = $woo_new_password;
	
    return $args;
}
add_filter( 'woocommerce_new_customer_data', 'wh_filter_password_auto' );

Password is 10 characters long and made up from letters,numbers and special characters


function wh_filter_password_auto( $args, $username ) {

    $woo_new_password = wp_generate_password( 10, true);
     $args['user_pass'] = $woo_new_password;
	
    return $args;
}
add_filter( 'woocommerce_new_customer_data', 'wh_filter_password_auto' );

Change State Fields To Required

Make state shipping and billing fields required


function woo_state_billing_required( $address_fields ) { 
  $address_fields['billing_state']['required'] = true;
	return $address_fields;
}
add_filter( 'woocommerce_billing_fields', 'woo_state_billing_required', 10, 1 );

function woo_state_shipping_required( $address_fields ) { 
	$address_fields['shipping_state']['required'] = true;
	return $address_fields;
}
add_filter( 'woocommerce_shipping_fields', 'woo_state_shipping_required', 10, 1 );

Change text from state to County* and after County add a red asterisk with dots beneath it


function wh_change_state_label_locale($locale){
    
  
    $locale['GB']['state']['label'] = __('County *', 'woocommerce');
    return $locale;
}
add this after County to get the red asterisk <span style ="color:#ff0000;border-bottom:1px dotted #ff0000;
    text-decoration: none;">*</span>

Automatically complete orders from PayPal


function wh_complete_order($order_id){
$order = new WC_Order( $order_id );

$paid_status = get_post_meta($order->id,'_transaction_id', true);
if (isset($paid_status) && ($paid_status <> '')){
if ($order->status == 'processing'){$order->update_status( 'completed' );}
}
}
add_action ('woocommerce_payment_complete','wh_complete_order', 20,3);

Leave a Reply

Your email address will not be published. Required fields are marked *