WooCommerce add shortcode to product attributes - wordpress

I want to add content to a product attribute.
This would mean that the value of a attribute can contain a shortcode.
The problem is that the shortcode itself is shown.
Here is what I did:
function prepare_temp( $atts ){
echo 'something';
}
add_shortcode( 'degrees', 'prepare_temp' );
This creates a shortcode but if I place this in the attribute value field it just shows [degrees]
Any way I can fix this by showing: something?

Related

ACF Image as Product Feature Image

How do I set an image that was uploaded via Advanced Custom Fields as the featured image of that woocommerce product only on the single product page?
This is how I set it up:
Step 1: I created an ACF image field with return format of 'Image Array'. Here's a screenshot of the settings.
ACF Image field settings
Step 2: I created conditional rules to only display the ACF input field on products for a specific category. That works. In the screen shot below you can see the field on the product page with an image uploaded to it.
ACF Image field on product page with uploaded image from media library
Step 3: Now this is the part I'm struggling with. I need to replace the woocommerce featured image with the uploaded ACF image when a user views the product page.
The screenshot below shows the product page with the woocommerce featured image but I want to replace that with the ACF image I uploaded in Step 2.
Product page on Front-end shows Woo featured image but I want to replace it with ACF image uploaded in Step 2
So I have the snippets plugin enabled where I can add code to swap the product images on the front-end (product page) but I need help with code please.
Any help will be appreciated, thanks!
I'm using OceanWP with WooCommerce.
In your ACF field set image to return ID or modify the function bellow if you need array or url value.
Add the following function to your active theme functions.php file.
function test($html, $post_thumbnail_id ) {
global $product;
$categories = $product->get_category_ids();
//Change 19 to the category id you need
if (in_array('19', $categories)):
//Change get_field to your field
$post_thumbnail_id = get_field('custom_thumb');
$html = wc_get_gallery_image_html( $post_thumbnail_id, true );
endif;
return $html;
}
add_filter( 'woocommerce_single_product_image_thumbnail_html', 'test',10,2);
The answer from Martin worked great. Here's the final code if anyone needs it. It's modified to fallback onto the default woocommerce featured image if the ACF image field is empty.
function acf_wall_art_feature_image($html, $post_thumbnail_id ) {
global $product;
$categories = $product->get_category_ids();
//Get the category id from admin/products/categories/(category_name) URL
if (in_array('30', $categories)):
//Get_field for ACF field name
$post_thumbnail_id = get_field('wall_art_featured_product_image');
//Fallback to woo featured image if no ACF image is set
if (!$post_thumbnail_id):
return $html;
else:
$html = wc_get_gallery_image_html( $post_thumbnail_id, true );
endif;
endif;
return $html;
}
add_filter( 'woocommerce_single_product_image_thumbnail_html',
'acf_wall_art_feature_image',10,2);

changing the display location of custom product field on a variable product page

