Support

Account

Forum Replies Created

  • Okay – I’ve now found the issue.

    When adding a new field the first value is visible in the select on the product page in the control panel. However, this field does not contain the value. I, therefore, had to go into each and every product and select another value and then back to the value I wanted for the change to kick in.

    It works now. Thank you.

  • @hube2 Thank you for your answer. I will try to supply with more context.

    The idea is as follows: When a order is completed I use the woocommerce_thankyou action in WooCommerce to create an email as follows:

    add_filter('woocommerce_thankyou', [$this, 'create_email'], 10, 1);

    In my custom fields setup, I’ve created a field group that applies to all products. In the field group I can set a supplier for the product from predefined options in a single select.

    What I want is to access that supplier on a product level in the create_email function that runs after an order is completed. My approach is as follows:

    
    public function create_email($order_id)
        {
            global $product;
    
            $order = wc_get_order($order_id);
            $items = $order->get_items();
    
            $subject = 'NEW ORDER #' . $order->get_order_number();
            $header = 'New order: #' . $order->get_order_number();
    
            foreach ($items as $item_key => $item) {
                $product = $item->get_product();
    
                if (strpos($product->get_slug(), 'vareproeve')) {
                    continue;
                }
    
                $product_id = $item->get_product_id();
    
                 $main .= '<p>ID: ' . $product_id . '</p>';
    
                    if ($supplier = get_field("supplier", $item->get_product_id())) {
                        $main .= '<p><strong>' . __('Supplier: ') . ': </strong>' . $supplier . '</p>';
                    }
    
            }
    
           // Setup rest of email
            if ($main != '') {
                $body = $head . $main . $foot;
    
                $this::send_email($recipient, $subject, $header, $body, $headers, $attachments, $order, $order_id);
                update_post_meta($order_id, '_order_mail_sent', 'yes');
                unset($head);
                unset($main);
                unset($foot);
            }
        }
    
    
  • Sorry – this was a typo in the code example. In my running code it is $item.

Viewing 3 posts - 1 through 3 (of 3 total)