Change WooCommerce Variable Product Custom Attributes dropdown option none name
Change WooCommerce Variable Product Custom Attributes dropdown option none name
I am building a website in WooCommerce and I have made a custom single-product page, as I wanted to style the page. I have created a child theme and within this child theme, the files structure looks like this:
#child-theme
|-- functions.php
|-- woocommerce.php
|-- woocommerce
| |-- add-to-cart
| |-- variable.php
|-- js
|-- custom-select-script.js
I have created a variable product and added attributes giving them names and values, like:
name: choose a price and values 25 | 50 | 75 | 100,
name: choose a card and values card1 | card2 | card 3.
I want to get this name / values as the default value for the generated dropdown, like this:
<select name="choose-a-price" id="choose-a-price">
<option value="">--Choose a Price--</option>
<option value="kind-1">K-1</option>
<option value="kind-2">k-2</option>
<option value="kind-3">k-3</option>
<option value="kind-4">k-4</option>
</select>
but I am getting all the time this:
<select name="choose-a-price" id="choose-a-price">
<option value="">--Choose a option--</option>
<option value="kind-1">K-1</option>
<option value="kind-2">k-2</option>
<option value="kind-3">k-3</option>
<option value="kind-4">k-4</option>
</select>
This is my variable.php and with the echo ($attribute_name), I see the name attributes, but somehow not getting them loaded in the dropdown.
This is my variable.php template file:
/**
* Variable product add to cart
*
* This template can be overridden by copying it to kaon-child/woocommerce/single-product/add-to-cart/variable.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://woo.com/document/template-structure/
* @package WooCommerce\Templates
* @version 6.1.0
*/
defined( 'ABSPATH' ) || exit;
global $product;
$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', __( 'Dit product is op dit moment niet meer beschikbaar.', 'woocommerce' ) ) ); ?></p>
<?php else : ?>
<table class="variations" cellspacing="0" role="presentation">
<tbody>
<?php foreach ( array_keys( $attributes ) as $attribute_name ) : ?>
<tr>
<th class="label"><label for="<?php echo esc_attr( sanitize_title( $attribute_name ) ); ?>"><?php echo wc_attribute_label( $attribute_name ); // WPCS: XSS ok. ?></label></th>
<td class="value">
<?php
wc_dropdown_variation_attribute_options(
array(
'options' => array_merge( array( '' => wc_attribute_label( $attribute_name ) ), $attributes[ $attribute_name ] ),
'attribute' => $attribute_name,
'product' => $product,
)
);
echo ($attribute_name);
echo end( $attribute_keys ) === $attribute_name ? wp_kses_post( apply_filters( 'woocommerce_reset_variations_link', '<a class="reset_variations" href="#">' . esc_html__( 'Clear', 'woocommerce' ) . '</a>' ) ) : '';
?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php do_action( 'woocommerce_after_variations_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; ?>
<?php do_action( 'woocommerce_after_variations_form' ); ?>
</form>
<?php
do_action( 'woocommerce_after_add_to_cart_form' );
I have tried many js, with wp filters and a lot of reading, but cant get it working.
How to make changes to the product attribute(s) dropdown(s)?
First, change your variable product attributes like:
Attribute name: Choose a Price and value(s): 25|50|75|100
Attribute name: Choose a Card and value(s): Card 1|Card 2|Card 3
Save attributes.
Check and regenerate, if needed, your Variations as you want.
Then add the following code snippet:
add_filter('woocommerce_dropdown_variation_attribute_options_html','customize_attribute_dropdown_show_option_none', 100, 2 );
function customize_attribute_dropdown_show_option_none( $html, $args ){
// Here, we define our related attribute slugs
$attribute_slugs = array('choose-a-price', 'choose-a-card');
if ( in_array( sanitize_title($args['attribute']), $attribute_slugs ) ) {
$html = str_replace($args['show_option_none'], $args['attribute'], $html);
}
return $html;
}
Code goes in functions.php file of your child theme (or in a plugin). Tested and work.
February 4, 2024
Turbo Multi-language Translator
Make the better internet purchasing globaly
Turbosify SEO Speed Booster
5.0 (7)•Free plan available
Get better conversions by improving store loading speed
Installed
Turbo Multi-language Chat - AI Customer service in one hand
February 4, 2024