你怎么能通过它的名字得到一个产品?我知道您可以通过 ID 和 SKU 来完成,但对于我目前的情况,我需要通过产品名称来获取产品。我一直在寻找它,似乎无法找到该功能。
我的功能出现在单个产品页面上,但我需要从中获取数据的产品不会是用户正在查看的当前产品。相反,它将与当前产品具有相同的名称,只是以不同的符号结尾。 (用户将查看的产品以“-r”结尾,而我需要搜索的产品以“-$$$”结尾)
到目前为止,在我的 Functions.php 中:
function fill_refurbished_addon_selectbox(){
//get id of current product
global $product;
$id= $product->get_id();
//get name of product
$currentName = get_the_title($id);
//remove -r, add -$$$ and store in var
$searchFor = substr($currentName, 0, -2) . "-$$$";
echo $searchFor;
//find $$$ product by title
$coreCharge = ***GET PRODUCT BY TITLE ($searchFOr)***;
//get price of $$$ product, store in var
//populate dropbox
}
add_action('woocommerce_before_add_to_cart_button', 'fill_refurbished_addon_selectbox', 20);
原因是,我需要用来自 -$$$ 产品的信息填充一个选择框。
最佳答案
请试试这个
$product = get_page_by_title( 'Product Title', OBJECT, 'product' );
get_page_by_title 检索给定标题的帖子。如果多个帖子使用相同的标题,则返回 ID 最小的帖子。
语法:-
get_page_by_title( $page_title, $output, $post_type );
关于php - WooCommerce - 按标题获取产品?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45036319/