Woocommerce Cart - parsing shortcode within cart output - wordpress

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>

Related

Add dropdown list to woocommerce cart and retrieve value on checkout?

I'd like to modify cart.php to contain a dropdown list of "checkout agents" and then retrieve that data when checking out to redirect to the correct agent (different URLs).
I already have cart.php in the child theme. I have created a cart-custom-checkout.php page to handle retrieving the cart contents and redirecting (wp_redirect) to an agents URL (I tested a single agent by building the URL from the cart contents and it works great).
I enabled the custom checkout via modifying the child theme functions.php as follows:
// enable custom checkout for woocommerce
add_shortcode( 'cart_custom_checkout', 'cart_custom_checkout_function' );
function cart_custom_checkout_function() {
wc_get_template('cart/cart-custom-checkout.php');
}
Then used that shortcode on the checkout page.
Now that everything works like I want in concept, the final step is to create the dropdown list in the cart.php so one of several agents can be chosen. From there I then need to somehow retrieve that in the cart-custom-checkout.php page. If it were part of the WC() cart data or something like that would be great.
Can someone help explain how I can do what I'm wanting to do?
TIA!!
Update: I've gone a little further and added my dropddown list as follows:
add_action('woocommerce_after_cart_totals', 'woocommerce_cart_add_resellers');
function woocommerce_cart_add_resellers() {
wc_get_template('cart/cart-add-reseller.php');
}
The cart-add-reseller.php contents are:
<div style="margin-top:5px">
<select name="agent">
<option value="Agent1">Use Agent1</option>
<option value="Agent2">Use Agent2</option>
</select>
</div>
Now just need to get what agent value is in the cart-custom-checkout.php page. How do I do that?
Update2: I am not getting any $_POST data when clicking the proceeded to checkout button.
// enable custom checkout for woocommerce
add_shortcode( 'cart_custom_checkout', 'cart_custom_checkout_function' );
function cart_custom_checkout_function() {
var_dump($_POST);
wc_get_template('cart/cart-custom-checkout.php');
}
It's empty.
Update 3: I've check $post which just gives me the checkout page itself with the shortcode in it, $_POST is empty, $_SESSIONS is empty, $_GET is empty. I'm not sure where the $_POST data went, maybe some javascript or something like that which I know nothing about. If I could just know a filter or action or something to give me the $_POST data, it will have the value I need. So I give up for now.
Any help would be appreciated.
If I fully understand what you have, if you were to add the following in functions.php, it will add the required field to the cart page and allow it to be accessible via $_POST on the checkout page. This essential replaces the default Proceed to Checkout button, with a new one that includes the select field and a post action.
add_action( 'woocommerce_proceed_to_checkout', 'add_agent_before_proceed_to_checkout', 15 );
function add_agent_before_proceed_to_checkout() {
remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
?>
<form id="checkout_form" method="POST" action="<?php echo esc_url ( wc_get_checkout_url() ); ?>">
<div style="margin-top:5px">
<select name="agent">
<option value selected="selected">-- Select an Agent --</option>
<option value="Agent1">Use Agent1</option>
<option value="Agent2">Use Agent2</option>
</select>
</div>
<button type="submit" class="checkout-button button alt wc-forward" style="width:100%;"><?php
esc_html_e( 'Proceed to checkout', 'woocommerce' ) ?></button>
</form>
<?php
}
The reason what you had added to cart didn't work, is the Proceed to Checkout button (woocommerce\templates\cart\proceed-to-checkout-button.php) by default doesn't do a post action, it is actually a link. Also the spot you were adding your input was not within a form so wouldn't have posted the value.

Display ACF fields on WooCommerce single product page via hook

This method works fine
Display ACF fields on WooCommerce single product page
but what if I want to use this code below. How do I have to format the code so that it works with the functions.php
$my_title = get_field('the_subtitle');
<?php if($my_title):?>
<span id="product-subtitle" class="product-subtitle subtitle"><?php echo $my_title;?></span>
<?php endif;?>

WooCommerce how to display specific "Additional Information" fields

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

Theme my login validate profile page?

