简体   繁体   English

在WooCommerce中从其SKU获取产品的网址

[英]Get the Url of a product from its SKU in WooCommerce

The question is simple, if I know the SKU of my product and nothing else, how can I retrieve the url/permalink to that item? 问题很简单,如果我只知道产品的SKU,而又不知道其他什么,如何获取该项目的url /永久链接?

This is commonly useful for third party integration. 这通常对于第三方集成很有用。

You can use dedicated function wc_get_product_id_by_sku( $sku ) where $sku argument is the SKU of your product. 您可以使用专用函数wc_get_product_id_by_sku( $sku ) ,其中$sku参数是产品的SKU。

It will return the product ID. 它将返回产品ID。

Reference: Function wc_get_product_id_by_sku 参考: 函数wc_get_product_id_by_sku

Then to get the permalink: 然后获得永久链接:

 $sku = 'Gh2563'; // SKU example to be replaced by the real SKU of the product $product_id = wc_get_product_id_by_sku( $sku ); $link = get_permalink( $product_id ); 
     function get_product_by_sku( $sku ) {

       global $wpdb;

     $product_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $sku ) );

     $url = get_permalink( $product_id );

     return $url;
        }

hope this help. 希望有帮助。

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM