Save custom product input to database in WooCommerce - php

I use the following code to the database:
$attribute_R_0 = wc_get_product( $post_id );
$title_top_R_0 = isset( $_POST['custom_text_field_title_related'] ) ? $_POST['custom_text_field_title_related'] : '';
$attribute_R_0->update_meta_data( 'custom_text_field_title_related', sanitize_text_field( $title_top_R_0 ) );
$attribute_R_0->save();
but I want to use a condition to avoid creating a blank table in db, which one is correct? A or B?
A:
if ('custom_text_field_title_related') {
$attribute_R_0 = wc_get_product( $post_id );
$title_top_R_0 = isset( $_POST['custom_text_field_title_related'] ) ? $_POST['custom_text_field_title_related'] : '';
$attribute_R_0->update_meta_data( 'custom_text_field_title_related', sanitize_text_field( $title_top_R_0 ) );
$attribute_R_0->save();
}
B:
$attribute_R_0 = wc_get_product( $post_id );
$nui_1 = $attribute_14->get_meta( 'custom_text_field_title_related' );
if ($nui_1 ){
$title_top_R_0 = isset( $_POST['custom_text_field_title_related'] ) ? $_POST['custom_text_field_title_related'] : '';
$attribute_R_0->update_meta_data( 'custom_text_field_title_related', sanitize_text_field( $title_top_R_0 ) );
$attribute_R_0->save();
}

None of them… To avoid a blank entry in database table, you should use instead:
if ( isset( $_POST['custom_text_field_title_related'] ) && ! empty( $_POST['custom_text_field_title_related'] ) ) {
$attribute_R_0 = wc_get_product( $post_id );
$attribute_R_0->update_meta_data( 'custom_text_field_title_related', sanitize_text_field( $_POST['custom_text_field_title_related'] ) );
$attribute_R_0->save();
}
or better:
$field_key_R_0 = 'custom_text_field_title_related';
if ( isset( $_POST[$field_key_R_0] ) && ! empty( $_POST[$field_key_R_0] ) ) {
$attribute_R_0 = wc_get_product( $post_id );
$attribute_R_0->update_meta_data( $field_key_R_0, sanitize_text_field( $_POST[$field_key_R_0] ) );
$attribute_R_0->save();
}

Related

Get a list of categories with a relationship to other category

I'm using a WordPress theme which has 3 different taxonomies. Country, State, City. So there are no parent child categories.
**What I'm trying to achieve:
**
On each state archive, I'm trying to show a list of cities and areas (each terms) which have a relationship with that state.
On each city archive, I want to show other cities (each terms) which have the same state.
This is how the theme currently saves the post meta for state, city and area:
// State
if( isset( $_POST['administrative_area_level_1'] ) ) {
$listing_state = sanitize_text_field( $_POST['administrative_area_level_1'] );
$state_id = wp_set_object_terms( $listing_id, $listing_state, 'listing_state' );
$homey_meta = array();
$homey_meta['parent_country'] = isset( $_POST['country'] ) ? $_POST['country'] : '';
if( !empty( $state_id) ) {
update_option('_homey_listing_state_' . $state_id[0], $homey_meta);
}
}
// City
if( isset( $_POST['locality'] ) ) {
$listing_city = sanitize_text_field( $_POST['locality'] );
$city_id = wp_set_object_terms( $listing_id, $listing_city, 'listing_city' );
$homey_meta = array();
$homey_meta['parent_state'] = isset( $_POST['administrative_area_level_1'] ) ? $_POST['administrative_area_level_1'] : '';
if( !empty( $city_id) ) {
update_option('_homey_listing_city_' . $city_id[0], $homey_meta);
}
}
// Area
if( isset( $_POST['neighborhood'] ) ) {
$listing_area = sanitize_text_field( $_POST['neighborhood'] );
$area_id = wp_set_object_terms( $listing_id, $listing_area, 'listing_area' );
$homey_meta = array();
$homey_meta['parent_city'] = isset( $_POST['locality'] ) ? $_POST['locality'] : '';
if( !empty( $area_id) ) {
update_option('_homey_listing_area_' . $area_id[0], $homey_meta);
}
}
Any advice is appreciated.
Thanks.

