Modify Woocommerce Shortcode [product id=""] - wordpress

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.

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);

Wordpress how to link Custom Post Type directly to a Custom Field instead of the parent post page

I have a Custom Post type with two custom fields - Description and Attachment (to upload file/PDF).
When I complete this Custom Post I want the link to go directly to the attachment rather than the post page. I am using CPT UI and Custom Fields plugins to manage all this.
Does anyone know how I can create a custom post that will go directly to an attachment rather than the post page? I want to be able to display the title of each post on a page and have the title go to the attachment within the post.
I hope this makes sense and any help greatly appreciated!
This example assumes that you are using ACF to create the fields. And the field with the file gives its ID when requested (when creating a field in ACF there is an option to give ID or link)
add_filter( 'post_type_link', 'custom_post_permalink1', 10, 4 );
function custom_post_permalink1( $permalink, $post, $leavename, $sample ) {
// Change here to your post type name
if ( $post->post_type == 'your_post_type' ) {
$post_current_id = $post->ID;
// Change here 'file' to your custom field slug
if(get_post_meta($post_current_id, "file", true) ):
$PDF_ID = get_post_meta($post_current_id, "file", true);
$PDF_URL = wp_get_attachment_url( $PDF_ID );
$permalink = $PDF_URL;
endif;
}
return $permalink;
}

WooCommerce add shortcode to product attributes

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?

Woocommerce /Display Custom Field in Wordpress

I added some custom fields to the each product in Woocommerce, and I would like the data from the custom fields to show on the following receipt page (Order Details) after checkout is completed.
Just can't get it, not strong in php, why can't get anything. I've tried to use var_dump(get_post_custom($order->id)); i don't have my custom field in result.
Can somebody light me up?
Here is my code:
add_action( 'woocommerce_order_details_after_order_table', 'code_activation', 10, 1);
function code_activation($order){
echo '<p><strong>'.__('Activation code').':</strong> ' . get_post_meta( $order->id, 'activation_code', true ). '</p>';
}
You have added custom fields to the product. So custom fields will be associated with your product-id .
But you are trying to retrieve the custom fields using $order->id which is wrong. following code should help to retrieve the product id from order. And using the product id you can retrieve your custom field.
$orderItems = $order->get_items();
foreach($orderItems as $orderItem)
{
$product_id = $orderItem['variation_id'] ? $orderItem['variation_id'] : $orderItem['product_id'] );
}

Woocommerce customize the product name in the cart and order pages

Actually, I already got hook to customize the price in the cart page and all other page.
This is the hook to customize the product price
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );
function add_custom_price( $cart_object ) {
$custom_price = 10; // This will be your custome price
foreach ( $cart_object->cart_contents as $key => $value ) {
$value['data']->price = $custom_price;
}
}
Actually, I am using woocommerce plugin. I want a hook to customize the product name displayed in the cart page and all other pages next to cart page.
I want to customize the Product name , i want to add the some static attributes to product name displayed in the cart page, order page ,order details page and all the pages next to cart page.
Thanks in advance
I wanted to share this as I managed to figure out something for my needs. (I only ever have a single item in the order as I've customised WooCommerce for holiday bookings.)
<?php
add_action( 'woocommerce_product_title', 'add_custom_name' );
function add_custom_name( $title ) {
return 'test name';
}
?>
Hopefully someone can elaborate on this further or take what has been done with the custom price code an properly re-purpose it for product names.

Resources