• Resolved calle81

    (@calle81)


    How can I get the ID of a product based on its name?

    Let me explain, I would like a PHP snippet that allows me to get the id of a product from its name.

    Example I have a product called “Maglia Rossa” how can I know what its id is?

    Thank you.

Viewing 3 replies - 1 through 3 (of 3 total)
  • function my_function() {
        $args = array(
          'title' => 'Maglia Rossa'
        );
        $products = wc_get_products( $args );
        $nr_products = count( $products );
        switch( $nr_products ) {
          case 0:
            // no products with that title
            $product_id = 0;
            break;
          case 1:
            // only 1 product with that title
            $product = $products[0];
            $product_id = $product->get_id();
            break;
          default:
            // multiple products with that title
            // ambiguous
            $product_id = 0;
        }
      }

    Hi @calle81

    Thanks for reaching out!

    I understand that you would like to retrieve the product ID base on the product name.

    This is a bit of a complicated topic that would need some customization to address. Unfortunately, custom coding is not something we can assist with directly. However, I’ll keep this thread open for a bit to see if anyone from the community can lend a hand.

    If you have any other questions related to development or custom coding, don’t hesitate to reach out to some of the great resources we have available for support. The WooCommerce community is filled with talented open-source developers, and many of them are active on the channels listed below:

    Meanwhile, I did some research and found this article could be a good starting point: WooCommerce: Get Product Info (ID, SKU, $) From $product Object

    Hope this helps!

    Plugin Support Gabriel – a11n

    (@gabrielfuentes)

    Since we haven’t heard from you for a while, I’m marking this as resolved. We’re always here to help.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How can I get the ID of a product based on its name?’ is closed to new replies.