need to fix my php code to redirect to different pages after purchase depending con category product in woocommerce

I´m working with this code but it's not working.
How can i fix it to redirect to 3 different pages for each category product (I need to set it for 3 different categories).
add_action( 'template_redirect', 'wc_custom_redirect_after_purchase' );
function wc_custom_redirect_after_purchase($order_product_categories) {
$product_categories1 = jobs;
$product_categories2 = courses;
$product_categories3 = exchange-post;
global $wp;
if ( is_checkout() && ! empty( $wp->query_vars['order-received'] ) ) {
$cat_in_cart = false;
$order_id = isset( $wp->query_vars['order-received'] ) ? intval( $wp->query_vars['order-received'] ) : 0;
$order = new WC_Order( $order_id );
$product_categories = array('jobs', 'courses', 'exchange-post');
foreach( $order->get_items() as $item ){
if( has_term( $product_categories, 'product_cat', $item->get_product_id() ) ) {
$cat_in_cart = true;
break;
}
}
if ( $cat1_in_cart ) {
wp_redirect( 'https://steamroute.com/post-a-job-check-out/');
}
elseif ( $cat2_in_cart ) {
wp_redirect( 'https://steamroute.com/courses-checkout/');
}
elseif ( $cat3_in_cart ) {
wp_redirect( 'https://steamroute.com/exchange-checkout/');
}
exit;
}
}
I think you mean this, don't forget that this code is based on 1 category per product, otherwise you will have to adjust it further
p.s. maybe you should consider this method? instead of a redirect https://markjaquith.wordpress.com/2014/02/19/template_redirect-is-not-for-loading-templates/
function wc_custom_redirect_after_purchase() {
$product_categories1 = 'jobs';
$product_categories2 = 'courses';
$product_categories3 = 'exchange-post';
global $wp;
if ( is_checkout() && ! empty( $wp->query_vars['order-received'] ) ) {
$cat_in_cart = false;
$order_id = isset( $wp->query_vars['order-received'] ) ? intval( $wp->query_vars['order-received'] ) : 0;
$order = new WC_Order( $order_id );
$product_categories = array( $product_categories1, $product_categories2, $product_categories3 );
foreach( $order->get_items() as $item ) {
$item_id = $item->get_product_id();
if( has_term( $product_categories, 'product_cat', $item_id ) ) {
$cat_in_cart = true;
$terms = get_the_terms( $item_id, 'product_cat' );
foreach ( $terms as $term ) {
// Categories by slug
$cat_name = $term->slug;
}
break;
}
}
if ( $cat_in_cart ) {
switch ($cat_name) {
case $product_categories1:
wp_redirect( 'https://steamroute.com/post-a-job-check-out/');
break;
case $product_categories2:
wp_redirect( 'https://steamroute.com/courses-checkout/');
break;
case $product_categories3:
wp_redirect( 'https://steamroute.com/exchange-checkout/');
break;
}
}
}
}
add_action( 'template_redirect', 'wc_custom_redirect_after_purchase' );

Customize Order received page based on shipping method in WooCommerce

