Category Archives: WordPress

Create A WordPress Woocommerce Coupon Programatically

How can I create a WordPress Woocommerce coupon programmatically

The Woocommerce coupon is kind of post. Its similar to creating a post.

coupon_code = 'Test'; // Code
$amount = '1'; // Amount
$discount_type = 'fixed_cart'; // Type: fixed_cart, percent, fixed_product, percent_product
					
$coupon = array(
	'post_title' => $coupon_code,
	'post_content' => '',
	'post_status' => 'publish',
	'post_author' => 1,
	'post_type' => 'shop_coupon'
);
					
$new_coupon_id = wp_insert_post( $coupon );
					

update_post_meta( $new_coupon_id, 'discount_type', $discount_type );
update_post_meta( $new_coupon_id, 'coupon_amount', $amount );
update_post_meta( $new_coupon_id, 'individual_use', 'no' );
update_post_meta( $new_coupon_id, 'product_ids', '' );
update_post_meta( $new_coupon_id, 'exclude_product_ids', '' );
update_post_meta( $new_coupon_id, 'usage_limit', '' );
update_post_meta( $new_coupon_id, 'expiry_date', '' );
update_post_meta( $new_coupon_id, 'apply_before_tax', 'no' );
update_post_meta( $new_coupon_id, 'free_shipping', 'no' );

How Can I Create An Woocommerce Order Programmatically

How can I create an Woocommerce Order Programmatically

You can use wc_create_order() to setup an Woocommerce order object and assign data you would like.

global $woocommerce;
$address = array(
      'first_name' => 'Test',
      'last_name'  => 'Test',
      'company'    => 'Test',
      'email'      => 'test@softwarehtec.com',
      'phone'      => '00-00-0000',
      'address_1'  => '0 test st',
      'address_2'  => '',
      'city'       => 'Test',
      'state'      => 'NY',
      'postcode'   => '10010',
      'country'    => 'US'
);


$order = wc_create_order();
$order->add_product( get_product('1'), 1); 
$order->set_address( $address, 'billing' );

$order->calculate_totals();
$order->update_status("completed", 'Created order programmatically', TRUE);  

How Can I Remove WordPress Widgets From Sidebar Programmatically

How can I remove a WordPress widgets from sidebar programmatically

You can modify the value of ‘sidebars_widgets’ option to remove or add widgets for your WordPress sidebar programmatically.

$sidebars_widgets = get_option( 'sidebars_widgets' );

//sidebar-1 - sidebar id
//adrotate_widgets - widget id

$sidebar_1_array = $sidebars_widgets['sidebar-1'];

foreach($sidebar_1_array as $k => $v){
    if(substr($v, 0, strlen("adrotate_widgets-")) === "adrotate_widgets-"){
        unset($sidebars_widgets['sidebar-1'][$k]);
    }
}

update_option( 'sidebars_widgets', $sidebars_widgets);

How To Sent The Activation Email After Created User Programmatically

I wrote a plugin and create a user programmatically, however dont know how to create a activation email and send it to the new user

You need to build a activation link for new user and sent it to new user manually

        $code = sha1( $user_id . time() );
        $activation_link = add_query_arg( array( 'key' => $code, 'user' => $user_id ), get_permalink( /* YOUR ACTIVATION PAGE ID HERE */ ));
        add_user_meta( $user_id, 'has_to_be_activated', $code, true );
        wp_mail( $data['user_email'], 'ACTIVATION SUBJECT', 'CONGRATS BLA BLA BLA. HERE IS YOUR ACTIVATION LINK: ' . $activation_link );

How To Create WordPress Tag Programmatically

I would like to create a new WordPress tag from plugin programmatically. Which function should I call

You can use wp_insert_term function to create a new tag for “post_tag” taxonomy

wp_insert_term(
  'New Tag Name', // the term 
  'post_tag', // the taxonomy
  array(
    'description'=> 'Tag Desc.',
    'slug' => 'new-tag',
    'parent'=> $parent_term_id
  )
);