I am using the theme my login WordPress plugin, using the custom pages I have added my own registration fields and validated them with no issues.
However I have worryingly discovered that corrupt code can be added and update to the profile page once a user is registered, I am wondering if there is the same offering for the profile page in terms of validation as there is for the registration?
The best way would be to use "theme my login" template pages, it already have the registration and profile part. You can add and remove fields to your liking and also style it, here is a tutorial for that
http://www.jfarthing.com/development/theme-my-login/adding-extra-registration-fields/.
If you have your custom built profile page then i suggest you use wpnonce to check the validity of the POST requests. Secondly use wordpress's own functions for fetching and updating data. Functions like get_user_meta and update_user_meta, these come built in with all the validation and you dont have to worry about it.
EDIT : I have written this code to demonstrate how to use a nonce field and then how to check the validity of the nonce field (By default Nonces are valid for 24 hours). The code below adds a form and asks for users height. The first php part wont run until the post request has been made. Once the request has been made then it checks for the integrity of the request. If all conditions are met then it will add a new meta field in the database called 'user_height', and will be updated every time the user changes it. Once the height has been set, it will also auto populate this in the input box, so they can see what is their current height. I hope this code covers all your doubts of showing user meta, adding/updating user meta and also validation nonces.
<?php
// Checking if the post request has been submitted and then verifing nonce
if (!isset( $_POST['get_user_height'] ) || !wp_verify_nonce( $_POST['get_user_height'], 'user_body_built' )
) {
print 'Sorry, the request cannot be verified.';
exit;
} else {
if(isset($_POST['user_height']) && !empty($_POST['user_height'])){
update_user_meta( $user_id, 'user_height', $_POST['user_height']);
}
}
<form method="post">
// Fetching previous height of user
<?php $user_height = get_user_meta($user_id, 'user_height', TRUE);?>
// Getting user's height and then saving it to users meta, if height was already set it will also show the current height.
<input type="text" name="user_height" <?php if($user_height){echo 'value="'.$user_height.'"';} ?> placeholder="enter your height">
// Generating a nonce field which will be checked on post request
<?php wp_nonce_field( 'user_body_built', 'get_user_height' ); ?>
</form>
Second EDIT (showcasing how to use existing registration fields on profile page, replace input names with the ones on registeration page): Just add this code in your profile page or functions.php it will automatically show these fields in the profile page.
function tml_edit_user_profile( $profileuser ) {?>
<p>
<label for="phone_number">Phone Number</label>
<!-- replace name attribute with the ones used on registration page -->
<input id="phone_number" type="text" name="phone_number" value="<?php echo $profileuser->phone_number; ?>" />
</p>
<?php
}
add_action( 'edit_user_profile', 'tml_edit_user_profile' );

Data/Content Architecture in WordPress 3.1

Update
I solved it in that way:
What you need:
Custom Post Type
Enabled Plugin "Posts 2 Posts" (see comments below)
Enabled Plugin "Allow numeric stubs"
You'll need the Plugin "Posts 2 Posts" to assign posts tp pages. "Allow numeric stubs" is a plugin that will allow you to have numeric slugs for pages. If try to add pages named to 2011 or 2012 you'll get Page-Slugs like 2011-2 or 2012-2 because it is not possible to have a page slug that is a number.
First you have to add some Pages. For Example 2011, Nominees and Actors. Now you arrange the Pages as childs to your needs.
2011
-Nominees
--Actors
Your Url will now look like http://example.com/2011/nominees/actors
Now you have to add a Custom Post Type which contains your nominees. Add some nominees to your Post Type (should work with Articles too).
Register a connection type in your functions.php
function my_connection_types() {
// Make sure the Posts 2 Posts plugin is active.
if ( !function_exists( 'p2p_register_connection_type' ) )
return;
p2p_register_connection_type( array(
'id' => 'posts_pages',
'from' => 'page',
'to' => 'nominees' // Your Post-Type
) );
}
add_action( 'init', 'my_connection_types', 100 );
Now go to your Actor-Page, look for a MetaBox named "Connected Nominees" and add your nominees.
Edit your page.php
<article class="single entry">
<header class="post post-header">
<h1><?php the_title(); ?></h1>
</header>
<section class="post post-content">
<?php the_content(); ?>
<?php
// Find connected pages
$connected = p2p_type( 'posts_pages' )->get_connected( get_queried_object_id() );
// Display connected pages
if ( $connected->have_posts() ) :
?>
<?php while ( $connected->have_posts() ) : $connected->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php endwhile; ?>
<?php
// Prevent weirdness
wp_reset_postdata();
endif;
?>
</section>
</article>
Now you should be able to see you asigned Posts (nominees) on your Page Actors.
The solution is easy to understand for authors since they are working with default wordpress behaviors and the content connection process feels really native.
I'm looking for some data architecture best practices in WordPress 3.1.n
Here are the facts:
It is some kind of award
The Award has **n** different categories
People could be nominated
Each nominee could be asscociated with 1 category
Each category is grouped by year
Each category can contain up to 3 or 5 nominees
Categories/Terms(?)
2011
actor
special-effects
story
2012
actor
special-effects
story
For Example:
http://example.com/nominees/2011/actor/all-nominees-in-actor-cat-in-2011.html
http://example.com/nominees/2011/special-effects/all-nominees-in-special-effects-cat-in-2011.html
http://example.com/nominees/2012/special-effects/all-nominees-in-special-effects-cat-in-2012.html
Note: Custom Post-Types and Custom Taxonomies are fine but i can't get hirachical Taxonomy-Terms work.
Does anyone have some suggestions to solve this with WordPress?
You might consider creating an "Award" post type with categories being the types of awards... Best Actor, Special Effects, etc. Also you could tag each award post by what year it is. I like tagging the year vs a year as a category because it gives you the option to build and scale content by year as the site grows. However a category would work as well.
Then, perhaps create an Actor post type with each actors name, this would have the added benefit of being scaleable should you want to expand on actor profiles down the road. Then consider connecting them using something like the "Posts 2 Posts" plugin. Plugin here
This plugin is great for associating posts with other posts like a "review" post with a "product" post. You will essentially be doing the same thing except "review" would be substituted with "award" and "product" would be associated with "actor".

Resources