gpt4 book ai didi

php - 将自定义维度字段添加到可变产品的每个变体设置

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:46:27 24 4
gpt4 key购买 nike

我正在尝试向每个产品变体设置添加“构建维度”字段。

这是我要完成的模拟: Here's a mock of what I'm trying to accomplish.

我遵循了以下这些提示,但它们并没有完全按照我的意愿行事:

那些正在将其添加到其他数据选项卡之一。每个变体我都需要它。每个变体都有一个构建尺寸和一个运输尺寸。

最佳答案

使用下面的 2 个钩子(Hook)函数,您将在模拟中得到您所期望的:

// Add variation custom "dimentions" fields
add_action( 'woocommerce_variation_options_dimensions','add_variation_options_built_dimensions', 10, 3 );
function add_variation_options_built_dimensions( $loop, $variation_data, $variation ){

$variation_built_lenght = get_post_meta($variation->ID,"_built_lenght", true );
if( ! $variation_built_lenght ) $variation_built_lenght = "";

$variation_built_width = get_post_meta($variation->ID,"_built_width", true );
if( ! $variation_built_width ) $variation_built_width = "";

$variation_built_height = get_post_meta($variation->ID,"_built_height", true );
if( ! $variation_built_height ) $variation_built_height = "";

?>
<p class="form-field form-row dimensions_field built_dimensions hide_if_variation_virtual form-row-last">

<label for="product_built_length"><?php
// translators: %s: dimension unit
printf(
__( 'Built dimensions (L&times;W&times;H) (%s)', 'woocommerce' ),
get_option( 'woocommerce_dimension_unit' )
);
?></label>

<?php echo wc_help_tip( __( 'Built length x width x height in decimal form', 'woocommerce' ) ); ?>
<span class="wrap">

<input id="product_built_length" placeholder="<?php esc_attr_e( 'Built length', 'woocommerce' ); ?>" class="input-text wc_input_decimal" size="6" type="text" name="built_lenght_<?php echo $loop; ?>" value="<?php echo esc_attr( $variation_built_lenght ); ?>" />

<input placeholder="<?php esc_attr_e( 'Built width', 'woocommerce' ); ?>" class="input-text wc_input_decimal" size="6" type="text" name="built_width_<?php echo $loop; ?>" value="<?php echo esc_attr( $variation_built_width ); ?>" />

<input placeholder="<?php esc_attr_e( 'Built height', 'woocommerce' ); ?>" class="input-text wc_input_decimal last" size="6" type="text" name="built_height_<?php echo $loop; ?>" value="<?php echo esc_attr( $variation_built_height ); ?>" />

</span>

</p>
<?php

}



//Save variation custom "dimentions" fields
add_action( 'woocommerce_save_product_variation','save_variation_options_built_dimensions', 10 ,2 );
function save_variation_options_built_dimensions( $variation_id, $loop ){

$built_lenght = $_POST["built_lenght_$loop"];
if(!empty($built_lenght))
update_post_meta( $variation_id, '_built_lenght', sanitize_text_field($built_lenght) );

$built_width = $_POST["built_width_$loop"];
if(!empty($built_width))
update_post_meta( $variation_id, '_built_width', sanitize_text_field($built_width) );

$built_height = $_POST["built_height_$loop"];
if(!empty($built_height))
update_post_meta( $variation_id, '_built_height', sanitize_text_field($built_height) );
}

代码位于您的事件子主题(或主题)的 function.php 文件中或任何插件文件中。

此代码已经过测试,适用于 WooCommerce 2.6.x 和 3+。

你会得到这个:

enter image description here


关于php - 将自定义维度字段添加到可变产品的每个变体设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45172481/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com