I am trying to get a product custom field ( ACF ) based on product ID in a post page.
I tried things like this $price[] = get_sub_field('price', $productId); but it doesn't seems to work when taking something else than the post id.
I managed to get the title though by doing like this <?php echo $product->get_title(); ?> but not sure on how to go to get the custom fields.
As we know $product->ID will not work anyway. So I think now your problem is how to get the product ID.
=> In Archive pages like shop or in Single product pages:
Using get_the_id() will give you mostly always the product id (the post ID).
$price= get_post_meta( get_the_id(), 'price', true );
reference link
You can also use global $product; with $product->get_id()
global $product;
$price = get_post_meta( $product->get_id(), 'price', true );
Related
I am trying to edit the single product's page add to cart button.
Successfully added a custom ajax call using LoicTheAztec's solution.
Code is working great. However I need to change $product_id in solution's code with product's custom field value named "custom_id".
Tried to parse it via $custom_data variable, something like:
$cart_item_key = WC()->cart->add_to_cart( $custom_data['custom_id'], $quantity, $variation_id, $variation, $cart_item_data );
Also tried to call it with another function:
function single_product_text_field() {
global $product;
// $cus_id = get_post_meta( $product->get_id(), 'custom_id', true );
$cus_id = $product->get_meta( 'custom_id' );
if ( ! empty($cus_id) )
return $cus_id;
}
Sadly I had no luck. Clicking the button did not proceed the ajax request.
If i replace $product_id with static custom field value, it's working great.
Unfortunately, I am out of ideas. In case you want to share one I would be at least grateful!
In my case, global $post page was declared.
so
$cus_id = get_post_field('custom_id', $product_id);
did the trick!
Appreciate all the guidance I can get on this one.
I am working on a shop using WordPress and Woocommerce.
I have a custom order form that I want to display on each product in a specific product category. My idea is to use an ACF field with true/false checkbox to determine whether the products in this category should have an order form or not.
The checkbox is located here:
So I don't want to name a specific product category in my code, instead I want to check if a single product belongs to a category which has a checked checkbox for the order form.
My issue is that I don't understand how to display the order form in a products post when checking the box, without the need to add it in all posts that should display it. Is it possible to access my ACF from a single product when it is added in the taxonomy product_cat? Any ideas?
I found the get_the_terms function but I am not sure how to use it.
This is how I've been trying to access my ACF:
function test() {
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
$queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
$checked_form = get_field('orderform', $taxonomy . '_' . $term_id);
if($checked_form) {
echo 'Checked!';
}else{
echo 'Not checked';
var_dump($terms);
var_dump($checked_form);
var_dump($term_id);
}
}
add_action('woocommerce_single_product_summary', 'test');
So I figured it out. I post my solution here in case someone else is looking for guidance.
After var_dumping get_the_terms( $post->ID, 'product_cat' ) I thought I might have to loop through each item in WP_term object and then used the term_id and taxonomy to retrieve the value from my ACF.
function woo_custom_order_form() {
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
if($terms) {
foreach($terms as $term) {
$term_id = $term->term_id;
$taxonomy = $term->taxonomy;
}
}
$checked_form = get_field('orderform', $taxonomy . '_' . $term_id);
if($checked_form) {
// Adds nav button that opens form
add_action('woocommerce_single_product_summary','add_nav_to_single_product_form', 20);
// Adds form on single product page
add_action('woocommerce_single_product_summary', 'add_form_to_single_product', 10);
// Filter hook for cf7 form on single product page
add_filter('wpcf7_form_tag', 'add_variations_to_order_form', 10, 2);
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
}
add_action( 'wp', 'woo_custom_order_form' );
I want to show woocommerce product short description and product data in rightside column of the product page. By default all woocommerce product page show product title, product pricing and short description on the top right corner of the page beside image placeholder but I want to move this fields to right side column and make this column sticky. Right now I have added short description and price manually
here is the ref image of product page
I tried to call this class="woocommerce-product-details__short-description" in column
i.e <div class="woocommerce-product-details__short-description">
but its is only working for buttons not for short descriptions and price
You can get the short description content using the custom shortcode code below.
add_shortcode( 'product_short_description', 'display_product_short_description' );
function display_product_short_description( $atts ){
$atts = shortcode_atts( array(
'id' => get_the_id(),
), $atts, 'product_short_description' );
global $product;
if ( ! is_a( $product, 'WC_Product') )
$product = wc_get_product($atts['id']);
return $product->get_short_description();
}
It can be used directly by using shortcode
[product_short_description]
or by calling it in product page
echo do_shortcode( "[product_short_description]" );
To get the product price you can use the following code in your product page
<?php $price = get_post_meta( get_the_ID(), '_price', true ); ?>
<p><?php echo wc_price( $price ); ?></p>
I am trying to show the price of a product in a post by code using HTML.
I am trying to utilize $product->get_price(). however, I need to be able to call the product by identifying it, using its code or something like it.
So Basically, all I want to do is to show the price of a particular product in the post by using a product ID as reference for example.
Any idea?
If you have the product's ID you can use that to create a product object:
$_product = wc_get_product( $product_id );
Then from the object you can run any of WooCommerce's product methods.
$_product->get_regular_price();
$_product->get_sale_price();
$_product->get_price();
Hope this one helps.
you use wc_get_product function :
$_product = wc_get_product( $product_id );
$_product->get_regular_price();
$_product->get_sale_price();
$_product->get_price();
Trying to get Product Data on custom Template By product ID, right now i have this code to get Product Title.
$productId = 164;
echo $p_title = get_the_title( $productId );
looking for Short Description, Price, Product Image, Product Url, Product Brand. Or might be loop will be better but that loop should work with product static ID.
Thanks in advance.
You would probably be better served by creating a new product object.
$productId = 164;
$product = wc_get_product( $productId );
echo $product->get_title();
echo $product->get_price_html();
Note, that the short description is merely the post's post_excerpt. If using outside of the loop (where $post is automatically defined) then you would need to get the post directly.
$post = get_post( $productId );
echo apply_filters( 'woocommerce_short_description', $post->post_excerpt );
or alternatively, if you've already defined the product object you could do
echo apply_filters( 'woocommerce_short_description', $product->post->post_excerpt );
since the WooCommerce product class will automatically define the $product->post property with get_post().
apply_filters() means that functions can be attached at this point to modify the content of $product->post->post_content. At a minimum, I know that wpautop() is attached to the woocommerce_short_description filter to create paragraph breaks.