如何在 Woocommerce 中保存属性的自定义字段? [英] How can I save a custom field of an attribute in Woocommerce?
问题描述
旧
我正在尝试在 Woocommerce 的产品属性中创建自定义字段.这可以选择是否突出显示属性.例如:
https://i.stack.imgur.com/Ge76B.png
我想要做的是,我在后端指定为突出显示的字段在前端以某种方式显示.
到目前为止,我已经能够添加该字段,但我还没有弄清楚如何保存它.这就是我所拥有的:
add_action('woocommerce_after_product_attribute_settings','wcb_add_product_attribute_is_highlighted', 10, 2);add_filter('woocommerce_admin_meta_boxes_prepare_attribute', 'wcb_admin_meta_boxes_prepare_attribute', 10, 3);函数 get_attribute_highlighted($id, $i) {return get_post_meta( 1, "attribute_".$id."_highlighted_".$i, true);}函数 wcb_add_product_attribute_is_highlighted($attribute, $i=0) {$value = get_attribute_highlighted($attribute->get_id(), $i);?><tr><td><div class="enable_variation show_if_canopytour show_if_variable_canopytour"><label><input type="checkbox"类=复选框"<?php 已检查( $value, true );?>name="attribute_highlighted[<?php echo esc_attr($i);?>]"值=1"/><?php esc_html_e( '突出显示属性', $this->wcb );?></label>
</td></tr><?php}函数 wcb_admin_meta_boxes_prepare_attribute($attribute, $data, $i=0) {//更新if(array_key_exists(attribute_highlighted", $data) &&is_array($data[attribute_highlighted"])) {update_post_meta( 1, "attribute_".$attribute->get_id()."_highlighted_".$i, wc_string_to_bool($data["attribute_highlighted"][$i]));}}
我使用 WC_Product_Attribute
类的 offsetSet
和 offsetGet
方法尝试了 woocommerce_admin_meta_boxes_prepare_attribute
过滤器,但我不能了解它是如何工作的.我无法让它保存我的自定义值.
升级
我通过 wp_ajax_woocommerce_save_attributes
操作更改了 woocommerce_admin_meta_boxes_prepare_attribute
过滤器,它似乎有效.现在出现第一次保存后没有更新的问题.
我解释当前的问题:我有一个复选框,当单击被激活时,我保存并在重新加载状态时保持状态.如果现在我想停用它,保存状态后它不会保持为禁用状态,而是将其恢复为激活状态.
这是更新的代码:
add_action('woocommerce_after_product_attribute_settings', 'wcb_add_product_attribute_is_highlighted', 10, 2);add_action('wp_ajax_woocommerce_save_attributes', 'wcb_ajax_woocommerce_save_attributes', 10);函数 get_attribute_highlighted($id, $i) {全球 $post;$id = sanitize_title($id);$id = strtolower($id);$val = get_post_meta( $post->ID, "attribute_".$id."_highlighted_".$i, true);返回 !empty($val) ?$val : 假;}函数 wcb_add_product_attribute_is_highlighted($attribute, $i=0) {$value = get_attribute_highlighted($attribute->get_name(), $i);?><tr><td><div class="enable_highlighted show_if_canopytour show_if_variable_canopytour"><label><input type="checkbox"类=复选框"<?php 已检查( $value, true );?>name="attribute_highlighted[<?php echo esc_attr($i);?>]"值=1"/><?php esc_html_e( '突出显示属性', $this->wcb );?></label>
</td></tr><?php}函数 wcb_ajax_woocommerce_save_attributes() {check_ajax_referer('save-attributes', 'security');parse_str( $_POST['data'], $data );$post_id = absint( $_POST['post_id'] );if(array_key_exists(attribute_highlighted", $data) &&is_array($data[attribute_highlighted"])) {foreach($data["attribute_highlighted"] as $i => $val) {$attr_name = sanitize_title($data[attribute_names"][$i]);$attr_name = strtolower($attr_name);update_post_meta( $post_id, "attribute_".$attr_name."_highlighted_".$i, wc_string_to_bool($val));}}}
检查 Ink 的回答
过时
我找到了解决问题的方法.如果有人有用,我会分享代码.问候!
add_action('woocommerce_after_product_attribute_settings', 'wcb_add_product_attribute_is_highlighted', 10, 2);add_action('wp_ajax_woocommerce_save_attributes', 'wcb_ajax_woocommerce_save_attributes', 10);函数 get_attribute_highlighted($id, $i) {全球 $post;$id = sanitize_title($id);$id = strtolower($id);$val = get_post_meta( $post->ID, "attribute_".$id."_highlighted_".$i, true);返回 !empty($val) ?$val : 假;}函数 wcb_add_product_attribute_is_highlighted($attribute, $i=0) {$value = get_attribute_highlighted($attribute->get_name(), $i);?><tr><td><div class="enable_highlighted"><label><input type="hidden";name="attribute_highlighted[<?php echo esc_attr($i);?>]"值=0"/><输入类型=复选框"类=复选框"<?php 已检查( $value, true );?>name="attribute_highlighted[<?php echo esc_attr($i);?>]"值=1"/><?php esc_html_e( '突出显示属性', $this->wcb );?></label>
</td></tr><?php}函数 wcb_ajax_woocommerce_save_attributes() {check_ajax_referer('save-attributes', 'security');parse_str( $_POST['data'], $data );$post_id = absint( $_POST['post_id'] );if(array_key_exists(attribute_highlighted", $data) &&is_array($data[attribute_highlighted"])) {foreach($data["attribute_highlighted"] as $i => $val) {$attr_name = sanitize_title($data[attribute_names"][$i]);$attr_name = strtolower($attr_name);update_post_meta( $post_id, "attribute_".$attr_name."_highlighted_".$i, wc_string_to_bool($val));}}}
最后,我唯一需要添加到代码中的是一个与复选框同名但值为 0 的隐藏输入:]"值=0"/>
这是结果的图像(https://i.stack.imgur.com/VscT1.jpg).通过单击保存复选框的值保持不变.该值保存在您正在修改的帖子的 post_meta 中.如果您想在前端突出显示特定属性,这很有用.
感谢@LoicTheAztec 的帮助:)
Old
I am trying to create a custom field in the product attributes in Woocommerce. This to be able to select if an attribute is highlighted or not. For example:
https://i.stack.imgur.com/Ge76B.png
What I want to do is that the field that I specify in backend as highlighted is displayed in a certain way in the frontend.
So far I have been able to add the field, but I have not managed to figure out how to save it. This is what I have:
add_action('woocommerce_after_product_attribute_settings','wcb_add_product_attribute_is_highlighted', 10, 2);
add_filter( 'woocommerce_admin_meta_boxes_prepare_attribute', 'wcb_admin_meta_boxes_prepare_attribute', 10, 3);
function get_attribute_highlighted($id, $i) {
return get_post_meta( 1, "attribute_".$id."_highlighted_".$i, true);
}
function wcb_add_product_attribute_is_highlighted($attribute, $i=0) {
$value = get_attribute_highlighted($attribute->get_id(), $i); ?>
<tr>
<td>
<div class="enable_variation show_if_canopytour show_if_variable_canopytour">
<label><input type="checkbox" class="checkbox" <?php checked( $value, true ); ?> name="attribute_highlighted[<?php echo esc_attr( $i ); ?>]" value="1" /> <?php esc_html_e( 'Highlight attribute', $this->wcb ); ?></label>
</div>
</td>
</tr>
<?php
}
function wcb_admin_meta_boxes_prepare_attribute($attribute, $data, $i=0) {
// updated
if(array_key_exists("attribute_highlighted", $data) && is_array($data["attribute_highlighted"])) {
update_post_meta( 1, "attribute_".$attribute->get_id()."_highlighted_".$i, wc_string_to_bool($data["attribute_highlighted"][$i]) );
}
}
I tried the woocommerce_admin_meta_boxes_prepare_attribute
filter using the offsetSet
and offsetGet
methods of the WC_Product_Attribute
class but I can not understand how it works. I could not make it save my custom value.
Upgrade
I changed the woocommerce_admin_meta_boxes_prepare_attribute
filter by the wp_ajax_woocommerce_save_attributes
action and it seems to work. Now I have the problem that it is not updated after saving for the first time.
I explain the current problem: I have a checkbox that when clicked is activated, I save and when reloading the state it is maintained. If now I want to deactivate it, after saving the state it is not kept as disabled, it is put back as activated.
This is the updated code:
add_action('woocommerce_after_product_attribute_settings', 'wcb_add_product_attribute_is_highlighted', 10, 2);
add_action('wp_ajax_woocommerce_save_attributes', 'wcb_ajax_woocommerce_save_attributes', 10);
function get_attribute_highlighted($id, $i) {
global $post;
$id = sanitize_title($id);
$id = strtolower($id);
$val = get_post_meta( $post->ID, "attribute_".$id."_highlighted_".$i, true);
return !empty($val) ? $val : false;
}
function wcb_add_product_attribute_is_highlighted($attribute, $i=0) {
$value = get_attribute_highlighted($attribute->get_name(), $i); ?>
<tr>
<td>
<div class="enable_highlighted show_if_canopytour show_if_variable_canopytour">
<label><input type="checkbox" class="checkbox" <?php checked( $value, true ); ?> name="attribute_highlighted[<?php echo esc_attr( $i ); ?>]" value="1" /> <?php esc_html_e( 'Highlight attribute', $this->wcb ); ?></label>
</div>
</td>
</tr>
<?php
}
function wcb_ajax_woocommerce_save_attributes() {
check_ajax_referer( 'save-attributes', 'security' );
parse_str( $_POST['data'], $data );
$post_id = absint( $_POST['post_id'] );
if(array_key_exists("attribute_highlighted", $data) && is_array($data["attribute_highlighted"])) {
foreach($data["attribute_highlighted"] as $i => $val) {
$attr_name = sanitize_title($data["attribute_names"][$i]);
$attr_name = strtolower($attr_name);
update_post_meta( $post_id, "attribute_".$attr_name."_highlighted_".$i, wc_string_to_bool($val) );
}
}
}
Check Ink's answer
Outdated
I have found the solution to my problem. I share the code in case someone is useful. Regards!
add_action('woocommerce_after_product_attribute_settings', 'wcb_add_product_attribute_is_highlighted', 10, 2);
add_action('wp_ajax_woocommerce_save_attributes', 'wcb_ajax_woocommerce_save_attributes', 10);
function get_attribute_highlighted($id, $i) {
global $post;
$id = sanitize_title($id);
$id = strtolower($id);
$val = get_post_meta( $post->ID, "attribute_".$id."_highlighted_".$i, true);
return !empty($val) ? $val : false;
}
function wcb_add_product_attribute_is_highlighted($attribute, $i=0) {
$value = get_attribute_highlighted($attribute->get_name(), $i); ?>
<tr>
<td>
<div class="enable_highlighted">
<label><input type="hidden" name="attribute_highlighted[<?php echo esc_attr( $i ); ?>]" value="0" /><input type="checkbox" class="checkbox" <?php checked( $value, true ); ?> name="attribute_highlighted[<?php echo esc_attr( $i ); ?>]" value="1" /> <?php esc_html_e( 'Highlight attribute', $this->wcb ); ?></label>
</div>
</td>
</tr>
<?php
}
function wcb_ajax_woocommerce_save_attributes() {
check_ajax_referer( 'save-attributes', 'security' );
parse_str( $_POST['data'], $data );
$post_id = absint( $_POST['post_id'] );
if(array_key_exists("attribute_highlighted", $data) && is_array($data["attribute_highlighted"])) {
foreach($data["attribute_highlighted"] as $i => $val) {
$attr_name = sanitize_title($data["attribute_names"][$i]);
$attr_name = strtolower($attr_name);
update_post_meta( $post_id, "attribute_".$attr_name."_highlighted_".$i, wc_string_to_bool($val) );
}
}
}
In the end the only thing I had to add to my code was a hidden input with the same name as the checkbox but with a value of 0: <input type="hidden" name="attribute_highlighted[<?php echo esc_attr( $i ); ?>]" value="0" />
Here an image of the result (https://i.stack.imgur.com/VscT1.jpg). By clicking on save the value of the checkbox is maintained. The value is saved in a post_meta of the post that you are modifying. This is useful if you want to highlight a specific attribute in the front end.
I appreciate the help of @LoicTheAztec :)
这篇关于如何在 Woocommerce 中保存属性的自定义字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!