I want to be able to target certain product-pages with CSS based on the SKU of the product they have on. Therefore I need to add the SKU as a body class for all Woocommerce products. How would I accomplish this?
//Add Woocommerce body classes
add_filter('body_class','sku_woocommerce_body_classes');
function sku_woocommerce_body_classes($classes){
global $post;
$product = get_product( $post->ID );
if ( $product->product_type == 'simple');
$classes[] = $product->get_sku();
return $classes;
}
You weren't clear on what type of pages to target, but the if statement will get you where you need to go in order to target them.
Related
I'm trying to display a badge with the text "Exclusive" to a specific product in the shop page or category archive or whenever this specific product loop item is displayed.
Yet I have tried to add_action before_shop_loop_item but the problem is that the $product variable does not contain the object. I was thinking of $product->get_id() and if it matches the product id to apply some HTML to that specific product loop item.
add_action('woocommerce_before_shop_loop_item', 'add_custom_badge', 1);
function add_custom_badge( $product ) {
if ( $product->get_id() === 123 ) {
echo '<script>console.log("add_custom_badge")</script>';
}
}
BTW get_id() cannot executed because $product looks like empty. So that's where I stack.
Yeah, and the location I want to print the HTML is woocommerce_before_shop_loop_item - right before the sales badge.
Any suggestions on how to filter through loop items?
$product is not passed to the callback function by default, at the woocommerce_before_shop_loop_item hook. That's why it doesn't work
Use global $product instead
So you get:
function action_woocommerce_before_shop_loop_item() {
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
if ( $product->get_id() == 123 ) {
echo '<script>console.log("add_custom_badge")</script>';
}
}
}
add_action( 'woocommerce_before_shop_loop_item', 'action_woocommerce_before_shop_loop_item', 10 );
I would like to loop throught all products in shop/category page and add special css class to product if it is featured. I have tried to do this with add_filter hook but it seems I am to dumb to get it working.
You were right about using a filter. I don't know which filter you were trying but you would edit the post_class and add a class if the post type is a product and has the specific category you want.
add_filter( 'post_class', 'namespace_featured_posts' );
function namespace_featured_posts( $classes ) {
if( 'product' == get_post_type() && has_term( 'featured', 'product_cat') ) {
$classes[] = 'featured-post';
return $classes;
}
}
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();
Background:
My site sell keytags of a variety of styles (inventory contains 750+) but tags can only be sold as groups of three because that is how the inventory arrives. To get customers to only have the option to buy the group of three we created product bundles or sets (a separate product that contains 3 of a style) and I'm attempting to present that using the wordpress product shortcode replacing the add to cart button for the single tag style. The single tags and the corresponding bundle have similar sku names with the difference being a k at the end of the sku of bundles.
Problem:
Below is the code that I'm using to check to see if the product is a single tag and if it is i'm inserting the single product shortcode where the add to cart was. It will not display the shortcode on the product pages
function add_content_after_addtocart(){
$current_product_id = get_the_ID();
$product = wc_get_product( $current_product_id );
$bundSku = $product->get_sku();
$bundSku .= "k";
if( $product->is_type('simple') ){
echo do_shortcode('[product size="small" sku="{$bundSku}"]');
}
}
add_action('woocommerce_after_add_to_cart_button','add_content_after_addtocart');
Replace this line:
echo do_shortcode('[product size="small" sku="{$bundSku}"]');
By this one:
echo do_shortcode("[product size='small' sku='{$bundSku}']");
It should work now… So your functional code will be:
add_action('woocommerce_after_add_to_cart_button','add_content_after_addtocart');
function add_content_after_addtocart() {
$current_product_id = get_the_ID();
$product = wc_get_product( $current_product_id );
$bundSku = $product->get_sku();
$bundSku .= "k";
if ($product->is_type('simple')) {
echo do_shortcode("[product size='small' sku='{$bundSku}']");
}
}
Code goes in function.php file of your active child theme (or theme). Tested and works.
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.