为复选框WooCommerce变体创建if语句

时间:2015-06-14 11:14:32

标签: php wordpress if-statement woocommerce

我成功地为WooCommerce变体添加了自定义复选框(请参阅下面的代码),现在我只需要一个'if语句',我可以在其他函数中使用,单词:

  • 如果选中此变体的复选框

有人可以帮我解决这个问题吗?

这里是我的functions.php代码:

<?php

/*** PARENT CSS ***/

add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );

function theme_enqueue_styles() {
  wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
}

/*** USER ROLE, DOES NOT TAKE VARIABLES ***/

function get_user_role() {
  global $current_user;
  $user_roles = $current_user->roles;
  $user_role = array_shift($user_roles);
  return $user_role;
}

/*** PROFESSIONAL ONLY PRODUCTS = HIDE ADD TO CART ***/

function wsis_remove_add_to_cart() {
  /**
    * see https://wordpress.org/support/topic/get-custom-field-in-admin
    * see https://wp-types.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');

/*** 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;
}

0 个答案:

没有答案