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_ ) );
}
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;
                    }
                }
I’ve got 3 different $custom_field variables as I’ve been trying every solution that I’ve found. But nothing’s working.
If I inspect element on the checkbox in the product edit menu for Dokan, it shows this: screenshot
It seems there are 2 inputs, 1 hidden. Maybe I should be trying to retrieve the attribute ‘checked’ rather than the actual values?
Advertisement
Answer
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.
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)
Check the condition was actually using the right values: if( $custom_field == “yes” )
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;
                }
            }