Change Woocommerce variable.php from table to div - php

I am currently customizing (or trying) the yourtheme/woocommerce/single-product/add-to-cart/variable.php in my theme however even though I've tried I can't seem to get rid of the table in the woocommerce code and replace it by divs and dropdown lists. I would like it to look like the sections below:
<!-- Product Section -->
<section>
<div class="container my-5 px-5 pt-4 bg-light">
<div class="row no-gutters">
<div class="col-md-5">
<img src="<?php bloginfo('template_directory'); ?>/assets/images/best-seller.jpg" class="img-fluid w-75 m-auto d-block"
alt="Best Seller">
</div>
<div class="col-md-7 pr-md-5">
<h1 class="main-heading my-4">BLACK MATTE JAR</h1>
<form>
<div class="form-row">
<div class="form-group col-md-7">
<label for="inputSize">Size</label>
<select id="inputSize" class="form-control">
<option selected>Big - 14 oz</option>
<option>Small - 10 oz</option>
</select>
</div>
<div class="form-group col-md-4 offset-md-1">
<label for="inputQuantity">Quantity</label>
<select id="inputQuantity" class="form-control">
<option selected>1 unit</option>
<option>2 unit</option>
</select>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-7">
<label for="inputFragrance">Fragrance</label>
<select id="inputFragrance" class="form-control">
<option selected><img src="<?php bloginfo('template_directory'); ?>/assets/images/icon-1.png" class="d-inline w-25">Peace
of mind</option>
<option>Refreshing</option>
</select>
</div>
<div class="form-group col-md-4 offset-md-1 mt-auto">
<div class="product-price pb-3">
<h5>Price <span class="float-right">25$</span></h5>
</div>
</div>
</div>
<button type="submit" class="btn btn-lg px-5 my-4">ADD TO CART</button>
</form>
</div>
</div>
</div>
</section>
So when I started all this, I managed to get all my product variations with the code below, however I couldn't figure out how to manage the woocommerce hooks and how to get the proper variation price when the user changed the dropdown list (so the code below was not using hooks at all)
`<div class="col-md-7 pr-md-5">
<h1 class="main-heading my-4"><?php the_title(); ?></h1>
<?php
/*** ADD TO CART DYNAMIC */
$attribute_keys = array_keys( $attributes );
$variations_json = wp_json_encode( $available_variations );
$variations_attr = function_exists( 'wc_esc_json' ) ? wc_esc_json( $variations_json ) : _wp_specialchars( $variations_json, ENT_QUOTES, 'UTF-8', true );
do_action( 'woocommerce_before_add_to_cart_form' ); ?>
<form class="variations_form cart" action="<?php echo esc_url( apply_filters( 'woocommerce_add_to_cart_form_action', $product->get_permalink() ) ); ?>" method="post" enctype='multipart/form-data' data-product_id="<?php echo absint( $product->get_id() ); ?>" data-product_variations="<?php echo $variations_attr; // WPCS: XSS ok. ?>">
<div class="form-row">
<div class="form-group col-md-7">
<label for="inputSize">Size</label>
<select id="inputSize" class="form-control">
<?php
$product = wc_get_product( get_the_id() );
$size = $product->get_attribute('size');
$size_array = print_r ($size , TRUE);
$size_text = explode ('|', $size_array);
$aux_size = 0;
foreach ($size_text as $s_txt){
if ($aux ==0) {
?><option selected><?php echo $s_txt ?></option>
<?php
} else {
?><option><?php echo $s_txt ?></option>
<?php
}
}
?>
</select>
</div>
<div class="form-group col-md-4 offset-md-1">
<label for="inputQuantity">Quantity</label>
<select id="inputQuantity" class="form-control">
<option selected>1 Unit</option>
<option>2 Units</option>
<option>3 Units</option>
<option>4 Units</option>
<option>5 Units</option>
</select>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-7">
<label for="inputFragrance">Fragrance</label>
<select id="inputFragrance" class="form-control">
<?php
$fragrance = $product->get_attribute('fragrance');
$fragrance_array = print_r ($fragrance , TRUE);
$fragrance_text = explode ('|', $fragrance_array);
$aux_size = 0;
foreach ($fragrance_text as $f_txt){
if ($aux ==0) {
?><option selected><?php echo $f_txt ?></option>
<?php
} else {
?><option><?php echo $f_txt ?></option>
<?php
}
}
?>
</select>
</div>
<div class="form-group col-md-4 offset-md-1 mt-auto">
<div class="product-price pb-3">
<h5>Price <span class="float-right">25$</span></h5>
</div>
</div>
</div>
<button onclick="Add_to_cart(<?php $product?>)" type="submit" class="btn btn-lg px-5 my-4">ADD TO CART</button>`
Then I discovered the variable.php, so below is what I can't make it:
1 - can't seem to give the same look and feel as before (refer to first code shown in this post.
2 - When I replace in the below code the and by div's the first div on the first iteration gives me $attribute_name = null.
<?php foreach ( $attributes as $attribute_name => $options ) : ?>
<tr>
<td class="label"><label for="<?php echo esc_attr( sanitize_title( $attribute_name ) ); ?>"><?php echo wc_attribute_label( $attribute_name ); // WPCS: XSS ok. ?></label></td>
<td class="value">
3 - I can't seem to fetch the variation price upon user change the product attributes.
Did any of you managed to achieve this? If so could you share your code? I really hope this thread is cleaner and clearer. I've added till where I've got in the code below.
<div class="col-md-7 pr-md-5">
<h1 class="main-heading my-4"><?php the_title(); ?></h1>
<?php
/*** ADD TO CART DYNAMIC */
$available_variations = $product->get_available_variations();
$attributes = $product->get_variation_attributes();
$attribute_keys = array_keys( $attributes );
$variations_json = wp_json_encode( $available_variations );
$variations_attr = function_exists( 'wc_esc_json' ) ? wc_esc_json( $variations_json ) : _wp_specialchars( $variations_json, ENT_QUOTES, 'UTF-8', true );
do_action( 'woocommerce_before_add_to_cart_form' ); ?>
<form class="variations_form cart" action="<?php echo esc_url( apply_filters( 'woocommerce_add_to_cart_form_action', $product->get_permalink() ) ); ?>" method="post" enctype='multipart/form-data' data-product_id="<?php echo absint( $product->get_id() ); ?>" data-product_variations="<?php echo $variations_attr; // WPCS: XSS ok. ?>">
<?php do_action( 'woocommerce_before_variations_form' );
?>
<?php if ( empty( $available_variations ) && false !== $available_variations ) : ?>
<p class="stock out-of-stock"><?php echo esc_html( apply_filters( 'woocommerce_out_of_stock_message', __( 'This product is currently out of stock and unavailable.', 'woocommerce' ) ) ); ?></p>
<?php else : ?>
<table class="variations" cellspacing="0">
<tbody>
<?php foreach ( $attributes as $attribute_name => $options ) : ?>
<tr>
<td class="label"><label for="<?php echo esc_attr( sanitize_title( $attribute_name ) ); ?>"><?php echo wc_attribute_label( $attribute_name ); // WPCS: XSS ok. ?></label></td>
<td class="value">
<?php
wc_dropdown_variation_attribute_options(
array(
'options' => $options,
'attribute' => $attribute_name,
'product' => $product,
)
);
?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<div class="single_variation_wrap">
<?php
/**
* Hook: woocommerce_before_single_variation.
*/
do_action( 'woocommerce_before_single_variation' );
/**
* Hook: woocommerce_single_variation. Used to output the cart button and placeholder for variation data.
*
* #since 2.4.0
* #hooked woocommerce_single_variation - 10 Empty div for variation data.
* #hooked woocommerce_single_variation_add_to_cart_button - 20 Qty and cart button.
*/
do_action( 'woocommerce_single_variation' );
/**
* Hook: woocommerce_after_single_variation.
*/
do_action( 'woocommerce_after_single_variation' );
?>
</div>
<?php endif; ?>
Ive been trying to change it for days without any success I return back to the original code most of the time this is the reason why I made my first post here as I cannot add the price into this code, nor turn it as per my html design.
Sorry for the simple question but I can't find anyone online that have achieved this before.

