WooCommerce how to display specific "Additional Information" fields - wordpress

I am a total WooCommerce noob and I am struggling with something that I feel should be pretty simple...
How can I display specific "Additional Information" fields on a product page? I suppose I need to add some shortcode in the PHP files, but I am lost on what exactly to add...
I have ~15 additional info fields that I created under "Attributes" on the product.
I would like to display specific attributes on various parts of the page, but not all in one spot, much like the "Additional Fields" tab does.
I can't seem to find a straight answer and I am really struggling with this.

So, I realize there is a lack of information.
To further explain what I was trying to do, I was attempting to create a shortcode function in order to display specific "Attribute" fields from the woocommerce product.
In order to do this, I found a solution. This code can be added to your theme's function.php file:
// To diplay formula via shortcode within product page
add_shortcode( 'show_formula', 'show_additional_info_formula' );
function show_additional_info_formula() {
global $product;
$formula = $product->get_attribute('Formula');
?>
<table class="woocommerce-product-attributes shop_attributes">
<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--formula">
<th class="woocommerce-product-attributes-item__label"><?php echo ucfirst( 'Formula' ); ?></th>
<td class="woocommerce-product-attributes-item__value"><?php echo $formula; ?></td>
</tr>
</table>
<?php
}
Then, the shortcode to use, is (in this instance), [show_formula].
You can modify this to match your desired outcome.
Change:
add_shortcode( 'show_your_info', 'show_additional_info_attribute_field' );
$your_field = $product->get_attribute('actual_woocommerce_field_name');
<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--unique_name">
<th class="woocommerce-product-attributes-item__label"><?php echo ucfirst( 'word_to_be_displayed' ); ?></th>
<td class="woocommerce-product-attributes-item__value"><?php echo $your_field; ?></td>
Then, once modified, the shortcode you'd use:
[show_your_field]
This answer led me down the right path

Related

Woocommerce My Account Order page overriding is not working [duplicate]

I want to style the content of some order columns in the list of orders (My Account area).
The problem is, that every content starts without any elements which I could use for styling.
For example: I want to give the order status a background color like a badge.
At the moment the status is the only content in the column. Like this:
<td class="woocommerce-orders-table__cell woocommerce-orders-table__cell-order-status" data-title="Status">
Completed
</td>
I want to change it to this:
<td class="woocommerce-orders-table__cell woocommerce-orders-table__cell-order-status" data-title="Status">
<span class="badge badge-success">Completed</span>
</td>
The class is an option. I could also style the simple <span> based on he class of the <td>.
Is there any way to change the output of the columns without touching the template file?
It's a very crucial template and I don't want to change it for such a simple addition.
In the template I saw this action before the content of each column:
<?php if ( has_action( 'woocommerce_my_account_my_orders_column_' . $column_id ) ) : ?>
Is there a way to use it?
That is indeed the correct way, use the column id 'order-status' in this case
woocommerce_my_account_my_orders_column_{$column_id}
function my_callback( $order ) {
echo '<span class="badge badge-success">' . esc_html( wc_get_order_status_name( $order->get_status() ) ) . '</span>';
}
add_action( 'woocommerce_my_account_my_orders_column_order-status', 'my_callback', 10, 1 );

Add several fields in user-edit.php

I want to insert several fields 'User detail' page NOT when registering, only for the back-end.
I added some codes in user-edit.php and I made a field named 'Company Name' but was NEVER saved!!!
Could you let me know what is wrong or is needed for saving it?
<tr class="user-company-name-wrap">
<th><label for="company_name"><?php _e( 'Company Name' ); ?></label></th>
<td><input type="text" name="company_name" id="company_name" value="<?php echo esc_attr( $profileuser->company_name ); ?>" class="regular-text" /></td>
</tr>
user-edit.php is a core file, and you should never add code to core files. Your code will be erased if any updates to Wordpress happen. I think this stack exchange post should help you add the functionality you want. I would suggest reading through all the answers to find which one would work best, and recommend using Advanced Custom Fields:
https://wordpress.stackexchange.com/questions/214719/how-do-i-add-a-field-on-the-users-profile-for-example-country-age-etc

