How can you get a product by it's name? I know you can do it by ID and SKU, but for my current situation I need to grab a product by its title. I've been looking it up and can't seem to find the function.
My function occurs on the single product page, but the product I need to get data from WILL NOT be the current product that the user is looking at. Instead it will have the same name as the current product, just ending with a different symbol. (the product the user will be looking at ends with "-r" and the one I need to search for ends with "-$$$")
So far in my 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);
The reason is, I need to fill a select box with info from the -$$$ product.
please try this
$product = get_page_by_title( 'Product Title', OBJECT, 'product' );
get_page_by_title retrieves a post given its title. If more than one post uses the same title, the post with the smallest ID will be returned.
syntax:- get_page_by_title( $page_title, $output, $post_type );
Related
I want to get the value of the category and say that if I am in a specific category such a text will appear and if it is not then another text will appear
$categories = get_the_category();
$category_id = $categories[0]->cat_ID;
if($category_id == 83){
echo '123';
}else{
echo '1234';
}
This is what I tried to do and it doesn't work for me. Is there a way to make it work well?
editing:
It is important for me to note that I want to display within the post the number of the category to which the post is associated.
WordPress Get Category ID from Slug
Here we pass the $slug to WordPress’ get_category_by_slug() function. That gives us an object of useful category data, including the ID, which we then call directly:
$slug = 'sales'; // assume Sales is category name you can get cat id from slug name
$cat = get_category_by_slug($slug);
$catID = $cat->term_id;
There's a number of posts on here attempting to solve this however, I haven't been able to find a solution that works.
I'm attempting to get the URL of the thumbnail image, associated with the product attribute term of the product displayed on a single-product page, and then display the thumbnail below the product meta, with a URL to the term's archive.
The hook that I'm using is as follows:
add_action('woocommerce_product_meta_end','product_brand_image');
Then I'm defining the function used in the hook:
function product_brand_image() {
}
The attribute name is "Brand", and the slug is "brand".
I've managed to get as far as getting the term ID using the following:
function product_brand_image() {
global $product;
$attributes = $product->get_attributes();
$attr_id = $attributes['pa_brand']['options'][0];
}
I've prnt'd this and it returns the term ID, which I've confirmed to be correct.
I'm struggling to now use this to get the associated term thumbnail URL, and term archive slug to be able to insert the thumbnail and then link it to the archive.
Looking to solve this programmatically without using another plugin.
UPDATE:
Ok, so using #Barrie Dawson comment as a guide, I am now up to this point:
function product_brand_image() {
global $product;
$attributes = $product->get_attributes();
$attr_id = $attributes['pa_brand']['options'][0];
$termMeta = get_term_meta($attr_id);
if (is_array($termMeta) && array_key_exists('product_search_image_id', $termMeta)) {
$post_id = $termMeta['product_search_image_id'][0];
$postData = get_post($post_id);
}
}
I have found that attribute term thumbnails are stored in table wp_posts. The 'product_search_image_id' field links to the ID field in wp_posts table. The URL of the thumbnail associated to the term is then listed under the 'guid' column in this same table. I need some assistance with the PHP to extract this.
<?php
function product_brand_image() {
global $product;
$attributes = $product->get_attributes();
$attr_id = $attributes['pa_brand']['options'][0];
$termMeta = get_term_meta($attr_id);
if (is_array($termMeta) && array_key_exists('thumbnail_id', $termMeta)) {
$termThumbnail = get_the_post_thumbnail_url($termMeta['thumbnail_id']);
}
}
you can also request available sizes see: https://developer.wordpress.org/reference/functions/get_the_post_thumbnail_url/
And here is the final solution I arrived at:
add_action('woocommerce_product_meta_end','product_brand_image');
function product_brand_image() {
global $product;
$attributes = $product->get_attributes();
$attr_id = $attributes['pa_brand']['options'][0];
$term = get_term($attr_id);
$termSlug = $term->slug;
$termMeta = get_term_meta($attr_id);
if (is_array($termMeta) && array_key_exists('product_search_image_id', $termMeta)) {
$post_id = $termMeta['product_search_image_id'][0];
$postData = get_post($post_id);
$prod_image_url = $postData->guid;
echo '<span class="product_brand_image"><img src="'.$prod_image_url.'" /></span>';
}
}
This displays the thumbnail of the term of the product attribute below the product meta, and links it to the archive page for that term. In this instance, it links to a shop page (archive) with all products associated to a particular 'brand'. See screenshot below.
Screenshot
I have an advanced custom field set up to show on a woocommerce subcategory that allows the user to define a colour via the color picker field.
This field will apply that colour to a number of elements related to that sub category (Styling the sub category thumbnail, the product page itself etc).
Im currently using as per the the ACF documentation this code to pull the field in and display it on the subcategory page:
$term = get_queried_object();
$color = get_field('colour', $term); // Get ACF Field
This works fine until it comes to the parent category for the sub pages. I am unable to call the field in for the sub categories of the parent. I understand I need to use get_terms(). I am unable ot get that working though.
This is some code I found which I have added to the loop on content-product_cat.php. However it just breaks the woocommerce loop. What would I need to do to this code to get the parent category page to show all the child subcategories each with its related color field?
// current term
$current_term = get_queried_object();
// child terms
// this returns an array of terms
$args = array(
'taxonomy' => 'YOUR TAXONOMY HERE',
'parent' => $current_term->term_id,
// you may need other arguments depending on your needs
);
$child_terms = get_terms($args);
// you need to maybe loop through the child terms gotte
// to pick which one you want to use
// I'm assuming that you only want to use the first one
$child_term = false; // set it to false to begin with
// we'll use this later
if ($child_terms) {
$child_term = $child_terms[0];
}
// make a decision
if ($child_term) {
// get field value(s) from child term
$color = get_field('color', $child_term);
} else {
// get field value(s) from current term
$color = get_field('color', $current_term);
}
// do something with the values
echo $color;
I found the solution here:
https://wordpress.stackexchange.com/a/341632
add_action('woocommerce_after_subcategory_title', 'wpse_add_custom_text_under_category_title', 10);
function wpse_add_custom_text_under_category_title($category) {
$term_id = 'product_cat_'.$category->term_id;
the_field('krotki_opis_kategorii', $term_id);
}
From Alex Uleberg:
get_queried_object_id on shop archive page it will return the id of the page and not the category. When you use the hook, the $category object will be passed in through the hook.
I'm building out custom landing pages for products in WooCommerce and I'd like to get the product price amongst other things in order to display them on the landing page.
Each landing page has some custom fields which allow the WP Admin to add in content, for the landing page as well as the product ID, which will then be used to generate the product price, checkout URL etc..
I can't get the wc_get_product(); to work with my custom field or a variable built off that. It only works when I use a straight ID. I think there's something I'm not understanding about how variables work within PHP. Here's my code.
<?php
//Gets the course ID from the custom field entered by user
$courseID = the_field('course_id');
// This line is where the problem is...
$_product = wc_get_product('$courseID');
// If I replace the line above with this line
// $_product = wc_get_product('7217');
// everything works great, but that does not let
// each landing page function based on the custom fields where the user determines
// the product ID they are selling on that landing page.
// Get's the price of the product
$course_price = $_product->get_regular_price();
// Output the Course price
?> <span class="coursePrice">$<?php echo $course_price;?></span>
Update
I get the following error using wc_get_product( $courseID ); or get_product( $courseID );:
Fatal error: Call to a member function get_regular_price() on a non-object in ...
Update related to your recent comment. The 2 ways to explore:
1) Instead of you should try to use to get the product object (avoiding the error):
$courseID = the_field('course_id');
// Optionally try this (uncommenting)
// $courseID = (int)$courseID;
// Get an instance of the product object
$_product = new WC_Product($courseID);
2) Alternatively if this doesn't work, you should try to use get_post_meta() function to get the product price (or any product meta data) this way:
<?php
//Gets the course ID from the custom field entered by user
$courseID = the_field('course_id');
// Get the product price (from this course ID):
$course_price = get_post_meta($courseID, '_regular_price', true);
// Output the Course price
?> <span class="coursePrice">$<?php echo $course_price;?></span>
This time you should get displayed the price with one or the other solutions.
Update: May be Also you need to convert $courseID to an integer variable.
Because you need to use your variable $courseID inside wc_get_product() (without the 2 ') function this way:
<?php
//Gets the course ID from the custom field entered by user
$courseID = the_field('course_id');
// Optionally try this (uncommenting)
// $courseID = (int)$courseID;
// Here
$_product = wc_get_product( $courseID );
$course_price = $_product->get_regular_price();
// Output the Course price
?> <span class="coursePrice">$<?php echo $course_price;?></span>
This should work now.
You can try this out :
$courseID = the_field('course_id');
$product = get_product( $courseID );
I figured out the answer after running through the possible solution routes that #LoicTheAztec supplied in his response. None of these worked and so I assumed something else was up.
I use Advanced Custom Fields to add custom fields in the back end and I was using ACF's the_field() in order to create my variable. That is incorrect usage of that function as it's designed to display the field, (it's basically using php's echo). To work with these custom field's you need to use ACf's get_field() which is to use it to store a value, echo a value and interact with a value.
Once I switched to setting my $courseID to this..
$courseID = get_field('course_id');
Everything worked. My code worked, and all #LoicTheAztec's code approaches also worked.
I have same product linked to different sub-category and I need save them separately and not adding in to cart/checkout etc…
I think the good way is to save them with variation named "category_name" with value [$product_category] to differentiate them in the loop and showing them in the cart/checkout pages like in this example :
Room_1 (main category)
Wall_works (subcategory)
Cleaning_windows hours Q.ty 4 (product)
———————————————————————————————————————
Room_2 (main category)
Wall_works (subcategory)
Cleaning_windows hours Q.ty 8 (product)
———————————————————————————————————————
Room_3 (main category)
Wall_works (subcategory)
Cleaning_windows hours Q.ty 8 (product)
And NOT like now as this (in 1 line):
Cleaning_windows hours Q.ty 20
I found online, this snippet for retrieving the main_category name but I dont know how use and save the value, and how to show it in cart/checkout like in my example above:
// retrieve category name
function get_product_category_by_id( $category_id ) {
$term = get_term_by( 'id', $category_id, 'product_cat', 'ARRAY_A' );
return $term['name'];
}
$prod_terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($prod_terms as $prod_term) {
// gets product cat id
$product_cat_id = $prod_term->term_id;
// gets an array of all parent category levels
$product_parent_categories_all_hierachy = get_ancestors( $product_cat_id, 'product_cat' );
// This cuts the array and extracts the last set in the array
$last_parent_cat = array_slice($product_parent_categories_all_hierachy, -1, 1, true);
foreach($last_parent_cat as $last_parent_cat_value){
// $last_parent_cat_value is the id of the most top level category, can be use whichever one like
}
}
$product_category = get_product_category_by_id( $last_parent_cat_value );
I need to create product variation with value => $product_category saved by default in all products when added to cart, without creating/inserting variations for all products (when created in back end) and without showing this variation in simple product page (front end).
How can I achieve this?
Thanks in advance for all help.