php 自定义Woocommerce产品变化属性

s4n0splo  于 2023-11-16  发布在  PHP
关注(0)|答案(1)|浏览(124)

我见过类似的问题,并尝试了所有的解决方案,但似乎没有一个适用于我的项目。
我所有的产品都有额外的规格,当客户在产品页面上选择不同的变体时,需要进行更改。我需要在产品属性表中显示规格。到目前为止,我已经尝试了几种不同的方法,但我的代码到目前为止看起来像这样。我可以从编辑产品页面获取额外的数据进行保存,但我不知道如何让它显示在产品页面上。
Functions.php

// Add custom fields to product variation settings
add_action( 'woocommerce_product_after_variable_attributes','add_variation_options_other_dimensions', 10, 3 );
function add_variation_options_other_dimensions( $loop, $variation_data, $variation ){

    woocommerce_wp_text_input( array(
        'id' => 'diameter[' . $loop . ']',
        'class' => 'short',
        'label'       => __( 'Diameter', 'woocommerce' ),
        'desc_tip'    => 'true',
        'description' => __( 'Diameter description help.', 'woocommerce' ),
        'value'       => get_post_meta( $variation->ID, 'diameter', true )
    ) );

    woocommerce_wp_text_input( array(
        'id' => 'thickness[' . $loop . ']',
        'class' => 'short',
        'label'       => __( 'Thickness', 'woocommerce' ),
        'desc_tip'    => 'true',
        'description' => __( 'Thickness description help.', 'woocommerce' ),
        'value'       => get_post_meta( $variation->ID, 'thickness', true )
    ) );

}

// Save product variation custom fields values
add_action( 'woocommerce_save_product_variation','save_variation_options_other_dimensions', 10 ,2 );
function save_variation_options_other_dimensions( $variation_id, $i ) {

    $diameter = $_POST["diameter"][$i];
    if ( isset( $diameter ) )
        update_post_meta( $variation_id, 'diameter', esc_attr( $diameter ) );

    $thickness = $_POST["thickness"][$i];
    if ( isset( $thickness ) )
        update_post_meta( $variation_id, 'thickness', esc_attr( $thickness ) );

}

字符串
我尝试在“product-attributes.php”和“additional-information.php”中显示值,因为我希望新值沿着原始属性一起显示。
我的产品-属性-php

<?php echo get_post_meta( $product->get_id(), 'diameter', true ); ?>
<?php echo get_post_meta( $product->get_id(), 'thickness', true ); ?>


是否可以在产品页面上的产品属性表中或其后面的附加信息选项卡中显示这些值?
如果我的问题是愚蠢的,提前道歉,这是我第一次发帖。我只是不能让它工作

mftmpeh8

mftmpeh81#

我不认为数据是你要求的,但这里有一个通用的指南,用于在选择变量时添加/显示自定义变量数据。我还转换为使用Woo的CRUD,以便将来兼容。

/**
 * Add custom fields to product variation settings
 *
 * @param string $loop
 * @param array $variation_data
 * @param WP_Post $variation
 * @return array
 */
function add_variation_options_other_dimensions( $loop, $variation_data, $variation ){

    $variation_obj = wc_get_product( $variation->ID );

    woocommerce_wp_text_input( array(
        'id' => 'diameter[' . $loop . ']',
        'class' => 'short',
        'label'       => __( 'Diameter', 'woocommerce' ),
        'desc_tip'    => 'true',
        'description' => __( 'Diameter description help.', 'woocommerce' ),
        'value'       => $variation_obj->get_meta( 'diameter', true ),
    ) );

    woocommerce_wp_text_input( array(
        'id' => 'thickness[' . $loop . ']',
        'class' => 'short',
        'label'       => __( 'Thickness', 'woocommerce' ),
        'desc_tip'    => 'true',
        'description' => __( 'Thickness description help.', 'woocommerce' ),
        'value'       => $variation_obj->get_meta( 'thickness', true ),
    ) );

}
add_action( 'woocommerce_product_after_variable_attributes','add_variation_options_other_dimensions', 10, 3 );

/**
 * Save product variation custom fields values
 *
 * @param int $variation_id
 * @param int $i
 */
function save_variation_options_other_dimensions( $variation_id, $i ) {

    $variation_obj = wc_get_product( $variation_id );

    if ( isset( $_POST["diameter"][$i] ) ) {
        $variation_obj->update_meta_data( 'diameter', wc_clean( $_POST["diameter"][$i] ) );
    }

    if ( isset( $_POST["thickness"][$i] ) ) {
        $variation_obj->update_meta_data( 'thickness', wc_clean( $_POST["thickness"][$i] ) );
    }

    $variation_obj->save();

}
add_action( 'woocommerce_save_product_variation','save_variation_options_other_dimensions', 10 ,2 );

/**
 * Add data to json encoded variation form.
 *
 * @param  array $data - this is the variation's json data
 * @param  object $product
 * @param  object $variation
 * @return array
 */
function kia_available_variation( $data, $product, $variation ){
    $kia_data['diameter']  = $variation->get_meta( 'diameter', true );
    $kia_data['thickness'] = $variation->get_meta( 'thickness', true );

    return array_merge( $data, $kia_data );

}
add_filter( 'woocommerce_available_variation', 'kia_available_variation', 10, 3 );

/**
 * Add scripts to variable products.
 */
function kia_on_found_template_for_variable_add_to_cart() {
    add_action( 'wp_print_footer_scripts', 'kia_variable_footer_scripts', 99 );
}
add_action( 'woocommerce_variable_add_to_cart', 'kia_on_found_template_for_variable_add_to_cart', 30 );

function kia_variable_footer_scripts() { ?>

    <script type="text/template" id="tmpl-variation-template-extra-data">
        <tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--attribute_diameter extra-data">
            <th class="woocommerce-product-attributes-item__label"><?php esc_html_e( 'Diameter', 'my-text-domain' );?></th>
            <td class="woocommerce-product-attributes-item__value">{{{ data.variation.diameter }}}</td>
        </tr>
        <tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--attribute_thickness extra-data">
            <th class="woocommerce-product-attributes-item__label"><?php esc_html_e( 'Thickness', 'my-text-domain' );?></th>
            <td class="woocommerce-product-attributes-item__value">{{{ data.variation.thickness }}}</td>
        </tr>
    </script>

    <script type="text/javascript">
        jQuery( document ).ready(function($) {
            $('form.cart')
                .on('found_variation', function( event, variation ) {

                    template     = wp.template( 'variation-template-extra-data' );

                    $template_html = template( {
                        variation: variation
                    } );

                    // Remove any existing rows.
                    $('#tab-additional_information').find('.woocommerce-product-attributes tr.extra-data').remove();
                    // Add new rows.
                    $('#tab-additional_information').find('.woocommerce-product-attributes tbody').append( $template_html );

                })
                .on( 'reset_data', function( event, variation ) {
                    $('#tab-additional_information').find('.woocommerce-product-attributes tr.extra-data').remove();
                });
        });
    </script>
<?php

}

字符串

相关问题