How could I customise the order thank you page based on the order's shipping method? So for example if a customer used 'Delivery on request' option, the thank you page would display a different title.
add_filter( 'the_title', 'woo_personalize_order_received_title', 10, 2 );
function woo_personalize_order_received_title( $title, $id ) {
if ( is_order_received_page() && get_the_ID() === $id ) {
global $wp;
// Get the order. Line 9 to 17 are present in order_received() in includes/shortcodes/class-wc-shortcode-checkout.php file
$order_id = apply_filters( 'woocommerce_thankyou_order_id', absint( $wp->query_vars['order-received'] ) );
$order_key = apply_filters( 'woocommerce_thankyou_order_key', empty( $_GET['key'] ) ? '' : wc_clean( $_GET['key'] ) );
if ( $order_id > 0 ) {
$order = wc_get_order( $order_id );
if ( $order->get_order_key() != $order_key ) {
$order = false;
}
}
if ( isset ( $order ) ) {
$chosen_titles = array();
$available_methods = $wp->shipping->get_packages();
$chosen_rates = ( isset( $wp->session ) ) ? $wp->session->get( 'chosen_shipping_methods' ) : array();
foreach ($available_methods as $method)
foreach ($chosen_rates as $chosen) {
if( isset( $method['rates'][$chosen] ) ) $chosen_titles[] = $method['rates'][ $chosen ]->label;
}
if( in_array( 'Delivery price on request', $chosen_titles ) ) {
//$title = sprintf( "You are awesome, %s!", esc_html( $order->billing_first_name ) ); // use this for WooCommerce versions older then v2.7
$title = sprintf( "You are awesome, %s!", esc_html( $order->get_billing_first_name() ) );
}
}
}
return $title;
}
is_order_received_page() doesn't exist. Instead use is_wc_endpoint_url( 'order-received' )…
Also $wp->session or $wp->shipping will not work. Instead you can find the chosen shipping method data in the order item "shipping".
Try this instead:
add_filter( 'the_title', 'customizing_order_received_title', 10, 2 );
function customizing_order_received_title( $title, $post_id ) {
if ( is_wc_endpoint_url( 'order-received' ) && get_the_ID() === $post_id ) {
global $wp;
$order_id = absint( $wp->query_vars['order-received'] );
$order_key = isset( $_GET['key'] ) ? wc_clean( $_GET['key'] ) : '';
if ( empty($order_id) || $order_id == 0 )
return $title; // Exit
$order = wc_get_order( $order_id );
if ( $order->get_order_key() != $order_key )
return $title; // Exit
$method_title_names = array();
// Loop through Order shipping items data and get the method title
foreach ($order->get_items('shipping') as $shipping_method )
$method_title_names[] = $shipping_method->get_name();
if( in_array( 'Delivery price on request', $method_title_names ) ) {
$title = sprintf( "You are awesome, %s!", esc_html( $order->get_billing_first_name() ) );
}
}
return $title;
}
Code goes on function.php file of your active child theme (or active theme). Tested and works.
Similar: Adding custom message on Thank You page by shipping method in Woocommerce

Adding first and last name to Wordpress registration form

