Seb*_*b G 2 php wordpress product woocommerce
你如何通过它的名字获得产品?我知道你可以通过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);
Run Code Online (Sandbox Code Playgroud)
原因是,我需要在 - $$$产品中填写一个包含信息的选择框.
请试试这个
$product = get_page_by_title( 'Product Title', OBJECT, 'product' );
Run Code Online (Sandbox Code Playgroud)
get_page_by_title检索给定其标题的帖子.如果多个帖子使用相同的标题,则将返回ID最小的帖子.
句法:- get_page_by_title( $page_title, $output, $post_type );