I've managed to do it with the following code:
<?php endforeach; ?>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label for="">Quantity</label>
<div class="single_variation_wrap">
<?php
/**
* Hook: woocommerce_before_single_variation.
*/
do_action( 'woocommerce_before_single_variation' );
/**
* Hook: woocommerce_single_variation. Used to output the cart button and placeholder for variation data.
*
* #since 2.4.0
* #hooked woocommerce_single_variation - 10 Empty div for variation data.
* #hooked woocommerce_single_variation_add_to_cart_button - 20 Qty and cart button.
*/
do_action( 'woocommerce_single_variation' );
/**
* Hook: woocommerce_after_single_variation.
*/
do_action( 'woocommerce_after_single_variation' );
?>
</div>
</div>
<div class="form-group col-md-6 mt-6">
<div class="product-price pb-3">
<h5 >Price <?php echo $product->get_price_html(); ?></h5>
</div>
</div>
</div>
<?php endif; ?>
<?php do_action( 'woocommerce_after_variations_form' ); ?>
</form>
I've converted the tds into div's in an easy manner that is still identifiable by woocommerce. Furthermore I've managed to fetch the price as shown in the code.
Now I am only having one issue which is to force the user to stay in my custom page if no selection is made.
Hope someone can suport me

Related

Critical error on single product page woo commerce

