get variable product attributes in cart page woocommerce - woocommerce

I want to display the attributes selected by the user on the cart page in WooCommerce
I tried to get attributes from the variable product but could not

I was able to fix this problem as follows
if ($_product->get_attributes()){
foreach ($_product->get_attributes() as $key => $val) { ?>
<p><?= wc_attribute_label($key) ?> : <span> <?= $val ?></span></p>
<?php }
}?>
you can get attributes from $_product

Related

Display all child categories with links of a specific Parent category in single woocommerce product page

<?php
global $product;
$brands_id = get_term_by('slug', 'authors', 'product_cat');
$terms = get_the_terms($post->ID, 'product_cat');
foreach ($terms as $term) {
$term_link = get_term_link( $term );
if($term->parent === $brands_id->term_id) { ?>
<div class="author-area">
<p>Author: <?php echo $term->name; ?></p>
<div>
<?php break;
}
}
?>
I am trying to show all the child categories separated by Comma of a specific parent categories (authors), on single product page. On this code, it works nicely. but it only output one category at a time where one of my product has 2 categories..
Can anyone help me here?
UPDATE
Found the solution. its inside my code. u need to remove "break" from the code.. this is what stopping loop.

Display ACF image field inside foreach loop

I have created a custom taxonomy for my custom post type.
Each term only needs to display its name (which works with $term->name;) and an image. For the image I am using ACF (as I usually do for custom fields on a page or post). It is set up and I can select an image when I add a new term in the dashboard, but how do I display it in a foreach loop? The usual get_field() isn't showing anything.
<ul class="taxonomy-terms">
<?php
$terms = get_terms( array(
'taxonomy' => 'my_taxonomy',
'hide_empty' => false,
) );
?>
<?php foreach ( $terms as $term) { ?>
<li>
<img src="<?php the_field('image'); ?>" />
<?php echo $term->name; ?>
</li>
<?php } ?>
</ul>
The issue being with the_field('image');
The image field is set to return the image url.
Maybe you should add your taxonomy as a parameter when calling the image, try :
<?php if( get_field('image') ): ?>
<img src="<?php the_field('image', $term); ?>" />
<?php endif; ?>
It's better to test the Image Field as you don't wan't to show an empty <img> tag.
Also please check that you didn't change the field name in the Image Field settings.
You can read about that here : https://www.advancedcustomfields.com/resources/image/
And here :
https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/
Let's hope this works,
Have a good day.
You should pass the current term of the loop as the second parameter of the ACF function:
<?php the_field('image', $term); ?>
See here:
https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/

Use list of titles of custom post type as values for ACF field

I would like to add a 'Post Author' select option in my Wordpress posts. Instead of creating 30 or so different users in Wordpress I would like to populate an ACF dropdown select field with all the titles of a custom post type (staff).
I found this code for outputting a list of custom post type titles...
// query for your post type
$post_type_query = new WP_Query(
array (
'post_type' => 'your-post-type',
'posts_per_page' => -1
)
);
// we need the array of posts
$posts_array = $post_type_query->posts;
// create a list with needed information
// the key equals the ID, the value is the post_title
$post_title_array = wp_list_pluck( $posts_array, 'post_title', 'ID' );
...and I found this code from an ACF article about dynamically populating a select box...
function acf_load_color_field_choices( $field ) {
// reset choices
$field['choices'] = array();
// get the textarea value from options page without any formatting
$choices = get_field('my_select_values', 'option', false);
// explode the value so that each line is a new array piece
$choices = explode("\n", $choices);
// remove any unwanted white space
$choices = array_map('trim', $choices);
// loop through array and add to field 'choices'
if( is_array($choices) ) {
foreach( $choices as $choice ) {
$field['choices'][ $choice ] = $choice;
}
}
// return the field
return $field;
}
add_filter('acf/load_field/name=color', 'acf_load_color_field_choices');
...however I'm really not sure how to splice the two together so that it grabs my custom field titles and adds them to that ACF field selector.
I tried but couldn't get any results to display.
Any ideas?
Thanks to the comment from mmmm I was able to use the 'Relationship' field type in ACF to achieve what I wanted... here is my code for anyone interested
'post-author' is the name of the ACF field, which was set to relationship, and the custom post type 'staff' was selected from the options, so I can choose any member of staff. They are then outputted in the post with thumbnail and job title...
<?php
$posts = get_field( 'post-author' );
if ( $posts ): ?>
<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
<?php setup_postdata($post); ?>
<div class="author">
<?php the_post_thumbnail('small'); ?>
<div class="author-text">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
<p>
<?php the_field('job_title') ?>
</p>
</div>
<div class="clearfix"></div>
</div>
<?php endforeach; ?>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>

Woocommerce Replace Product in Cart Upon Place Order in Checkout Page

