How to make arguments compulsory in WP CLI command - php

I want to use WP CLI to delete the WooCommerce orders.
I have 3 arguments ( product_id,start_date,end_date ). How do I check all 3 arguments are passed in command or not ?
How do I do something like this ?
if ( ! empty( ALL THE ARGS ) ) {
WP_CLI::success( "Success" );
} else {
WP_CLI::error( "args missing" );
}
Below is my code.
$delete_woo_orders = function( $args,$assoc_args ) {
WP_CLI::line( $assoc_args['product_id'] );
WP_CLI::line( $assoc_args['start_date'] );
WP_CLI::line( $assoc_args['end_date'] );
};
WP_CLI::add_command( 'delete_woo_orders', $delete_woo_orders );
Here is my command : wp delete_woo_orders --product_id=1 --start_date="some_date" end_date="some_date"

You can try below code :-
if( defined( 'WP_CLI' ) && WP_CLI ) {
function delete_order ( $args, $assoc_args ) {
global $wpdb;
if( $assoc_args['product_id'] && $assoc_args['start_date'] &&
$assoc_args['end_date'] ){
WP_CLI::success( "all args passed" ); // Success Message
}else{
WP_CLI::error( "args missing" ); // Failed Message
}
}
WP_CLI::add_command( 'delete_woo_orders', 'delete_order' );
}
I have little bit modified your code and checked the $assoc_args have value or not and showing a success and error message.

Related

Woocommerce order->get_data() throws PHP Fatal error: Uncaught Error

I have a function running which first checks to the see if the post type is 'shop_order' and if it's not then the function shouldn't be run. I'm using the return; to end it but when I try adding a new post in the 'product' post type i get the following error:
PHP Fatal error: Uncaught Error: Call to a member function get_data() on boolean in..
Why is it saying the function get_data() is uncaught? This code shouldn't run to this point if the post_type is not shop_order. I've even printed out $post_type which returns product.
function woocommerce_process_shop_order ( $post_id, $post, $update ) {
if (isset($post->post_status) && 'auto-draft' == $post->post_status) {
return;
}
// Autosave, do nothing
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// AJAX? Not used here
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
return;
}
// Check user permissions
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
// don't run the echo if the function is called for saving revision.
if ( $post->post_type == 'revision' ) {
return;
}
$post_type = get_post_type($post_id);
error_log( print_r( $post_type, true ) );
// terminate early if post type is not 'post' and if we are updating post object and not creating it.
if ( "shop_order" != $post_type && !$update ) {
return;
error_log( print_r( 'not shop order', true ) );
}
$is_new = $post->post_date === $post->post_modified;
if ( $is_new ) {
$order = wc_get_order( $post_id );
$order_data = $order->get_data();
}
}

Processing form data without nonce verification Error on my Function - Wordpress