My website started showing this strange error recently which I have been unable to resolve. When I try to view a product, it returns a critical error.
I’ve done some troubleshooting and I realized the theme is the culprit. I have also checked the error log and I saw that this specific line of code is causing the error:
Uncaught Error: Call to a member function is_in_stock() on string in /home/u306409103/domains/apdbrestore.com/public_html/wp-content/themes/custom-theme/woocommerce/single-product.php:45 Stack trace: #0
When you check that line, it says:
<?php if( !$product->is_in_stock() ) : ?>
<div class="outofstock">
<img src="<?php echo imgfolder('out-of-stock.png'); ?>" alt="Out of Stock">
</div>
<?php endif; ?>
To be specific the error is pointing towards the first line:
<?php if( !$product->is_in_stock() ) : ?>
I know the problem is coming from here but don’t know what next to do. What statement can I use here to do away with this error?
Thanks
Full code
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
get_header( 'shop' ); ?>
<?php
/**
* woocommerce_before_main_content hook.
*
* #hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content)
* #hooked woocommerce_breadcrumb - 20
*/
do_action( 'woocommerce_before_main_content' );
?>
<?php
if( has_post_thumbnail() ) :
$img = get_the_post_thumbnail_url();
else :
$img = placeholder();
endif;
?>
<div class="product-single">
<div class="container">
<div class="row">
<div class="col-md-7">
<div class="product-single-left product-contain">
<?php if( !$product->is_in_stock() ) : ?>
<div class="outofstock">
<img src="<?php echo imgfolder('out-of-stock.png'); ?>" alt="Out of Stock">
</div>
<?php endif; ?>
<div class="product-single-holder">
<div class="product-single-gallery">
<div class="product-single-image active" data-img="<?php echo $img; ?>" style="background-image: url(<?php echo $img; ?>);"></div>
<?php global $product;
$attachment_ids = $product->get_gallery_attachment_ids();
foreach( $attachment_ids as $attachment_id ) { ?>
<div class="product-single-image" data-img="<?php echo wp_get_attachment_url( $attachment_id ); ?>" style="background-image: url(<?php echo $image_link = wp_get_attachment_url( $attachment_id ); ?>);"></div>
<?php } ?>
</div>
</div>
<div class="product-single-featured">
<img src="<?php echo $img; ?>" alt="<?php echo get_the_title(); ?>">
</div>
</div>
</div>
<div class="col-md-5">
<div class="product-single-content">
<h3><?php echo get_the_title(); ?></h3>
<div class="product-single-price">
<div class="home-sale-price">
<?php if( $product->get_sale_price() ) : ?>
<div class="home-sale-less">
Listed Price - <span>P <?php echo wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ) ); ?> </span>
</div>
<div class="home-sale-orig">
<?php echo 'P '.wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) ); ?>
</div>
<?php else : ?>
<div class="home-sale-less">Listed Price - <span>P <?php echo wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) ); ?> </span></div>
<?php endif; ?>
</div>
<?php if(get_field('terms')): ?>
<div class="home-sale-term">Terms - <span>P <?php echo get_field('terms'); ?></span></div>
<?php endif; ?>
<?php if(get_field('dp_and_pay')): ?>
<div class="product-single-term"><?php echo get_field('dp_and_pay'); ?></div>
<?php endif; ?>
</div>
<div class="product-single-desc">
<?php echo apply_filters( 'the_content', $product->get_short_description() ); ?>
</div>
<i class="fas fa-plus-square"></i>Add to Cart
</div>
<?php while ( have_posts() ) : the_post(); ?>
<?php //wc_get_template_part( 'content', 'single-product' ); ?>
<?php endwhile; // end of the loop. ?>
</div>
</div>
</div>
<div class="home-sale home-new">
<div class="container">
<div class="row justify-content-center">
<div class="col-md-10">
<div class="header-excerpt">
<?php $salehead = get_field('new_arrivals_content',8); ?>
<h3>Recommmended Items</h3>
<div class="header-excerpt-text"><?php echo $salehead['content']; ?></div>
</div>
</div>
</div>
<div class="row">
<div class="col-12">
<div class="home-sale-slider">
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'order_by' => 'date',
'order' => 'DESC'
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
if( has_post_thumbnail() ) :
$img = get_the_post_thumbnail_url();
else :
$img = placeholder();
endif;
$product = wc_get_product(get_the_ID());
?>
<div class="home-sale-holder product-contain">
<?php if( !$product->is_in_stock() ) : ?>
<div class="outofstock">
<img src="<?php echo imgfolder('out-of-stock.png'); ?>" alt="Out of Stock">
</div>
<?php endif; ?>
<div class="home-sale-top">
<img src="<?php echo $img; ?>" alt="<?php echo get_the_title(); ?>">
<i class="fas fa-plus-square"></i>Add to cart
</div>
<div class="home-sale-bottom">
<a href="<?php echo get_permalink(); ?>">
<h5><?php echo get_the_title(); ?></h5>
<div class="home-sale-price">
<?php if( $product->get_sale_price() ) : ?>
<div class="home-sale-less">
Cash - <span>P <?php echo wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ) ); ?> </span>
</div>
<div class="home-sale-orig">
<?php echo 'P '.wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) ); ?>
</div>
<?php else : ?>
<div class="home-sale-less">Cash - <span>P <?php echo wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) ); ?> </span></div>
<?php endif; ?>
</div>
<?php if(get_field('terms')): ?>
<div class="home-sale-term">Terms - <span>P <?php echo get_field('terms'); ?></span></div>
<?php endif; ?>
</a>
</div>
</div>
<?php endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</div>
</div>
</div>
</div>
View All Products
</div>
</div>
<?php
/**
* woocommerce_after_main_content hook.
*
* #hooked woocommerce_output_content_wrapper_end - 10 (outputs closing divs for the content)
*/
do_action( 'woocommerce_after_main_content' );
?>
<?php
/**
* woocommerce_sidebar hook.
*
* #hooked woocommerce_get_sidebar - 10
*/
do_action( 'woocommercve_sidebar' );
?>
<?php get_footer( 'shop' );
/* Omit closing PHP tag at the end of PHP files to avoid "headers already sent" issues. */
Finally got the solution to this problem and in case anyone stumbles across this, all I had to do was to update how to get the reference post ID because the existing one is deprecated already.
<?php global $product;
$product = wc_get_product( $post->ID );
$stock_quantity = get_post_meta($post->ID, '_stock', true);
//var_dump($product->get_stock_status());
?>
<?php if ($product->get_stock_status() != 'instock') : ?>
<div class="outofstock">
<img src="<?php echo imgfolder('out-of-stock.png'); ?>" alt="Out of Stock">
</div>
<?php endif; ?>
Make sure that the $product is not a string or a boolean before checking for stock.
<?php if( is_object($product) && !$product->is_in_stock() ) : ?>
Or, you completly stop processing the template by bailing out at the top if no product was returned like this:
if (!is_object($product)) {
return;
}
Try to put this code, I updated your code a bit.
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
get_header( 'shop' );
global $product;
?>
<?php
/**
* woocommerce_before_main_content hook.
*
* #hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content)
* #hooked woocommerce_breadcrumb - 20
*/
do_action( 'woocommerce_before_main_content' );
?>
<?php
if( has_post_thumbnail() ) :
$img = get_the_post_thumbnail_url();
else :
$img = placeholder();
endif;
?>
<div class="product-single">
<div class="container">
<div class="row">
<div class="col-md-7">
<div class="product-single-left product-contain">
<?php if( !$product->is_in_stock() ) : ?>
<div class="outofstock">
<img src="<?php echo imgfolder('out-of-stock.png'); ?>" alt="Out of Stock">
</div>
<?php endif; ?>
<div class="product-single-holder">
<div class="product-single-gallery">
<div class="product-single-image active" data-img="<?php echo $img; ?>" style="background-image: url(<?php echo $img; ?>);"></div>
<?php
$attachment_ids = $product->get_gallery_attachment_ids();
foreach( $attachment_ids as $attachment_id ) { ?>
<div class="product-single-image" data-img="<?php echo wp_get_attachment_url( $attachment_id ); ?>" style="background-image: url(<?php echo $image_link = wp_get_attachment_url( $attachment_id ); ?>);"></div>
<?php } ?>
</div>
</div>
<div class="product-single-featured">
<img src="<?php echo $img; ?>" alt="<?php echo get_the_title(); ?>">
</div>
</div>
</div>
<div class="col-md-5">
<div class="product-single-content">
<h3><?php echo get_the_title(); ?></h3>
<div class="product-single-price">
<div class="home-sale-price">
<?php if( $product->get_sale_price() ) : ?>
<div class="home-sale-less">
Listed Price - <span>P <?php echo wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ) ); ?> </span>
</div>
<div class="home-sale-orig">
<?php echo 'P '.wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) ); ?>
</div>
<?php else : ?>
<div class="home-sale-less">Listed Price - <span>P <?php echo wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) ); ?> </span></div>
<?php endif; ?>
</div>
<?php if(get_field('terms')): ?>
<div class="home-sale-term">Terms - <span>P <?php echo get_field('terms'); ?></span></div>
<?php endif; ?>
<?php if(get_field('dp_and_pay')): ?>
<div class="product-single-term"><?php echo get_field('dp_and_pay'); ?></div>
<?php endif; ?>
</div>
<div class="product-single-desc">
<?php echo apply_filters( 'the_content', $product->get_short_description() ); ?>
</div>
<i class="fas fa-plus-square"></i>Add to Cart
</div>
<?php while ( have_posts() ) : the_post(); ?>
<?php //wc_get_template_part( 'content', 'single-product' ); ?>
<?php endwhile; // end of the loop. ?>
</div>
</div>
</div>
<div class="home-sale home-new">
<div class="container">
<div class="row justify-content-center">
<div class="col-md-10">
<div class="header-excerpt">
<?php $salehead = get_field('new_arrivals_content',8); ?>
<h3>Recommmended Items</h3>
<div class="header-excerpt-text"><?php echo $salehead['content']; ?></div>
</div>
</div>
</div>
<div class="row">
<div class="col-12">
<div class="home-sale-slider">
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'order_by' => 'date',
'order' => 'DESC'
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
if( has_post_thumbnail() ) :
$img = get_the_post_thumbnail_url();
else :
$img = placeholder();
endif;
$new_product = wc_get_product(get_the_ID());
?>
<div class="home-sale-holder product-contain">
<?php if( !$new_product->is_in_stock() ) : ?>
<div class="outofstock">
<img src="<?php echo imgfolder('out-of-stock.png'); ?>" alt="Out of Stock">
</div>
<?php endif; ?>
<div class="home-sale-top">
<img src="<?php echo $img; ?>" alt="<?php echo get_the_title(); ?>">
<i class="fas fa-plus-square"></i>Add to cart
</div>
<div class="home-sale-bottom">
<a href="<?php echo get_permalink(); ?>">
<h5><?php echo get_the_title(); ?></h5>
<div class="home-sale-price">
<?php if( $new_product->get_sale_price() ) : ?>
<div class="home-sale-less">
Cash - <span>P <?php echo wc_price( wc_get_price_to_display( $new_product, array( 'price' => $new_product->get_sale_price() ) ) ); ?> </span>
</div>
<div class="home-sale-orig">
<?php echo 'P '.wc_price( wc_get_price_to_display( $new_product, array( 'price' => $new_product->get_regular_price() ) ) ); ?>
</div>
<?php else : ?>
<div class="home-sale-less">Cash - <span>P <?php echo wc_price( wc_get_price_to_display( $new_product, array( 'price' => $new_product->get_regular_price() ) ) ); ?> </span></div>
<?php endif; ?>
</div>
<?php if(get_field('terms')): ?>
<div class="home-sale-term">Terms - <span>P <?php echo get_field('terms'); ?></span></div>
<?php endif; ?>
</a>
</div>
</div>
<?php endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</div>
</div>
</div>
</div>
View All Products
</div>
</div>
<?php
/**
* woocommerce_after_main_content hook.
*
* #hooked woocommerce_output_content_wrapper_end - 10 (outputs closing divs for the content)
*/
do_action( 'woocommerce_after_main_content' );
?>
<?php
/**
* woocommerce_sidebar hook.
*
* #hooked woocommerce_get_sidebar - 10
*/
do_action( 'woocommercve_sidebar' );
?>
<?php get_footer( 'shop' );

