在 Quform 挂钩中获取 Woocommerce 产品属性值
Getting a Woocommerce product attribute value in a Quform hook
我正在尝试获取 woocommerce 产品属性的值(名称为 dwnfile
)。
该值是一个 url,将被传递到将填充表单重定向的挂钩函数中。我使用的表格是 maid with Quform commercial plugin。
我已经收到插件作者关于我的表单设置的代码,但他们无法告诉我如何获取属性值。
这是他们给我的初始代码:
function my_populate_wc_attribute($form)
{
$value = 'some value'; // Get the value from WooCommerce somehow
$form->setValue('iphorm_3_2', $value);
}
add_action('iphorm_pre_display_3', 'my_populate_wc_attribute');
这是我尝试过的:
function my_populate_wc_attribute($form)
{
global $product;
$value = array_shift( wc_get_product_terms( $product->id, 'pa_dwnfile', array( 'fields' => 'names' ) ) );
$form->setValue('iphorm_3_2', $value);
}
add_action('iphorm_pre_display_3', 'my_populate_wc_attribute');
但是这个值好像是空的。
请问有什么帮助吗?
更新(最后):
问题仍然存在于 $product
全局对象(你没有进入这个钩子)。
你应该试试:
function my_populate_wc_attribute( $form ) {
global $post;
$term = wc_get_product_terms( $post->id, 'pa_nopeus', array( 'fields' => 'names' ) );
$value = current( $term );
$form->setValue('iphorm_3_2', $value);
}
add_action('iphorm_pre_display_3', 'my_populate_wc_attribute');
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
这应该有效。
But this is not the best way to do it. You should add a custom field in the product "General" setting tab, instead of using a product attribute to get an URL value.
UPDATE: Your main problem is that your form is NOT in your product page, so you can't get the post ID (or the product ID)
.
您可以尝试使用产品自定义字段(而不是产品属性)。
这是自定义字段的代码:
// Add Fields
add_action( 'woocommerce_product_options_general_product_data', 'add_product_general_custom_field' );
function add_product_general_custom_field() {
global $post;
// Get the selected value
$value = get_post_meta( $post->ID, '_dwnfile', true );
if( empty( $value ) ) $value = '';
woocommerce_wp_text_input( array(
'id' => 'dwnfile',
'label' => __( 'Download link', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( 'Add your download link …', 'woocommerce' ),
'value' => $value, // Displaying the selected value
) );
}
// Save Fields
add_action( 'woocommerce_process_product_meta', 'save_product_general_custom_field' );
function save_product_general_custom_field( $post_id ){
if( empty( $_POST['dwnfile'] ) ) return;
update_post_meta( $post_id, '_dwnfile', esc_attr( $_POST['dwnfile'] ) );
}
您将获得“下载 link”自定义字段(在产品常规选项中):
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
The form should be on the product page to get the product ID. if is not the case nothing will work.
So you should better tell what you are trying to do.
要获得 url 值,您将使用:
$value = get_post_meta( $post_id, '_dwnfile', true );
或者(如果未定义产品 ID):
$value = get_post_meta( get_the_id(), '_dwnfile', true );
Now you can ask authors of the plugin as this is a much more classic way: post meta data.
我正在尝试获取 woocommerce 产品属性的值(名称为 dwnfile
)。
该值是一个 url,将被传递到将填充表单重定向的挂钩函数中。我使用的表格是 maid with Quform commercial plugin。
我已经收到插件作者关于我的表单设置的代码,但他们无法告诉我如何获取属性值。
这是他们给我的初始代码:
function my_populate_wc_attribute($form)
{
$value = 'some value'; // Get the value from WooCommerce somehow
$form->setValue('iphorm_3_2', $value);
}
add_action('iphorm_pre_display_3', 'my_populate_wc_attribute');
这是我尝试过的:
function my_populate_wc_attribute($form)
{
global $product;
$value = array_shift( wc_get_product_terms( $product->id, 'pa_dwnfile', array( 'fields' => 'names' ) ) );
$form->setValue('iphorm_3_2', $value);
}
add_action('iphorm_pre_display_3', 'my_populate_wc_attribute');
但是这个值好像是空的。
请问有什么帮助吗?
更新(最后):
问题仍然存在于 $product
全局对象(你没有进入这个钩子)。
你应该试试:
function my_populate_wc_attribute( $form ) {
global $post;
$term = wc_get_product_terms( $post->id, 'pa_nopeus', array( 'fields' => 'names' ) );
$value = current( $term );
$form->setValue('iphorm_3_2', $value);
}
add_action('iphorm_pre_display_3', 'my_populate_wc_attribute');
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
这应该有效。
But this is not the best way to do it. You should add a custom field in the product "General" setting tab, instead of using a product attribute to get an URL value.
UPDATE: Your main problem is that your form is NOT in your product page, so you can't get the post ID
(or the product ID)
.
您可以尝试使用产品自定义字段(而不是产品属性)。
这是自定义字段的代码:
// Add Fields
add_action( 'woocommerce_product_options_general_product_data', 'add_product_general_custom_field' );
function add_product_general_custom_field() {
global $post;
// Get the selected value
$value = get_post_meta( $post->ID, '_dwnfile', true );
if( empty( $value ) ) $value = '';
woocommerce_wp_text_input( array(
'id' => 'dwnfile',
'label' => __( 'Download link', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( 'Add your download link …', 'woocommerce' ),
'value' => $value, // Displaying the selected value
) );
}
// Save Fields
add_action( 'woocommerce_process_product_meta', 'save_product_general_custom_field' );
function save_product_general_custom_field( $post_id ){
if( empty( $_POST['dwnfile'] ) ) return;
update_post_meta( $post_id, '_dwnfile', esc_attr( $_POST['dwnfile'] ) );
}
您将获得“下载 link”自定义字段(在产品常规选项中):
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
The form should be on the product page to get the product ID. if is not the case nothing will work.
So you should better tell what you are trying to do.
要获得 url 值,您将使用:
$value = get_post_meta( $post_id, '_dwnfile', true );
或者(如果未定义产品 ID):
$value = get_post_meta( get_the_id(), '_dwnfile', true );
Now you can ask authors of the plugin as this is a much more classic way: post meta data.