0

I have a wordpress plugin which performs a count of purchased products in an order which are flagged with my custom metadata value. The custom metadata value is in the form of a checkbox within the product page. I'm using WooCommerce/WooCommerce subscriptions.

If a customer purchases 5 products, a count is done and then stored in a variable. Only products that have the custom metadata value checkbox are counted.

The count does not occur, as I believe my custom metadata value checkbox is not being detected when using get_meta(); within the loop.

Code below shows the metadata checkbox

/* Adding Extra field to product to Enable Traccar integration */

// Display Fields
add_action('woocommerce_product_options_general_product_data', 'wootraccar_product_custom_fields_add');
function wootraccar_product_custom_fields_add(){
    global $post;

    echo '<div class="wootraccar_product_custom_field">';

    // Custom Product Checkbox Field
    woocommerce_wp_checkbox( array(
        'id'        => '_wootraccar_integration',
        'desc'      => __('Check this box if Traccar integration is required for this product', 'woocommerce'),
        'label'     => __('Traccar Integration Required', 'woocommerce'),
        'desc_tip'  => 'true'
    ));

    echo '</div>';
}

// Save Fields
add_action('woocommerce_process_product_meta', 'wootraccar_product_custom_fields_save');
function wootraccar_product_custom_fields_save($post_id){
    // Custom Product Text Field
    $wootraccar_integration = isset( $_POST['_wootraccar_integration'] ) ? 'yes' : 'no';
        update_post_meta($post_id, '_wootraccar_integration', esc_attr( $wootraccar_integration ));
}

the for each loop

        $order = wc_get_order( $order_id );
        $email  = $order->get_billing_email();
        $name = $order->get_billing_first_name();

        $devicecount =0;

            foreach ( $order->get_items() as $item ) {
            if( $item->get_meta( '_wootraccar_integration', true )=='yes')
            {
                $devicecount = $devicecount +  $item->get_quantity();
            }
        }


1 Answer 1

0
{
    $userid="";
    $integration_status="";
      // Get an instance of the WC_Order object
        $order = wc_get_order( $order_id );
        $email  = $order->get_billing_email();
        $name = $order->get_billing_first_name();
        $items = $order->get_items();

        $devicecount =0;        

        foreach ( $items as $item ) {
            $product_name = $item->get_name();
            $product_id = $item->get_product_id();
            $product_variation_id = $item->get_variation_id();

            $productToBeIntegrated=get_post_meta($product_id,'_wootraccar_integration', true);
            //$integration_status.=$productToBeIntegrated;

            if ($productToBeIntegrated=="yes") {
                $devicecount = $devicecount +  $item->get_quantity();
                $integration_status.=$devicecount;          }
        }
1
  • 3
    It's not clear how this solves the users problem. Could you edit your answer and add in more detail on what this code does and how it solves the users problem?
    – Howdy_McGee
    Oct 17, 2021 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.