i added a custom field to a variable product and displayed it on the front single product page with the help of an answer here
https://stackoverflow.com/a/55774613/14382875
by default the custom field is displayed on the front page above the add to cart button. but i want to move the custom field from there to right above the product meta data or the sku field in variable product. i was able to do this in simple product via hooks but the variable product code doesn't seem to be using any hook or i am missing something.
the code i used to display the variable product on front is this
function vp_variation_display_commodity_code( $data, $product, $variation ) {
if( $value = $variation->get_meta( 'jk_mpn' ) ) {
$data['price_html'] .= '<p class="vp-ccode"><strong>' . __("Manifacturer Part #", "woocommerce") .
': </strong>'.esc_html( $value ).'</small></p>';
}
return $data;
}```

OceanWP + Woo: How to add on STORE Site Default Attribute Value between Product name and Price

I have add Product in Woo: Theme OceanWP
ex.
Name product: Product1
attribute1: A
attribute2: B
attribute3: C
attribute4: D
On Product Page I see Name Product and all check attribute I have in Additional Information with is generate automated in Woo and automated add to Single Product Page.
On Main Store page I have Only Photo, Product Name, Price ( rest options I off in Woo like Tags, Categories, Stars, There is no options to show Attributes in main site store)
So… I Want to display only in Main Store Page ( and all sliders, sliders show this same information with I check in Woo) only one attribute value ex.
Picture
Product name
Just one ATTRIBUTE VALUE
PRICE
When I add this code:
add_action( 'woocommerce_after_shop_loop_item_title', 'additional_info_under_add_to_cart', 35 );
function additional_info_under_add_to_cart() {
global $product;
if ( $product && ( $product->has_attributes() ) ) {
wc_display_product_attributes( $product );
}
}
I see this:
https://prnt.sc/117f4e6
Ok, I see my All add attribute in each other products, but now, how to isolate just the value of attribute Model between Product name and Price, and there will be a fix it ??
You can use the ocean_before_archive_product_price hook.
// shows the "Model" attribute between the product name and price
add_action( 'ocean_before_archive_product_price', 'add_attribute_after_shop_loop_item_title', 1 );
function add_attribute_after_shop_loop_item_title() {
global $product;
// if the slug of the "Model" attribute is "model" use "pa_model"
if ( $product->get_attribute( 'pa_model' ) ) {
echo '<p>' . $product->get_attribute( 'pa_model' ) . '</p>';
}
}
The code has been tested and works. Add it to your active theme's functions.php.

Modify Woocommerce Shortcode [product id=""]

I'm trying to modify the existing wordpress woocommerce single product shortcode, (ie: [product id="2020"]
This shortcode will display the product by id and description.
My goal is to use a modified shortcode on a normal "page" & display the product with gallery (and it's thumbnails) as it would display per single product page.
function show_my_product_callback($atts)
{
$a = shortcode_atts(array(
'product_id' => ''),$atts );
echo get_the_title($a['product_id']);
echo get_the_content($a['product_id']);
}
add_shortcode( 'show_my_product', 'show_my_product_callback' );
on using it: [show_my_product product_id='3']
more reading resources.
get_post_meta - for the custom fields.
get_post_content - for the product description.
get_the_title - for the name of the product.

Get a sidebar widget that show products of the same categories in Woocommerce

I’m trying to set a sidebar in the single product page that show all products of the same categories as the product displayed.
That's how I proceed:
1) First I’ve created a sidebar called “Products_of_same_Category” to put in there a widget to show what I needed, then in function.php of my child theme I added the following snippet to execute php code in text widget:
// Enable PHP in widgets
add_filter('widget_text','execute_php',100);
function execute_php($html){
if(strpos($html,"<"."?php")!==false){
ob_start();
eval("?".">".$html);
$html=ob_get_contents();
ob_end_clean();
}
return $html;
}
2) Then when I saw that the snippet runs ok I added that code to test it:
<?php
$prod=get_the_term_list( $post->ID, 'product_cat');
echo $prod;
?>
And all worked fine, it gave me the exact name of the current category of the product displayed in the single product page.
3) So I've tried another test, deleting the previous code, to view if a shortcode translated in php should works in a widget too (writing at this time the exact category name requested, in this case "towels" - in the code below I substitute it with THE-CATEGORY-I-LIKE):
<?php echo do_shortcode('[product_category category=“THE-CATEGORY-I-LIKE” per_page="20" columns="1" orderby="title" order="desc"]'); ?>`
And all was again well done!
4) Finally I mixed all in this code to show the list of products of same categories but something goes wrong:
<?php $prod=get_the_term_list( $post->ID, 'product_cat', '', '', '' );
echo do_shortcode('[product_category category="'.$prod.'" per_page="20" columns="1" orderby="title" order="desc"]'); ?>
In last case the code doesn't display anything. I don't understand where I made mistakes, the syntax is wrong or the solving approach is illogical?
I really appreciate any help about this.
The problem is how you get the category slug. get_the_term_list will give you a formatted linked list of the categories, so it will display category names, not category slugs, which are different things. "Towels" would be your category name, but the category slug would be "towels". And product_category shortcode expect a slug, not a name.
The correct approach to get the category product slug is the following:
$terms = get_the_terms($post, 'product_cat');
if($terms && ! is_wp_error($terms)) {
foreach($terms as $term) {
echo do_shortcode('[product_category category="'.$term->slug.'" per_page="20" columns="1" orderby="title" order="desc"]');
}
}
This will display the products of all the categories associated to your product. See get_the_terms doc for reference.
In order to remove from the results the current product shown, you can make use of the woocommerce_shortcode_products_query filter. It isn't documented, but you can find it by looking at the product_category shortcode code located in includes/class-wc-shortcodes.php. In the product_category() method you will find the following line:
$return = self::product_loop( $query_args, $atts, 'product_cat' );
Where $query_args is a WP_Query parameters array. In the same class you will find the method product_loop() called here and see the following:
$products = new WP_Query( apply_filters( 'woocommerce_shortcode_products_query', $query_args, $atts ) );
So the query arguments are filtered - you will be able to work with that to add the desirated behavour. What you want to do is to use the post__not_in parameter to the query like this:
function remove_current_product_from_wc_shortcode($args, $atts) {
if(is_product()) { // check if we're on a single product page
$args['post__not_in'] = array(get_queried_object_id());
}
return $args;
}
add_filter('woocommerce_shortcode_products_query', 'remove_current_product_from_wc_shortcode');
This code should go in your theme functions.php - please not this is untested, so if it doesn't work look at the get_queried_object_id() return if it contain the current product ID.

Resources