Filter do not apply Wordpress

I have got a filter in tab. I use motors theme. Filter has to redirect on main filter's page with applied options. It redirects and options are applied in list but link is not changed (it should be like this: site.com/?page_id=16410&make=citroen&ca-year=2012&min_price=2000&max_price=200000 ) but it's just like this: site.com/?page_id=16410 (main filter page). On this image I chose Chevrolet cars but it didn't apply: Chevrolet
If I choose one more option, it will apply all of them but I want it to work from tabs properly.
Form code:
<form action="<?php echo esc_url( stm_get_listing_archive_link() ); ?>" method="post">
<?php foreach ( $tax_query_args as $taxonomy_term_key => $taxonomy_term ): ?>
<?php //empty($taxonomy_term[0]->numeric) and ?>
<?php if ( !empty( $taxonomy_term[0] ) ): ?>
<div class="col-md-<?php echo esc_attr( $filter_columns_number ); ?> col-sm-6">
<div class="form-group">
<select name="<?php echo ( esc_attr( $taxonomy_term_key ) == "price" ) ? "max_price" : esc_attr( $taxonomy_term_key ) ?>" class="form-control">
<option value=""><?php printf( esc_html__( 'Select %s', 'motors' ), stm_get_name_by_slug( $taxonomy_term_key ) ); ?></option>
<?php foreach ( $taxonomy_term as $attr_key => $attr ): ?>
<option value="<?php echo esc_attr( $attr->slug ); ?>"
<?php if ( $attr->count == 0 && !$taxonomy_term[0]->numeric ) {
echo 'disabled="disabled"';
} ?>
data-disabled="<?php echo ( esc_attr( $attr->count ) == 0 && !$taxonomy_term[0]->numeric ) ? 'disabled' : "null" ?>">
<?php echo ( esc_attr( $taxonomy_term_key ) == "price" ) ? stm_listing_price_view( $attr->name ) : esc_attr( $attr->name ); ?>
</option>
<?php endforeach; ?>
</select>
</div>
</div>
<?php endif; ?>
<?php endforeach; ?>
<div class="col-md-3 col-sm-6">
<div class="row">
<div class="col-md-8 col-sm-12">
<button type="submit" class="button icon-button">
<i class="stm-icon-search"></i><?php esc_html_e( 'Search', 'motors' ); ?>
</button>
</div>
<div class="col-md-4 hidden-sm hidden-xs">
<a href="" class="reset-all reset-styled"
title="<?php esc_html_e( 'Reset search fields', 'motors' ); ?>">
<i class="stm-icon-reset"></i>
</a>
</div>
</div>
</div>
</form>
If you want the parameters to show in the URL, then you need to use method GET, not POST.
And because your form action URL already contains GET parameters, what is explained under submitting a GET form with query string params and hidden params disappear also needs to be taken into account - those would get discarded, when the form is submitted, so the parameter needs to be supplied via a hidden field instead:
<input type="hidden" name="page_id" value="16410">

