Skip Navigation

[Geschlossen] Create 'if' statement from custom field

This support ticket is created vor 7 Jahre, 4 Monate. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 14 Antworten, has 3 Stimmen.

Last updated by Caridad vor 7 Jahre, 4 Monate.

Assigned support staff: Minesh.

Author
Artikel
#323314

Hi,

In this topic: https://toolset.com/forums/topic/custom-field-for-woocommerce-variations-and-display-it-with-views/

I asked if it was possible with types to add custom field to WooCommerce variations. That was not possible.

I now managed to add a checkbox myself (happy!).

In my functions.php I also have this working code to DISABLE the Add to cart part of variations:

function filter_woocommerce_variation_is_purchasable( $purchasable ) 
{
  if ( !is_user_logged_in() || appthemes_check_user_role( 'customer' ) ) {
    $purchasable = false; 
  }    
    return $purchasable;
};

add_filter( 'woocommerce_variation_is_purchasable', 'filter_woocommerce_variation_is_purchasable' );

I want to change this to:

  if ( VARATION CHECKBOX IS CHECKED && !is_user_logged_in() || VARATION CHECKBOX IS CHECKED && appthemes_check_user_role( 'customer' ) ) {

The BIG LETTERED PART is which I don't manage after several houres of trying.
Can you please help.

Kind regards,

Willem

#323349

Minesh
Supporter

Languages: Englisch (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello and thank you for contacting Toolset support.

1)
As I understand you want to replace following if statement:

  if ( !is_user_logged_in() || appthemes_check_user_role( 'customer' ) ) {

With your if statement.

if ( VARATION CHECKBOX IS CHECKED && !is_user_logged_in() || VARATION CHECKBOX IS CHECKED && appthemes_check_user_role( 'customer' ) ) {

Now, I would like to know how you assign the value to your 'VARATION CHECKBOX IS CHECKED' constant.?

2)
-- Could you please send me step by step information to reproduce your issue.
*** Please take FULL BACKUP of your database and your website.***
In order to investigate your issue if we require to check things on your install in order to see what could be going wrong there.

If you agree to this please use the form fields I have enabled below this comment box to provide temporary access details (wp-admin and FTP)?

I have set next reply as private.

3)
Could you please send me debug information that will help us to investigate your issue.
=> https://toolset.com/faq/provide-debug-information-faster-support/

#323361
2015-08-06_18-33-45.jpg

Hi!

The website is build offline using the 'hosts file trick', so you can ofcourse access it that way if you want to?

Anyways, I can provide debug info but I don't think that will be helpfull since there is nothing to debug, I just need something to get working.

Here is the code I used to create the checkbox for the variations:

<?php //************** CUSTOM FIELDS WC VARIATIONS *****************//

//Display Fields
add_action( 'woocommerce_product_after_variable_attributes', 'variable_fields', 10, 3 );
//JS to add fields for new variations
add_action( 'woocommerce_product_after_variable_attributes_js', 'variable_fields_js' );
//Save variation fields
add_action( 'woocommerce_process_product_meta_variable', 'save_variable_fields', 10, 1 );

/**
 * Create new fields for variations
 *
*/
function variable_fields( $loop, $variation_data, $variation ) {
?>
	<tr>
		<td>
			<?php
      // Checkbox
			woocommerce_wp_checkbox( 
			array( 
				'id'            => '_checkbox['.$loop.']', 
				'label'         => __('My Checkbox Field', 'woocommerce' ), 
				'description'   => __( 'Check me!', 'woocommerce' ),
				'value'         => get_post_meta( $variation->ID, '_checkbox', true ), 
				)
			);
			?>
		</td>
	</tr>
  <?php
}
/**
 * Create new fields for new variations
 *
*/
function variable_fields_js() {
?>
	<tr>
		<td>
			<?php
      // Checkbox
			woocommerce_wp_checkbox( 
			array( 
				'id'            => '_checkbox[ + loop + ]', 
				'label'         => __('My Checkbox Field', 'woocommerce' ), 
				'description'   => __( 'Check me!', 'woocommerce' ),
				'value'         => '', 
				)
			);
			?>
		</td>
	</tr>
  <?php
}
/**
 * Save new fields for variations
 *
*/
function save_variable_fields( $post_id ) {
	if (isset( $_POST['variable_sku'] ) ) :
		$variable_sku          = $_POST['variable_sku'];
		$variable_post_id      = $_POST['variable_post_id'];
    
    // Checkbox
		$_checkbox = $_POST['_checkbox'];
		for ( $i = 0; $i < sizeof( $variable_sku ); $i++ ) :
			$variation_id = (int) $variable_post_id[$i];
			if ( isset( $_checkbox[$i] ) ) {
				update_post_meta( $variation_id, '_checkbox', stripslashes( $_checkbox[$i] ) );
			}
		endfor;
    
    	endif;
}

In the image I uploaded you can see this checkbox being checked for one of the variations, this is saved through the code you see above.

To loop through the variations I tried to modify the first part of this code: https://wordpress.org/support/topic/display-custom-field-values-for-product-variations?replies=2#post-7083747. Also tried this one to loop through the variations: hidden link.

With bode I get an error for this line:

foreach ($available_variations as $prod_variation) :

I think it's neccesarry to loop trough the variations, before you can make an if statement like I want: 'if checkbox is checked'.

Kind regards,

Willem

#323597

Minesh
Supporter

Languages: Englisch (English )

Timezone: Asia/Kolkata (GMT+05:30)

Sorry, but it will be great if you setup a test install where I can see your issue in action and send me access details.

-- Could you please send me step by step information to reproduce your issue.
-- A link where I can see your issue in action.

*** Please take FULL BACKUP of your database and your website.***
In order to investigate your issue if we require to check things on your install in order to see what could be going wrong there.

If you agree to this please use the form fields I have enabled below this comment box to provide temporary access details (wp-admin and FTP)?

I have set next reply as private.

#323975

Hi, do you have an idea when you will come back to me on this question? Thanks a lot.

#323982

Minesh
Supporter

Languages: Englisch (English )

Timezone: Asia/Kolkata (GMT+05:30)

Unfortunately staging authorization access details are not working at this end so I could not even able to load the wp-admin login page. Could you please verify access details and re-send it back to me.

I've set next reply as private.

#324105

Minesh
Supporter

Languages: Englisch (English )

Timezone: Asia/Kolkata (GMT+05:30)

Could you please share me few links of products where I can see product variations with custom checkboxes integrated by you.

I've set next reply as private.

#324310

Dear Willem

The filter you are trying to use actually takes 2 parameters.
The second parameter is the product object.
You can look into this object to see if its a variation or not:

function filter_woocommerce_variation_is_purchasable( $purchasable, $product ) 
{
  $isVariation = ( $product->product_type == 'variation' );
  if ( $isVariation && !is_user_logged_in() || $isVariation && appthemes_check_user_role( 'customer' ) ) {
    $purchasable = false; 
  }    
    return $purchasable;
};
 
add_filter( 'woocommerce_variation_is_purchasable', 'filter_woocommerce_variation_is_purchasable', 10, 2 );

I hope it helps.
Caridad

#324362

Hi Caridad,

Thanks for your reply. I'm learning ;-).

The difficulty however is that I created a checkbox on the variation in the backend of WordPress, and I need to use that checkbox in the if statement. So 'if checkbox is checked for a variation' then...

I don't see your code doing that, I'm still trying to learn and find out how I should do it, but I'm not any further than that I think we should loop through all variation to check if checkbox is checked.

Kind regards,

Willem

#324552

Ok, WooCommerce 2.4 is out now, and it's MUCH simpler now to delete the 'add to cart' part on a variation:

function wsis_remove_add_to_cart(){
    // @since 2.4.0
    remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
}
add_action('init','wsis_remove_add_to_cart');

But as explained in previous post as well, this code should only be executed when the custom checkbox on a variation is checked. For that, I need to implent an 'if' statement in code above, and that is still the problem I'm having when I started this topic.

Kind regards,

Willem

#324581

Hi,

Today I finished adding a checkbox to 'products' with types for simple products. Adding a checkbox to woocommerce variable product variations was not possible with types, that's why I had to do it the manual way as explained in this topic.

Anyway, creating the if statement for simple product was fairly easy for this PHP noob, as you can see below, only now the 'if' statement for the variation and then I'm good to go.. hope you can help me:

function wsis_remove_add_to_cart() {
  
  $professionals_only_product = types_render_field("professionals-only-checkbox", array("output" => "raw"));
  if ( $professionals_only_product = 1 && !is_user_logged_in() || $professionals_only_product = 1 && get_user_role() == 'customer' ) {
    
    /**
      * remove add to cart single product page simple product
      * for some reason adding comments after '//' is giving errors
      **/    
    remove_action( 'woocommerce_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30 );    
  }


  /** IF STATEMENT VARIATIONS CHECKBOX HERE!! **/  
  /**
    * remove add to cart single product page variable product
    * @since 2.4.0
    * for some reason adding comments after '//' is giving errors
    **/
  remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
}

add_action('init','wsis_remove_add_to_cart');

Kind regards,

Willem

#324586

Ok, this is my last effort and it's not working, I got this code from here: https://wordpress.org/support/topic/display-custom-field-values-for-product-variations?replies=2#post-7083747 to loop through variations, but I do something wrong...

function wsis_remove_add_to_cart() {
  
  $professionals_only_product = types_render_field("professionals-only-checkbox", array("output" => "raw"));
  if ( $professionals_only_product = 1 && !is_user_logged_in() || $professionals_only_product = 1 && get_user_role() == 'customer' ) {
    
    /**
      * remove add to cart single product page simple product
      * for some reason adding comments after '//' is giving errors
      **/    
    remove_action( 'woocommerce_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30 );    
  }
  
  $custom_data = array();
  foreach ($available_variations as $prod_variation) :
    /** get some vars to work with **/
    $variation_id = $prod_variation['variation_id'];
    $variation_object = get_post($variation_id);
    $variable_custom_field = get_post_meta( $variation_object->ID, '_checkbox', true);

    $custom_data[$variation_id] = array(
        "true" => $variable_custom_field
    );
  endforeach;

  if ( $variable_custom_field = true ) {   
  
    /**
      * remove add to cart single product page variable product
      * @since 2.4.0
      * for some reason adding comments after '//' is giving errors
      **/
    remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
  }
}

add_action('init','wsis_remove_add_to_cart');
#324638

Sorry for al the posts, I could not delete all the others, but I discovered mistake in my types php as well, this is correct one so far:

function wsis_remove_add_to_cart() {
  /**
    * see https://wordpress.org/support/topic/get-custom-field-in-admin
    * see https://toolset.com/forums/topic/how-to-use-types-field-value-in-functions-php/
    */
  /* get the post id and assign it to a variable */
  $post_id = get_the_ID();
  /* get the value from the field as it appears in the post with the ID from above and assign it to a variable */
  $professionals_only_product = types_get_field_meta_value( 'professionals-only-checkbox', $post_id ); 
  if ( $professionals_only_product = 1 && !is_user_logged_in() || $professionals_only_product = 1 && get_user_role() == 'customer' ) {
    /**
      * remove add to cart single product page simple product
      * for some reason adding comments after '//' is giving errors
      **/   
    remove_action( 'woocommerce_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30 );    
  }
  
  /** IF STATEMENT VARIATIONS CHECKBOX HERE!! **/ 
  /**
    * remove add to cart single product page variable product
    * @since 2.4.0
    * for some reason adding comments after '//' is giving errors
    **/
  remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
}

add_action('init','wsis_remove_add_to_cart');

What I don't understand, is that I have to put all my comments between /* */ , normally I use // but that is giving me errors...

Kind regards,

Willem

#324663

Double post.

#324842

I'm glad you got it working.

Its the first time I hear about that problem with comments in PHP, but its outside of the scope of the forum which is helping with the usability of Toolset products.

Best
Caridad

Das Thema „[Geschlossen] Create 'if' statement from custom field“ ist für neue Antworten geschlossen.