I have been searching on the web, reading docs & stuff but I can't figure out on replacing product in Checkout Page.
For your information, my main product page is in the Home Page and each product that have been selected, will redirected to Checkout Page. Now here, there is a problem. Let me explain....
You see, I have a carousel slider in Checkout Page which user can change/replace their product (which already been added into their cart) before they pay.
form-checkout.php
global $woocommerce;
global $product;
$items = $woocommerce->cart->get_cart();
foreach ($items as &$item){
$id = $item['product_id'];
}
echo $id;
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel" data-interval="false">
<div class="carousel-inner" role="listbox">
<?php
// Querying of product information retrieval
$args = array( 'post_type' => 'product', 'posts_per_page' => 4, 'orderby' =>'menu_order', 'order' =>'ASC');
$loop = new WP_Query( $args );
// Display each retrieved product
while ( $loop->have_posts() ) :
$loop->the_post();
// WooCommerce global product variable. Refer: https://docs.woothemes.com/document/class-reference/
global $product;
global $woocommerce;
?>
<div class="item <?php if ($product->id == $id) { ?> active <?php } ?>">
<div class="p-big" id="p-custom-color">
<strong><?php the_title(); ?></strong>
</div>
<div class="p-light-black">CANDIDATES</div>
<input type="hidden" id="product" name="productid" value="<?php echo $product->id; ?>">
</div>
<?php
endwhile;
wp_reset_query(); // After the loop ended, quit the custom loop and reset back the main loop
?>
</div>
</div>
<!-- Upon form submission -->
if (isset($_POST['woocommerce_checkout_place_order'])){
global $woocommerce;
$woocommerce->cart->empty_cart(); // Empty the cart
$selectedproduct = $_POST['selectedproductid']; // Get the selected product
do_shortcode('[add_to_cart id="' . $selectedproduct . '"]'); // Insert the selected product in the the cart
return esc_url( wc_get_checkout_url() ); // Redirect to Payment Gateway Page
}
<form name="checkout" method="post" class="checkout woocommerce-checkout" action="" enctype="multipart/form-data">
<?php if ( sizeof( $checkout->checkout_fields ) > 0 ) : ?>
<?php do_action( 'woocommerce_checkout_before_customer_details' ); ?>
<?php do_action( 'woocommerce_checkout_billing' ); ?>
<?php do_action( 'woocommerce_checkout_after_customer_details' ); ?>
<?php endif; ?>
<h3 id="order_review_heading"><?php _e( 'Your order', 'woocommerce' ); ?></h3>
<?php do_action( 'woocommerce_checkout_before_order_review' ); ?>
<div id="order_review" class="woocommerce-checkout-review-order">
<!-- Checkout Review -->
<input type="hidden" id="selectedproduct" name="selectedproductid" value="">
<?php do_action( 'woocommerce_checkout_order_review' ); ?>
</div>
<?php do_action( 'woocommerce_checkout_after_order_review' ); ?>
</form>
As you can see, in the carousel, I have included <input type="hidden" id="product" name="productid" value="<?php echo $product->id; ?>"> to get each product ID and with my jQuery (I didn't show here), I took any product ID that the product is currently on the active slide and fill it in the <input type="hidden" id="selectedproduct" name="selectedproductid" value=""> in the form.
By that, I can replace the product that have been added to cart with the selected/chosen product based on the active slide with these code (Located above the form):-
<!-- Upon form submission -->
if (isset($_POST['woocommerce_checkout_place_order'])){
global $woocommerce;
$woocommerce->cart->empty_cart(); // Empty the cart
$selectedproduct = $_POST['selectedproductid']; // Get the selected product
do_shortcode('[add_to_cart id="' . $selectedproduct . '"]'); // Insert the selected product in the the cart
return esc_url( wc_get_checkout_url() ); // Redirect to Payment Gateway Page
}
The problem here is, it failed to replace the old product with the current chosen product and it just redirect to the payment gateway page with the old product.
I want it to replace the product with the new selected ones upon placing order. Is it possible? I hope it is, because I have been playing with WooCommerce for weeks now and I don't want my effort to be futile. Help me guys.....
After few days of figuring this out, with 30+ Chrome tabs, 50+ of purchases test & 10 gallons of coffee, finally I found the answer...
add_action('woocommerce_checkout_process', 'change_product_upon_submission');
function change_product_upon_submission() {
if ( !empty( $_POST['_wpnonce'] ) && !empty($_POST['selectedproductid']) ) {
$selectedproduct = $_POST['selectedproductid']; // Get the selected product
WC()->cart->empty_cart(); //Empty the cart
WC()->cart->add_to_cart( $selectedproduct ); // Insert the selected product in the cart
}
}
The hook required to trigger this function is within the WC_Checkout process_checkout() class in includes/class-wc-checkout.php. This woocommerce_checkout_process doesn't exist WooCommerce template files, we're gonna be thorough. So to do whatever custom stuff before sending data to the payment gateway upon place order submission, we're gonna need to manipulate the woocommerce_checkout_process hook as process_checkout() function processes the checkout after the confirm order button is pressed.
Hope this save someone's lives as I don't have any because I need to sleep after few days of burning midnight oil figuring this abomination.

Display Link to Category in Wordpress?

I am trying to figure out how to display the category of an article, and a link to the category. Any help would be greatly appreciated.
If you want to do this on post page you can add something like the following to your single.php file of your theme.
<div class="meta">Posted in: <span><?php the_category(', ') ?> </span></div>
Here's some info that will be of use:
http://codex.wordpress.org/Template_Tags/wp_list_categories
Basically you can call: <?php wp_list_categories( $args ); ?> and this will output what you're looking for.
Thr $args parameter is an array of settings strings that lets you change the order, style, depth etc, on links returned.
Note that: <?php the_category(', ') ?>will display the category as a link. which is good.... but if you want only the category URL (that is, the category link only), then you will have to use the <?php get_category_link($category_ID); ?> the $category_ID is required. once you fix that in, the category URL will be returned.
Consider the example:
<?php
// Get the ID of a given category
$category_id = get_cat_ID( 'Category Name' );
// Get the URL of this category
$category_link = get_category_link( $category_id );
?>
<!-- Print a link to this category -->
Category Name
Now you can see how we got the category ID and then using it to get the category Link. Hope this answers your question well enough?
You can use get_the_category()
<?php
$categories = get_the_category();
$separator = ' ';
$output = '';
if($categories){
foreach($categories as $category) {
$output .= ''.$category->cat_name.''.$separator;
}
echo trim($output, $separator);
}
?>

Resources