按自定义字段在字体中有条件地隐藏或显示WooCommerce产品变体

时间:2018-08-24 作者:Out of Orbit

我已使用以下代码向产品变体添加了一个自定义字段(复选框):

/**
* Create new fields for variations
*
*/
function variation_settings_fields( $loop, $variation_data, $variation ) {

woocommerce_wp_checkbox( 
array( 
    \'id\'            => \'_wholesale_checkbox[\' . $variation->ID . \']\', 
    \'label\'         => \'Visa endast för företagskunder\', 
    \'description\'   => \'\',
    \'value\'         => get_post_meta( $variation->ID, \'_wholesale_checkbox\', true ), 
    )
);

}
/**
 * Save new fields for variations
 *
*/
function save_variation_settings_fields( $post_id ) {

// Checkbox
$checkbox = isset( $_POST[\'_wholesale_checkbox\'][ $post_id ] ) ? \'yes\' : \'no\';
update_post_meta( $post_id, \'_wholesale_checkbox\', $checkbox );

}

add_action( \'woocommerce_variation_options\', \'variation_settings_fields\', 10, 3 );
add_action( \'woocommerce_save_product_variation\', \'save_variation_settings_fields\', 10, 2 );
它根据需要添加复选框,如屏幕截图所示。

enter image description here

现在,我想基于此字段在前端有条件地隐藏或显示产品变体。问题是,我有一些自定义的变体列表(不是我的工作),我不知道如何抓住\\u批发\\u复选框字段来跳过输出列表的循环中的变体(复选框已选中)。请参见以下输出自定义变体列表的代码:

/**
 * Convert WooCommerce variation dropdown to radio buttons.
 *
 * @param string $html Original dropdown html.
 * @param array  $args Arguments.
 */
function ac_variation_radio_buttons( $html, $args ) {
$options   = $args[\'options\'];
$product   = $args[\'product\'];
$attribute = $args[\'attribute\'];
$name      = $args[\'name\'] ? $args[\'name\'] : \'attribute_\' . sanitize_title( $attribute );
$id        = $args[\'id\'] ? $args[\'id\'] : sanitize_title( $attribute );

if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {
    $attributes = $product->get_variation_attributes();
    $options    = $attributes[ $attribute ];
}

// Output original dropdown element.
echo $html;

/* Create custom radio input list */
$html = \'<section for="\' . esc_attr( $id ) . \'" class="ac_variation_list_block">\';

if ( ! empty( $options ) ) {
    if ( $product && taxonomy_exists( $attribute ) ) {
        // Get terms if this is a taxonomy - ordered. We need the names too.
        $terms = wc_get_product_terms( $product->get_id(), $attribute, [
            \'fields\' => \'all\',
        ] );
        // var_dump($options);
        foreach ( $terms as $term ) {
            /*
            if (**CUSTOM_FIELD is checked** )) {
                continue;
            }
            */
            if ( in_array( $term->slug, $options, true ) ) {
                $html .= \'<section class="ac_variation_option">\';
                $html .= \'<input id="\' . esc_attr( $term->slug ) . \'_v" type="radio" name="\' . esc_attr( $name ) . \'_g" value="\' . esc_attr( $term->slug ) . \'" \' . checked( sanitize_title( $args[\'selected\'] ), $term->slug, false ) . \'/>\';
                $html .= \'<label for="\' . esc_attr( $term->slug ) . \'_v"><span></span>\' . esc_html( apply_filters( \'woocommerce_variation_option_name\', $term->name ) ) . \'</label>\';
                $html .= \'</section>\';
            }
        }
    }
}

$html .= \'</section>\';

echo $html;
}

add_filter( \'woocommerce_dropdown_variation_attribute_options_html\', \'ac_variation_radio_buttons\', 10, 2 );

1 个回复
SO网友:Out of Orbit

我最终通过操纵$选项数组解决了这个问题。请参阅下面的完整代码,以通过自定义字段有条件地隐藏产品变体,并将变体显示为单选按钮。用注释标记的解决方案

/**
* Convert WooCommerce variation dropdown to radio buttons.
*
* @param string $html Original dropdown html.
* @param array  $args Arguments.
*/
function ac_variation_radio_buttons( $html, $args ) {
$options   = $args[\'options\'];
$product   = $args[\'product\'];
$attribute = $args[\'attribute\'];
$name      = $args[\'name\'] ? $args[\'name\'] : \'attribute_\' . sanitize_title( $attribute );
$id        = $args[\'id\'] ? $args[\'id\'] : sanitize_title( $attribute );

if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {
    $attributes = $product->get_variation_attributes();
    $options    = $attributes[ $attribute ];
}

// Output original dropdown element.
echo $html;

/**** ADDED CODE *****/
//Remove wholesale products for regular users
if($product->is_type( \'variable\' )){
    $user = wp_get_current_user();
    if(!in_array(\'fretagskund\', $user->roles)){
        foreach ( $product->get_children() as $child_id ) {
            $variation = wc_get_product( $child_id );

            if ( ! $variation || ! $variation->exists() ) {
                continue;
            }

            if(get_post_meta($variation->get_id(), \'_wholesale_checkbox\', true) === "yes" ){
                $attribute_name = get_post_meta($variation->get_id(), \'attribute_pa_storlek\', true);

                if ( in_array( $attribute_name, $options, true ) ) {
                    $options = array_diff($options, array($attribute_name));
                }

            }

        }
    }
}

/* Create custom radio input list */
$html = \'<section for="\' . esc_attr( $id ) . \'" class="ac_variation_list_block">\';

if ( ! empty( $options ) ) {
    if ( $product && taxonomy_exists( $attribute ) ) {
        // Get terms if this is a taxonomy - ordered. We need the names too.
        $terms = wc_get_product_terms( $product->get_id(), $attribute, [
            \'fields\' => \'all\',
        ] );

        foreach ( $terms as $term ) {
            if ( in_array( $term->slug, $options, true ) ) {
                $html .= \'<section class="ac_variation_option">\';
                $html .= \'<input id="\' . esc_attr( $term->slug ) . \'_v" type="radio" name="\' . esc_attr( $name ) . \'_g" value="\' . esc_attr( $term->slug ) . \'" \' . checked( sanitize_title( $args[\'selected\'] ), $term->slug, false ) . \'/>\';
                $html .= \'<label for="\' . esc_attr( $term->slug ) . \'_v"><span></span>\' . esc_html( apply_filters( \'woocommerce_variation_option_name\', $term->name ) ) . \'</label>\';
                $html .= \'</section>\';
            }
        }
    }
}

$html .= \'</section>\';

echo $html;
}


add_filter( \'woocommerce_dropdown_variation_attribute_options_html\', \'ac_variation_radio_buttons\', 10, 2 );

结束

相关推荐