Here is my function:
public function save_meta( $term_id = 0, $taxonomy = '' ) {
$meta = ! empty( $_POST['banner'] ) ? $_POST['banner'] : '';
if ( empty( $meta ) ) {
delete_term_meta( $term_id, 'banner' );
} else {
update_term_meta( $term_id, 'banner', $meta );
}
}
And When Travis review the code it tells me that
Processing form data without nonce verification.
| | (WordPress.CSRF.NonceVerification.NoNonceVerification)
I tried the following but is not working:
public function save_meta( $term_id = 0, $taxonomy = '' ) {
$meta = ! empty( $_POST['banner'] ) && wp_verify_nonce( sanitize_key( $_POST['banner'] ) ? $_POST['banner'] : '';
if ( empty( $meta ) ) {
delete_term_meta( $term_id, 'banner' );
} else {
update_term_meta( $term_id, 'banner', $meta );
}
}
What is wrong with my code?
The nonce is a hash of the user id, the session token, the current time and a tag generated by the function wp_create_nonce(). This hash is used to validate that the request is not conterfeit. In your case a suitable tag would be 'update-banner_' . $term_id. Your HTTP request should return this nonce as a query or post parameter. For form submission this is usually done by using a hidden field in the form. WordPress provides the convenience function wp_nonce_field() to do this. Your request handler should then verify this nonce using the function wp_verify_nonce() or the convenience function check_admin_referer(). Please read the WordPress documentation for details on calling these functions.

Issues in validating custom code in Contact Form 7

I'm trying to implement a text field validation for field name VoucherNumber that requires the code to be in a certain pattern which is 'WWV-'followed by 4 numbers.
I was successfully able to implement this on google docs using the following expression ^[W]WV-[0-9][0-9][0-9][0-9].
I researched through various answers and attempted to add this code in functions.php but it didn't work. It would just show that the form is being sent (spinning wheel) but it would not be sent even after 5 minutes.
add_filter( 'wpcf7_validate_text*', 'validate_voucher_number', 20, 2 );
function validate_voucher_number( $result, $tag ) {
$tag = new WPCF7_FormTag( $tag );
if ( 'VoucherNumber' == $tag->name ) {
$VoucherNumber = isset( $_POST['VoucherNumber'] ) ? trim( $_POST['VoucherNumber'] ) : '';
if ( ! preg_match ( "^[W]WV-[0-9][0-9][0-9][0-9]" , $VoucherNumber) ){
$result->invalidate( $tag, "Voucher number is invalid" );
}
}
return $result;
}
Just make sure that your field name is VoucherNumber and try this code :
add_filter( 'wpcf7_validate_text', 'vouchervalidation', 20, 2 );
function vouchervalidation( $result, $tag ) {
if ( 'VoucherNumber' == $tag->name ) {
$VoucherNumber = isset( $_POST['VoucherNumber'] ) ? trim( $_POST['VoucherNumber'] ) : '';
if ( ! preg_match ( "^[W]WV-[0-9][0-9][0-9][0-9]" , $VoucherNumber) ){
$result->invalidate( $tag, "Voucher number is invalid" );
}
}
return $result;
}

Woocommerce payment options only for certain countries

I am trying to enable and disable certain payment functions for certain countries in Woocommerce. I had placed the following code in my functions:
function payment_gateway_disable_by_country( $available_gateways ) {
// Abort if in admin area
if ( is_admin() ) {
return $available_gateways;
}
$billing_country = WC()->customer->get_country();
$shipping_country = ! empty( WC()->customer->get_shipping_country() ) ? WC()->customer->get_shipping_country() : $billing_country;
if ( isset( $available_gateways['invoice'] ) && $billing_country != 'DE' ) {
unset( $available_gateways['invoice'] );
}
if ( isset( $available_gateways['cod'] ) && $shipping_country != 'DE' ) {
unset( $available_gateways['cod'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways','payment_gateway_disable_by_country');
but then I am getting a white screen with the following error message:
Fatal error: Can't use method return value in write context in /home/www/mysite/html/wordpress/wp-content/themes/mytheme/functions.php on line 10.
Is there anything wrong with my code?

How to add error message while add new custom post and edit custom post

I am trying to add error message on custom field in custom post save action But my validation not working. Check my code below.
add_action( 'save_post', 'save_event_meta_data' );
function save_event_meta_data( $post_id ) {
$event_university = $_POST['event_university'];
if ( isset( $_POST['post_type'] ) && 'tribe_events' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
if($event_university=="") {
new WP_Error( 'Error', __( "Please select university" ) );
return;
}
$event_university_data = $event_university ;
update_post_meta( $post_id, 'event_university', $event_university_data );
}
please do the needful.
For displaying error message on other page, you need to save it in some variable. So try below code.
if($event_university=="") {
global $error;
$error = new WP_Error();
$error->add("Please select university");
return;
}
Then on the other page you can access that error message using below code:
global $error;
echo $error->get_error_message();

Categories