web-dev-qa-db-ja.com

Woocommerce - アクティブバリエーション商品の価格を表示

私はwoocommerceの商品バリエーションにラジオボタンを使用しています。各製品バリエーションには独自の価格があります。ラジオボタンが選択された後にバリアント価格を表示したいのですが。アクティブに選択されたvariation_idを 関数get_product_variation_price($ variation_id)に渡す方法がわからない。

ラジオボタンのHTMLコードの例は、HTML出力では次のようになります。

<input type="radio" class="radioselect" name="attribute_size" value="3,000+"      
id="size_v_3,000+">
<label for="size_v_3,000+">3,000+</label>

それでも私のバリエーションは#12、#3041などのような番号(私は思うポスト番号)です。

これが私の完全なvariable.phpファイルです。

<?php
/**
 * Variable product add to cart
 *
 * @author  WooThemes
 * @package WooCommerce/Templates
 * @version 2.5.0
 *
 * Modified to use radio buttons instead of dropdowns
 * @author 8manos
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

function print_attribute_radio( $checked_value, $value, $label, $name ) {
    // This handles < 2.4.0 bw compatibility where text attributes were not sanitized.
    $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 );
    $super_filtered = explode(' ', $filtered_label);
    printf( '<div><input type="radio" class="radioselect" 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, $super_filtered[0] );
}

global $product;

$attribute_keys = array_keys( $attributes );

do_action( 'woocommerce_before_add_to_cart_form' ); ?>

<form class="variations_form cart" method="post" enctype='multipart/form-data' data-product_id="<?php echo absint( $product->id ); ?>" data-product_variations="<?php echo htmlspecialchars( json_encode( $available_variations ) ) ?>">
    <?php do_action( 'woocommerce_before_variations_form' ); ?>

    <?php if ( empty( $available_variations ) && false !== $available_variations ) : ?>
        <p class="stock out-of-stock"><?php _e( 'This product is currently out of stock and unavailable.', 'woocommerce' ); ?></p>
    <?php else : ?>
        <table class="variations" cellspacing="0">
            <tbody>
                <?php foreach ( $attributes as $name => $options ) : ?>
                    <tr>
                        <td class="label" style="margin-top: -10px;"><label for="<?php echo sanitize_title( $name ); ?>"><?php echo wc_attribute_label( $name ); ?></label></td>
                        <?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" style="margin-top: -30px;">

                            <?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>
                <?php endforeach; ?>
            </tbody>
        </table>

        <?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>

        <div class="single_variation_wrap">
            <?php do_action( 'woocommerce_before_single_variation' ); ?>

            <?php
            if ( has_action( 'woocommerce_single_variation' ) ) {
                do_action( 'woocommerce_single_variation' );
            } else {
                // Backwards compatibility with WC < 2.4
            ?>
                <div class="woocommerce-variation single_variation"></div>

                <div class="woocommerce-variation-add-to-cart variations_button">
                    <?php if ( ! $product->is_sold_individually() ) : ?>
                        <?php woocommerce_quantity_input( array( 'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( $_POST['quantity'] ) : 1 ) ); ?>
                    <?php endif; ?>
                    <button type="submit" class="single_add_to_cart_button button alt otto"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>
                    <input type="hidden" name="add-to-cart" value="<?php echo absint( $product->id ); ?>" />
                    <input type="hidden" name="product_id" value="<?php echo absint( $product->id ); ?>" />
                    <input type="hidden" name="variation_id" class="variation_id" value="0" />
                </div>
            <?php } ?>

            <?php do_action( 'woocommerce_after_single_variation' ); ?>
        </div>

        <?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>

    <?php endif; ?>

    <?php do_action( 'woocommerce_after_variations_form' ); ?>
<p>


<?php

function get_product_variation_price($variation_id) {

    global $woocommerce; 
    $product = new WC_Product_Variation($variation_id);
    return $product->get_price_html(); // Works. Use this if you want the formatted price

}

///Display actively selected variant radio buttons price
echo get_product_variation_price(11);
?>

</p>
</form>

<?php do_action( 'woocommerce_after_add_to_cart_form' ); ?>
1
Taj

私は解決策を思いつくことができました。ラジオボタンの下/近くにバリアント価格が表示されます。

バリアント価格を表示します(IDが与えられている場合)。

function get_product_variation_price($variation_id) {

global $woocommerce; 
$product = new WC_Product_Variation($variation_id);
return $product->get_price_html(); // Works. Use this if you want the formatted price
}

バリアントIDを検索し、関数get_product_variation_priceに送信してから、価格を表示します。

///Display actively selected variant radio buttons price
$product_variations = $product->get_available_variations();
$arr_variations_id = array();
foreach ($product_variations as $variation) {
    $product_variation_id = $variation['variation_id'];
    $product_price = get_product_variation_price($product_variation_id);
    echo $product_price;
}

ラジオボタンの表示は次のとおりです。

foreach ( $options as $option ) {
    print_attribute_radio( $checked_value, $option, $option, $sanitized_name );
                                }

ラジオボタンforeachを配列Keyで修正

foreach ( $options as $key=>$option ) {...}

結合されたForeach:

foreach ( $options as $key=>$option ) {
    print_attribute_radio( $checked_value, $option, $option, $sanitized_name );
    $product_variations = $product->get_available_variations();
    $variation_product_id = $product_variations [$key]['variation_id'];
    $product_price = get_product_variation_price($variation_product_id);
    printf ('%1$s</div> ', $product_price);
                                    }

合計コード

<?php
/**
 * Variable product add to cart
 *
 * @author  WooThemes
 * @package WooCommerce/Templates
 * @version 2.5.0
 *
 * Modified to use radio buttons instead of dropdowns
 * @author 8manos
 * Further modifications to display price below radio buttons
 * @author GoodGuyTaj
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}
// START get product price 
function get_product_variation_price($variation_id) {

    global $woocommerce; 
    $product = new WC_Product_Variation($variation_id);
    return $product->get_price_html(); // Works. Use this if you want the formatted price

}
// END get product price 


// START Display Radio Buttons
function print_attribute_radio( $checked_value, $value, $label, $name ) {
    // This handles < 2.4.0 bw compatibility where text attributes were not sanitized.
    $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 );
    $super_filtered = explode(' ', $filtered_label);
    printf( '<div><input type="radio" class="radioselect" name="%1$s" value="%2$s" id="%3$s" %4$s><label for="%3$s">%5$s</label>', $input_name, $esc_value, $id, $checked, $super_filtered[0] );
}

global $product;

$attribute_keys = array_keys( $attributes );

do_action( 'woocommerce_before_add_to_cart_form' ); ?>

<form class="variations_form cart" method="post" enctype='multipart/form-data' data-product_id="<?php echo absint( $product->id ); ?>" data-product_variations="<?php echo htmlspecialchars( json_encode( $available_variations ) ) ?>">
    <?php do_action( 'woocommerce_before_variations_form' ); ?>

    <?php if ( empty( $available_variations ) && false !== $available_variations ) : ?>
        <p class="stock out-of-stock"><?php _e( 'This product is currently out of stock and unavailable.', 'woocommerce' ); ?></p>
    <?php else : ?>
        <table class="variations" cellspacing="0">
            <tbody>
                <?php foreach ( $attributes as $name => $options ) : ?>
                    <tr>
                        <td class="label" style="margin-top: -10px;"><label for="<?php echo sanitize_title( $name ); ?>"><?php echo wc_attribute_label( $name ); ?></label></td>
                        <?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" style="margin-top: -30px;">

                            <?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 $key=>$option ) {
                                        print_attribute_radio( $checked_value, $option, $option, $sanitized_name );
                                        $product_variations = $product->get_available_variations();
                                        $variation_product_id = $product_variations [$key]['variation_id'];
                                        $product_price = get_product_variation_price($variation_product_id);
                                        printf ('%1$s</div> ', $product_price);
                                    }


                                }
                            }

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

        <?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>

        <div class="single_variation_wrap">
            <?php do_action( 'woocommerce_before_single_variation' ); ?>

            <?php
            if ( has_action( 'woocommerce_single_variation' ) ) {
                do_action( 'woocommerce_single_variation' );
            } else {
                // Backwards compatibility with WC < 2.4
            ?>
                <div class="woocommerce-variation single_variation"></div>

                <div class="woocommerce-variation-add-to-cart variations_button">
                    <?php if ( ! $product->is_sold_individually() ) : ?>
                        <?php woocommerce_quantity_input( array( 'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( $_POST['quantity'] ) : 1 ) ); ?>
                    <?php endif; ?>
                    <button type="submit" class="single_add_to_cart_button button alt otto"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>
                    <input type="hidden" name="add-to-cart" value="<?php echo absint( $product->id ); ?>" />
                    <input type="hidden" name="product_id" value="<?php echo absint( $product->id ); ?>" />
                    <input type="hidden" name="variation_id" class="variation_id" value="0" />
                </div>
            <?php } ?>

            <?php do_action( 'woocommerce_after_single_variation' ); ?>
        </div>

        <?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>

    <?php endif; ?>

    <?php do_action( 'woocommerce_after_variations_form' ); ?>

</form>

<?php do_action( 'woocommerce_after_add_to_cart_form' ); ?>
4
Taj