Woocommerce Cart - parsing shortcode within cart output

Bit of an unusual customisation in WooCommerce that I require...
I've modified the standard WooCommerce cart.php so that the customer can't edit the quantity or remove items from cart. Basically, the customer first visits a seating plan page, selects their seat, and this then adds the relevant ticket (which is just a WC product) to their cart, and displays the cart page.
I want a column in the cart output, which has a button next to every row (every ticket) directing the customer back to the relevant seating plan for that ticket.
I've stored the seating plan shortcode in the WooCommerce product details, and I'm able to recall this in cart.php, and it displays in the right place as text using this line of code:
<td class="product-seating" data-title="<?php esc_attr_e( 'Seating Plan', 'woocommerce' ); ?>">
<?php echo the_field('wccaf_seating_plan_link', $product_id) ; ?>
</td>
This returns the following text nicely under a Seating Plan column in the cart for every ticket:
[tc_seat_chart id="3818" show_legend="true" button_title="Select your seat(s)" subtotal_title="Subtotal" cart_title="Continue to Checkout"]
However, what I actually want it to do is parse this shortcode, which should create a nice button with a link to the correct seating plan.
How would I make it parse the shortcode and not just display it as text? I tried playing around with do_shortcode but didn't have any luck.
Try this:
<td class="product-seating" data-title="<?php esc_attr_e( 'Seating Plan', 'woocommerce' ); ?>">
<?php $seating_plan = get_field('wccaf_seating_plan_link', $product_id) ;
echo do_shortcode($seating_plan);
?>
</td>

Save editor's value in user meta and retrieve it

I am making user meta fields. I have to implement a field named business_profile and implement editor on it. I have implemented the editor but I can't seem to either save its value or retrieve it. Below is my code.
<tr>
<th>
<label for="address">Business Profile</label></th>
<td><?php
$content = get_the_author_meta('business_profile', $user->ID);
$editor_id = 'mycustomeditor';
wp_editor($content, $editor_id);
?>
</td>
</tr>
And for update:
update_usermeta($user_id, 'business_profile', $_POST['business_profile']);
Where am I wrong?
Your code should read
update_usermeta($user_id, 'business_profile', $_POST['mycustomeditor']);
since mycustomeditor is the $editor_id than this is how the $_POST variable would be named .
business_profile on the other hand is just the meta_data for user or user_meta name .
It appears you just confused the two .

Wordpress save metabox checkboxes selection

Having an issue whereby the checkboxes inside a metabox aren't saving. I can save one value and have it return the value of checked to the checkboxes, just not multiple values, which I need. I've tried adding a foreach loop to the update_post_meta code but it didn't work. Slightly confused as to where I'm going wrong.
checkbox code is:
$areas = $wpdb->get_results("SELECT * FROM locations ORDER BY locationName ASC");
if( count($areas) ) :
?>
<div id="locationAssignedBoxes" size="1">
<?php
foreach($areas as $area) :
?>
<input type="checkbox" id="locationAssigned" name="locationAssigned" value="<?php echo $area->id; ?>"<?php if(get_post_meta($post->ID, 'locationAssigned', true) == $area->id) { ?> checked="checked"<?php } ?> /> <?php echo $area->locationName; ?><br>
<?php
endforeach;
?>
</div>
<?php
endif;
?>
Update_post_meta code is:
update_post_meta($post->ID, 'locationAssigned', $_POST['locationAssigned']);
Thanks very much!
This isn't a cut-and-paste answer to your question, but it should help. (I'm trying to solve a similar problem.)
Your problem in general: update_post_meta() saves a single meta value, not a collection of values. Because you want to save the values of multiple checkboxes, you have two choices:
Call update_post_meta() once for each of the checkboxes, creating a
collection of meta values associated with the post.
Combine all of the checkbox values into a single string and save those as a single value with one update_post_meta() call.
These two earlier questions are related and might point you in the right direction.
Save checkboxes of a metabox with a foreach loop (invalid argument)
Listing Pages With Checkboxes In a Metabox (and saving them)

Resources