最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Woocommerce variation with radio button + price

programmeradmin0浏览0评论

I have replaced the dropdown menu of woocommerce with a checked input, but it doesn't change the price of the variation selected, and I don't know how to fix this ?

(result seeable here : / ) ( I duplicated the original code + the code that I did to see, that's why it shows up twice)

At the beginning of variable.php I have :

$attribute_keys = array_keys( $attributes );

function print_attribute_radio( $checked_value, $value, $label, $name ) {

$checked = sanitize_title( $checked_value ) === $checked_value ? checked( $checked_value, sanitize_title( $value ), false ) : checked( $checked_value, $value, false );

$input_name = 'attribute_' . esc_attr( $name ) ;
$esc_value = esc_attr( $value );
$id = esc_attr( $name . '_v_' . $value );
$filtered_label = apply_filters( 'woocommerce_variation_option_name', $label );
printf( '<div><input type="radio" name="%1$s" value="%2$s" id="%3$s" %4$s><label for="%3$s">%5$s</label></div>', $input_name, $esc_value, $id, $checked, $filtered_label );
}

Then the original code is this :

<table class="variations" cellspacing="0">
        <tbody>
            <?php foreach ( $attributes as $attribute_name => $options ) : ?>
                <tr>
                    <td class="label"><label for="<?php echo sanitize_title( $attribute_name ); ?>"><?php echo wc_attribute_label( $attribute_name ); ?></label></td>
                    <td class="value">
                        <?php
                            $selected = isset( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ? wc_clean( urldecode( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ) : $product->get_variation_default_attribute( $attribute_name );
                            wc_dropdown_variation_attribute_options( array( 'options' => $options, 'attribute' => $attribute_name, 'product' => $product, 'selected' => $selected ) );
                            echo end( $attribute_keys ) === $attribute_name ? apply_filters( 'woocommerce_reset_variations_link', '<a class="reset_variations" href="#">' . __( 'Clear', 'woocommerce' ) . '</a>' ) : '';
                        ?>
                    </td>
                </tr>
            <?php endforeach;?>
        </tbody>
    </table>

And my code is this :

        <?php foreach ( $attributes as $name => $options ) : ?>
            <table class="variations" cellspacing="0">
                <tbody>
                <tr>
                    <?php
                    $sanitized_name = sanitize_title( $name );
                    if ( isset( $_REQUEST[ 'attribute_' . $sanitized_name ] ) ) {
                        $checked_value = $_REQUEST[ 'attribute_' . $sanitized_name ];
                    } elseif ( isset( $selected_attributes[ $sanitized_name ] ) ) {
                        $checked_value = $selected_attributes[ $sanitized_name ];
                    } else {
                        $checked_value = '';
                    }
                    ?>
                    <td class="value">
                        <?php
                        if ( ! empty( $options ) ) {
                            if ( taxonomy_exists( $name ) ) {
                                // Get terms if this is a taxonomy - ordered. We need the names too.
                                $terms = wc_get_product_terms( $product->id, $name, array( 'fields' => 'all' ) );

                                foreach ( $terms as $term ) {
                                    if ( ! in_array( $term->slug, $options ) ) {
                                        continue;
                                    }
                                    print_attribute_radio( $checked_value, $term->slug, $term->name, $sanitized_name );
                                }
                            } else {
                                foreach ( $options as $option ) {
                                    print_attribute_radio( $checked_value, $option, $option, $sanitized_name );
                                }
                            }
                        }

                        echo end( $attribute_keys ) === $name ? apply_filters( 'woocommerce_reset_variations_link', '<a class="reset_variations" href="#">' . __( 'Clear', 'woocommerce' ) . '</a>' ) : '';
                        ?>
                    </td>
                </tr>
            </tbody>
    </table>
            <?php endforeach; ?>

Thank you

发布评论

评论列表(0)

  1. 暂无评论