My registration form on WP only had the options Username, e-mail and password.
//1. firstname
add_action( 'register_form', 'myplugin_register_form' );
function myplugin_register_form() {
$first_name = ( ! empty( $_POST['first_name'] ) ) ? trim( $_POST['first_name'] ) : '';
?>
<?php
}
//2. Add validation. In this case, we make sure first_name is required.
add_filter( 'registration_errors', 'myplugin_registration_errors', 10, 3 );
function myplugin_registration_errors( $errors, $sanitized_user_login, $user_email ) {
if ( empty( $_POST['first_name'] ) || ! empty( $_POST['first_name'] ) && trim( $_POST['first_name'] ) == '' ) {
$errors->add( 'first_name_error', __( '<strong>ERROR</strong>: You must include a first name.', 'mydomain' ) );
}
return $errors;
}
//3. Finally, save our extra registration user meta.
add_action( 'user_register', 'myplugin_user_register' );
function myplugin_user_register( $user_id ) {
if ( ! empty( $_POST['first_name'] ) ) {
update_user_meta( $user_id, 'first_name', trim( $_POST['first_name'] ) );
}
}
In my registration form (template) I've placed the following:
<label><?php _e('First Name', APP_TD) ?></label>
<input tabindex="3" type="text" name="first_name" class="text regular-text" id="display_name" value="<?php echo esc_attr( wp_unslash( $first_name ) ); ?>" maxlength="100" />
And it works as it should. It grabs the first name and places it in the user profile. However I'm not sure on how to add the last name to it aswell, I'm quite a beginner, and for some reason I can't get the last name to work. Help would be much appreciated.
I've never worked with something like that on my WP. Where have you put this code?
However I would try to execute something like this:
//1. Fistname
add_action( 'register_form', 'myplugin_register_form' ); function myplugin_register_form() {
$first_name = ( ! empty( $_POST['first_name'] ) ) ? trim( $_POST['first_name'] ) : '';
$last_name = ( ! empty( $_POST['last_name'] ) ) ? trim( $_POST['last_name'] ) : '';
?>
<?php
}
//2. Add validation. In this case, we make sure first_name is required.
add_filter( 'registration_errors', 'myplugin_registration_errors', 10, 3 );
function myplugin_registration_errors( $errors, $sanitized_user_login, $user_email ) {
if ( empty( $_POST['first_name'] ) || ! empty( $_POST['first_name'] ) && trim( $_POST['first_name'] ) == '' ) {
$errors->add( 'first_name_error', __( '<strong>ERROR</strong>: You must include a first name.', 'mydomain' ) );
}
if ( empty( $_POST['last_name'] ) || ! empty( $_POST['last_name'] ) && trim( $_POST['last_name'] ) == '' ) {
$errors->add( 'last_name_error', __( '<strong>ERROR</strong>: You must include a last name.', 'mydomain' ) );
}
return $errors;
}
//3. Finally, save our extra registration user meta.
add_action( 'user_register', 'myplugin_user_register' );
function myplugin_user_register( $user_id ) {
if ( ! empty( $_POST['first_name'] ) ) {
update_user_meta( $user_id, 'first_name', trim( $_POST['first_name'] ));
}
if ( ! empty( $_POST['last_name'] ) ) {
update_user_meta( $user_id, 'last_name', trim( $_POST['last_name'] ) );
}
}

How to solve the key variable on $unset key

I am running a PHP script, and keep getting errors like:
Notice: Undefined variable: key in D:\0-MYBLOG\SERVER-MYBLOG\InstantWP_4.3.1\iwpserver\htdocs\wordpress\wp-content\plugins\sama-author-review\admin\metabox.php on line 310
Line 310 looks like this:
unset( $new['items_review'][$key] );
Here the code:
if ( $new['items_review'] && is_array( $new['items_review'] )) {
foreach ( $new['items_review'] as $review ) {
if ( empty( $review['label'] ) ) {
unset( $new['items_review'][$key] );
} else {
$review['value'] = absint( $review['value'] );
$review['label'] = esc_attr( $review['label'] );
if ( empty( $review['slug'] )) {
$review['slug'] = sanitize_title( $review['label'] );
} else {
$review['slug'] = sanitize_title( $review['slug'] );
}
$review['style'] = esc_attr( $review['style'] );
$items_review[$i] = $review;
$i++;
}
}
}
Is there a quick fix to resolve these error?
Really appreciate for any help
Thank you
I think I get what the problem is, you have forgotten to put the key parameter on your foreach statement.
I admit its a bit of a guess as I have no idea what is in $new but it seams a logical possibility.
if ( $new['items_review'] && is_array( $new['items_review'] )) {
// foreach ( $new['items_review'] as $review ) {
// replace foreach with this line
foreach ( $new['items_review'] as $key => $review ) {
if ( empty( $review['label'] ) ) {
unset( $new['items_review'][$key] );
} else {
$review['value'] = absint( $review['value'] );
$review['label'] = esc_attr( $review['label'] );
if ( empty( $review['slug'] )) {
$review['slug'] = sanitize_title( $review['label'] );
} else {
$review['slug'] = sanitize_title( $review['slug'] );
}
$review['style'] = esc_attr( $review['style'] );
$items_review[$i] = $review;
$i++;
}
}
}

Categories