遍历订单项时如何检查自定义复选框产品元的值 [英] How to check value of Custom Checkbox Product Meta when looping through order items
问题描述
我在Dokan new-single-product.php模板中创建了一个自定义复选框元:
I created a custom checkbox meta in Dokan new-single-product.php template:
<div class="dokan-form-group">
<div class="dokan-input-group">
<?php dokan_post_input_box( $post_id, '_custom_field', array( 'label' => __('Custom Checkbox','dokan') ), 'checkbox' ); ?>
</div>
然后使用以下代码存储值:
Then used the following code to store the values:
add_action( 'woocommerce_product_options_general_product_data', 'wc_custom_add_custom_fields' );
function wc_custom_add_custom_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
// Checkbox
woocommerce_wp_checkbox(
array(
'id' => '_custom_field',
'label' => __('Custom Field', 'woocommerce' )
)
);
echo '</div>';
}
// Save Fields
add_action( 'dokan_process_product_meta', 'wc_custom_save_custom_fields' );
add_action( 'dokan_new_product_added', 'wc_custom_save_custom_fields' );
add_action( 'woocommerce_process_product_meta', 'wc_custom_save_custom_fields' );
function wc_custom_save_custom_fields($post_id) {
$woocommerce_wccaf_precio_por_ = $_POST['_custom_field'];
if( !empty( $woocommerce_wccaf_precio_por_) || empty( $woocommerce_wccaf_precio_por_))
update_post_meta( $post_id, '_custom_field', esc_attr( $woocommerce_wccaf_precio_por_ ) );
}
现在,我希望逻辑是:遍历订购物料,如果物料已选中自定义复选框,则将物料行价格总计添加到$ amount变量中.这将在订单完成时运行.
Now I want the logic to be: Go through Order Items, IF item has custom checkbox checked, add item line price total to $amount variable. This runs on order complete.
public function amount_total( $order_id ) {
// Get Order
$order = wc_get_order( $order_id );
$amount = 0;
// Loop through order items
$items = $order->get_items();
foreach ( $order->get_items() as $item_id => $item ) {
$custom_field = $item->get_meta('_custom_field');
//$custom_field = wc_get_order_item_meta( $item_id, '_custom_field', true );
// $custom_field = get_post_meta( $item_id, '_custom_field', true );
$line_total = $item->get_total();
if( isset($custom_field) ) {
$amount += $line_total;
}
}
在尝试找到的每个解决方案时,我都有3个不同的$ custom_field变量.但是什么都没用.
I've got 3 different $custom_field variables as I've been trying every solution that I've found. But nothing's working.
如果我检查Dokan的产品编辑菜单中复选框上的元素,它将显示以下内容:屏幕截图
If I inspect element on the checkbox in the product edit menu for Dokan, it shows this: screenshot
似乎有2个输入,其中1个隐藏.也许我应该尝试检索已检查"属性,而不是实际值?
It seems there are 2 inputs, 1 hidden. Maybe I should be trying to retrieve the attribute 'checked' rather than the actual values?
推荐答案
关于这方面还有更多文章,我一直认为我的问题有所不同,但是我做了以下工作,并使用我在桌面上找到的一般解决方案进行了修复.其他帖子:
There are a couple more articles on this and I have been thinking my problem is different however I did the following and fixed it with the general solution I found on the other posts:
-
检查了元密钥中实际上在数据库中存储了什么元值.
Checked what meta value is actually being stored in the database for the meta key.
检查过的get_post_meta方法的参数对方法是可读的(在我的情况下$ item_id无效,该方法需要产品的实际帖子ID)
Checked get_post_meta method's parameters are readable to the method (in my case $item_id was invalid, the method requires the actual post ID of the product)
检查条件是否实际上使用了正确的值:if($ custom_field =="yes")
Check the condition was actually using the right values: if( $custom_field == "yes" )
foreach函数的最终代码:
Final code for foreach function:
foreach ( $order->get_items() as $item_id => $item ) {
$product_id = $item->get_product_id();
$custom_field = get_post_meta( $product_id, '_custom_field', true );
$line_total = $item->get_total();
if( $custom_field == "yes" ) {
$amount += $line_total;
}
}
这篇关于遍历订单项时如何检查自定义复选框产品元的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!