Bypass "Billing" section when customer is paying with gift card - woocommerce

My wp theme was custom built and has a great ajax checkout experience, but I am having an issue when customers attempt to pay with a yith gift card that covers the total amount of the cart. Essentially throughout the checkout process there is a single button to proceed through the checkout steps; Shipping>billing>summary.
When a gift card is applied that covers the total amount, customers put in their shipping info, then click next to go to billing, at this point the credit card fields are hidden (as they should be), but the button (which now states "Proceed To Summary") is no longer clickable. The customer cant move from the empty billing page to complete their order. As such, is there a way to bypass the "billing" step if the order total is 0 and go straight to summary so the customer can complete their order?
Here is the code from my theme form-checkout.php :
<?php
/**
* Checkout Form
*
* This template can be overridden by copying it to yourtheme/woocommerce/checkout/form-checkout.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* #see https://docs.woocommerce.com/document/template-structure/
* #package WooCommerce/Templates
* #version 3.5.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
do_action( 'woocommerce_before_checkout_form', $checkout );
// If checkout registration is disabled and not logged in, the user cannot checkout.
if ( ! $checkout->is_registration_enabled() && $checkout->is_registration_required() && ! is_user_logged_in() ) {
echo esc_html( apply_filters( 'woocommerce_checkout_must_be_logged_in_message', __( 'You must be logged in to checkout.', 'woocommerce' ) ) );
return;
}
?>
<div class="checkout-container d-lg-flex">
<!-- Checkout main -->
<div class="checkout-main">
<!-- Header -->
<div class="checkout-header"><?php _e('Secure checkout', 'woocommerce'); ?></div>
<?php if( !is_user_logged_in() ){ ?>
<div class="checkout-section section-checkout-logreg">
<?php
if( isset($_GET['action']) && $_GET['action'] == 'register' ) {
//wc_get_template( '/checkout/checkout_registration_page.php' );
wc_get_template( 'myaccount/form-registration.php' );
}
else if( isset($_GET['action']) && $_GET['action'] == 'forgot_pass' ) {
//wc_get_template( '/checkout/checkout_registration_page.php' );
wc_get_template( 'myaccount/form-lost-password.php' );
}
else{
//wc_get_template( '/checkout/checkout_login_page.php' );
wc_get_template( 'myaccount/form-login-single.php' );
}
?>
<!-- Button area -->
<div class="checkout-button-area d-flex flex-wrap justify-content-between">
<?php _e('Back To store', 'woocommerce'); ?>
</div>
</div>
<?php } ?>
<!-- Progress -->
<div class="checkout-progress"<?php if(!is_user_logged_in()){ echo ' style="display: none;"'; } ?>>
<div class="progress-step active" id="progress-shipping">
<?php _e('Shipping', 'woocommerce'); ?>
</div>
<div class="progress-step" id="progress-payment">
<?php _e('Payment', 'woocommerce'); ?>
</div>
<div class="progress-step" id="progress-summary">
<?php _e('Summary', 'woocommerce'); ?>
</div>
</div>
<form name="checkout" method="post" class="checkout woocommerce-checkout" action="<?php echo esc_url( wc_get_checkout_url() ); ?>" enctype="multipart/form-data"<?php if(!is_user_logged_in()){ echo ' style="display: none;"'; } ?>>
<?php if ( $checkout->get_checkout_fields() ) : ?>
<?php do_action( 'woocommerce_checkout_before_customer_details' ); ?>
<div id="customer_details">
<!-- Checkout Shipping -->
<div class="checkout-section section-shipping">
<?php do_action( 'woocommerce_checkout_shipping' ); ?>
</div>
<!-- Checkout Billing -->
<div class="checkout-section section-billing" style="display: none;">
<?php do_action( 'woocommerce_checkout_billing' ); ?>
</div>
<!-- Checkout Summary -->
<div class="checkout-section section-summary" style="display: none;">
<div class="row">
<div class="summary-section summary-shipping col-12 col-sm-6">
<div class="summary-section-wrap">
<h4 class="summary-title"><?php _e('Shipping Address', 'woocommerce'); ?></h4>
<button type="button" class="summary-edit" title="<?php _e('Edit', 'woocommerce'); ?>"><?php _e('Edit', 'woocommerce'); ?></button>
<div class="summary-section-content"></div>
</div>
</div>
<div class="summary-section summary-option col-12 col-sm-6">
<div class="summary-section-wrap">
<h4 class="summary-title"><?php _e('Shipping Option', 'woocommerce'); ?></h4>
<button type="button" class="summary-edit" title="<?php _e('Edit', 'woocommerce'); ?>"><?php _e('Edit', 'woocommerce'); ?></button>
<div class="summary-section-content"></div>
</div>
</div>
<div class="summary-section summary-payment col-12 col-sm-6" data-ending="<?php _e('Ending in', 'woocommerce'); ?>">
<div class="summary-section-wrap">
<h4 class="summary-title"><?php _e('Payment Details', 'woocommerce'); ?></h4>
<button type="button" class="summary-edit" title="<?php _e('Edit', 'woocommerce'); ?>"><?php _e('Edit', 'woocommerce'); ?></button>
<div class="summary-section-content"></div>
</div>
</div>
</div>

How to load WooCommerce cross sell and cart-collaterals inside one div

I'm trying to load the WooCommerce .cross-sells div inside a custom div named .cart-collaterals-cross-sell which already includes the .cart-collaterals elements, so that i can style them better in one row. At the moment the .cross-sells is loaded by the cross-sells.php and i tried to implement the code of it into the cart.php, so that i have both functions in one file and that i could put them inside the same div. Problem is that the cross sells doesn't load when i copy the code inside the cart.php
That's the original code of the cart.php which includes already the .cart-collaterals
<div class="cart-collaterals-cross-sell">
<div class="cart-collaterals">
<h2><?php _e( 'Cart totals', 'woocommerce' ); ?></h2>
<?php if ( ! is_ajax() && wc_coupons_enabled() ) { ?>
<div class="nm-coupon-wrap">
<div class="nm-coupon-inner">
<?php esc_html_e( 'Gutschein', 'nm-framework' ); ?>
<div class="nm-coupon">
<input type="text" id="nm-coupon-code" class="input-text" name="nm_coupon_code" value="" placeholder="<?php esc_attr_e( 'Coupon code', 'woocommerce' ); ?>" />
<input type="submit" id="nm-apply-coupon-btn" class="button border" name="nm_apply_coupon" value="<?php esc_attr_e( 'Apply coupon', 'woocommerce' ); ?>" />
<?php do_action( 'woocommerce_cart_coupon' ); ?>
</div>
</div>
</div>
<?php } ?>
<?php
/**
* Cart collaterals hook.
*
* #hooked woocommerce_cross_sell_display
* #hooked woocommerce_cart_totals - 10
*/
do_action( 'woocommerce_cart_collaterals' );
?>
</div>
</div>
And that's the way i tried it. What am i doing wrong?
<div class="cart-collaterals-cross-sell">
<div class="cart-collaterals">
<h2><?php _e( 'Cart totals', 'woocommerce' ); ?></h2>
<?php if ( ! is_ajax() && wc_coupons_enabled() ) { ?>
<div class="nm-coupon-wrap">
<div class="nm-coupon-inner">
<?php esc_html_e( 'Gutschein', 'nm-framework' ); ?>
<div class="nm-coupon">
<input type="text" id="nm-coupon-code" class="input-text" name="nm_coupon_code" value="" placeholder="<?php esc_attr_e( 'Coupon code', 'woocommerce' ); ?>" />
<input type="submit" id="nm-apply-coupon-btn" class="button border" name="nm_apply_coupon" value="<?php esc_attr_e( 'Apply coupon', 'woocommerce' ); ?>" />
<?php do_action( 'woocommerce_cart_coupon' ); ?>
</div>
</div>
</div>
<?php } ?>
<?php
/**
* Cart collaterals hook.
*
* #hooked woocommerce_cross_sell_display
* #hooked woocommerce_cart_totals - 10
*/
do_action( 'woocommerce_cart_collaterals' );
?>
</div>
<div class="cross-sells">
<h2><?php _e( 'You may be interested in…', 'woocommerce' ) ?></h2>
<?php woocommerce_product_loop_start(); ?>
<?php foreach ( $cross_sells as $cross_sell ) : ?>
<?php
$post_object = get_post( $cross_sell->get_id() );
setup_postdata( $GLOBALS['post'] =& $post_object );
wc_get_template_part( 'content', 'product' ); ?>
<?php endforeach; ?>
<?php woocommerce_product_loop_end(); ?>
</div>
</div>
You can use following two functions
For Cart collaterals ==> woocommerce_cart_totals();
For Cross Sells =======> woocommerce_cross_sell_display();
Try Using following code I am providing here:
<div class="cart-collaterals-cross-sell">
<div class="cart-collaterals">
<h2><?php _e( 'Cart totals', 'woocommerce' ); ?></h2>
<?php if ( ! is_ajax() && wc_coupons_enabled() ) { ?>
<div class="nm-coupon-wrap">
<div class="nm-coupon-inner">
<?php esc_html_e( 'Gutschein', 'nm-framework' ); ?>
<div class="nm-coupon">
<input type="text" id="nm-coupon-code" class="input-text" name="nm_coupon_code" value="" placeholder="<?php esc_attr_e( 'Coupon code', 'woocommerce' ); ?>" />
<input type="submit" id="nm-apply-coupon-btn" class="button border" name="nm_apply_coupon" value="<?php esc_attr_e( 'Apply coupon', 'woocommerce' ); ?>" />
<?php do_action( 'woocommerce_cart_coupon' ); ?>
</div>
</div>
</div>
<?php } ?>
<?php
/**
* Cart collaterals hook.
*
* #hooked woocommerce_cross_sell_display
* #hooked woocommerce_cart_totals - 10
*/
//do_action( 'woocommerce_cart_collaterals' );
woocommerce_cart_totals();
?>
</div>
<div class="cross-sells">
<?php woocommerce_cross_sell_display(); ?>
</div>
</div>

Variable product dropdowns does not appear in the custom product page

I am building an e-commerce shop using WordPress that uses Woocommerce. I have a custom page for products page that needs to hold the dropdowns menus (from the variation attribute). But the drop down does not appear. How can call those variations into this page.
This is my code for the single-product.php:
<?php
$attributes = $product->get_attributes();
?>
<div class="ProductViewing_DescSizing">
<div class="row">
<?php foreach ( $attributes as $attribute_name => $options ) : ?>
<?php $terms = get_the_terms($product->id, $attribute_name); ?>
<div class="col-sm-6">
<div class="form-group">
<label for="<?php echo sanitize_title( $attribute_name ); ?>"><?php echo wc_attribute_label( $attribute_name ); ?></label><!-- get attribute name per product -->
<select class="form-control">
<?php foreach ($terms as $value) { ?>
<option value="<?php echo $value->name; ?>"> <?php echo $value->name; ?></option>
<?php } ?>
</select>
</div>
</div>
<?php endforeach;?>
</div>
<div class="addtocart">
<input type="submit" name="" value="ADD TO CART" class="btn btn-red btn-addtocart">
</div>
SHOW Size guide
</div>

Categories