How to change "add to cart" in WooCommerce? - php

I wanna use <ul> <li> instead of <select> <option>
I enter this code in function.php but doesn't work:
function wc_dropdown_variation_attribute_options( $args = array() ) {
.
.
.
$html = '<div class="tt-wrapper"><div class="tt-title-options">اندازه:</div><ul id="' . esc_attr( $id ) . '" class="tt-options-swatch options-large ' . esc_attr( $class ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" data-show_option_none="' . ( $show_option_none ? 'yes' : 'no' ) . '">';
if ( ! empty( $options ) ) {
if ( $product && taxonomy_exists( $attribute ) ) {
// Get terms if this is a taxonomy - ordered. We need the names too.
$terms = wc_get_product_terms(
$product->get_id(),
$attribute,
array(
'fields' => 'all',
)
);
foreach ( $terms as $term ) {
if ( in_array( $term->slug, $options, true ) ) {
$html .= '<li><a href="#" value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name, $term, $attribute, $product ) ) . '</a></li>';
}
}
} else {
foreach ( $options as $option ) {
// This handles < 2.4.0 bw compatibility where text attributes were not sanitized.
$selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false );
$html .= '<li><a href="#" value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option, null, $attribute, $product ) ) . '</a></li>';
}
}
}
$html .= '</ul></div></div>';
echo apply_filters( 'woocommerce_dropdown_variation_attribute_options_html', $html, $args ); // WPCS: XSS ok.
}
How can I change woocommerce HTML codes to my own HTML codes?
this is more detail for post this question

To override the default themes of WooCommerce you better have a "woocommerce" sub-directory on your theme folder.
for example:
/wp-content/themes/mytheme/woocommerce/
There you can specify exactly which of the themes you want to change.
Learn more about it here.
Also, you can hook to actions and filters. In the example you sent, you can hook to this filter: woocommerce_dropdown_variation_attribute_options_html to change it's final output.
Learn more about it here and here.

Related

Add Stock Status boolean as custom attribute of WooCommerce variation selector

I am using the code found here, provided by LoicTheAztec. It adds stock status related text to the variation selector on WooCommerce products. It works as required.
I would, however, like to also add a custom attribute to each <option ... > in the dropdown selector.
My reason for this is that I am writing some jQuery that will be triggered by whether or not the selected option is in stock. That will be cleanest if each option has a stock attribute. For example, <option value=“1kg” class=“ ... “ stock-status=“true”>1kg - (in stock)</option>. In this example I’ve added a custom attribute called stock-status, which will either be true or false.
The code in use is:
// Function that will check the stock status and display the corresponding additional text
function get_stock_status_text( $product, $name, $term_slug ){
foreach ( $product->get_available_variations() as $variation ){
if($variation['attributes'][$name] == $term_slug ) {
$stock = $variation['is_in_stock'];
break;
}
}
return $stock == 1 ? ' - (In Stock)' : ' - (Out of Stock)';
}
// The hooked function that will add the stock status to the dropdown options elements.
add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'show_stock_status_in_dropdown', 10, 2);
function show_stock_status_in_dropdown( $html, $args ) {
// Only if there is a unique variation attribute (one dropdown)
if( sizeof($args['product']->get_variation_attributes()) == 1 ) :
$options = $args['options'];
$product = $args['product'];
$attribute = $args['attribute']; // The product attribute taxonomy
$name = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title( $attribute );
$id = $args['id'] ? $args['id'] : sanitize_title( $attribute );
$class = $args['class'];
$show_option_none = $args['show_option_none'] ? true : false;
$show_option_none_text = $args['show_option_none'] ? $args['show_option_none'] : __( 'Choose an option', 'woocommerce' );
if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {
$attributes = $product->get_variation_attributes();
$options = $attributes[ $attribute ];
}
$html = '<select id="' . esc_attr( $id ) . '" class="' . esc_attr( $class ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" data-show_option_none="' . ( $show_option_none ? 'yes' : 'no' ) . '">';
$html .= '<option value="">' . esc_html( $show_option_none_text ) . '</option>';
if ( ! empty( $options ) ) {
if ( $product && taxonomy_exists( $attribute ) ) {
$terms = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) );
foreach ( $terms as $term ) {
if ( in_array( $term->slug, $options ) ) {
// HERE Added the function to get the text status
$stock_status = get_stock_status_text( $product, $name, $term->slug );
$html .= '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) . $stock_status ) . '</option>';
}
}
} else {
foreach ( $options as $option ) {
$selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false );
// HERE Added the function to get the text status
$stock_status = get_stock_status_text( $product, $name, $option );
$html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) . $stock_status ) . '</option>';
}
}
}
$html .= '</select>';
endif;
return $html;
}
My knowledge of PHP isn’t sufficient to know the best way to go about this. One crude method would be to test if the result of get_stock_status_text contains the string (In or (No, and then add stock-status=“true” (or false) too the $html output accordingly. I can likely come up with a way to do that, but I suspect that’s not the best way to go about it. I am thinking it would be better to set a variable as true or false within the get_stock_status_text function, and then based on the value of that a stock-status custom attribute would have a value of true|false accordingly.
What would be an efficient way to add a custom attribute to the $html output, with a true|false value?
With a bit of playing around, it looks like I’ve come up with a solution. If there’s a more efficient or tidier way to achieve the result, I’d be very interested to know. This solution does at least have the desired output.
I have modified the code (from my question) to the following:
/******* ADD STOCK STATUS TO DROP DOWN SELECTOR ******/
// Function that will check the stock status and display the corresponding additional text
function get_stock_status_text( $product, $name, $term_slug ){
foreach ( $product->get_available_variations() as $variation ){
if($variation['attributes'][$name] == $term_slug ) {
$stock = $variation['is_in_stock'];
break;
}
}
return $stock == 1 ? ' - (In Stock)' : ' - (No Stock)';
}
function get_stock_status_bool( $product, $name, $term_slug ){
foreach ( $product->get_available_variations() as $variation ){
if($variation['attributes'][$name] == $term_slug ) {
$stock_bool = $variation['is_in_stock'];
break;
}
}
return $stock_bool == 1 ? 'true' : 'false';
}
// The hooked function that will add the stock status to the dropdown options elements.
add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'show_stock_status_in_dropdown', 10, 2);
function show_stock_status_in_dropdown( $html, $args ) {
// Only if there is a unique variation attribute (one dropdown)
if( sizeof($args['product']->get_variation_attributes()) == 1 ) :
$options = $args['options'];
$product = $args['product'];
$attribute = $args['attribute']; // The product attribute taxonomy
$name = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title( $attribute );
$id = $args['id'] ? $args['id'] : sanitize_title( $attribute );
$class = $args['class'];
$show_option_none = $args['show_option_none'] ? true : false;
$show_option_none_text = $args['show_option_none'] ? $args['show_option_none'] : __( 'Choose an option', 'woocommerce' );
if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {
$attributes = $product->get_variation_attributes();
$options = $attributes[ $attribute ];
}
$html = '<select id="' . esc_attr( $id ) . '" class="' . esc_attr( $class ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" data-show_option_none="' . ( $show_option_none ? 'yes' : 'no' ) . '">';
$html .= '<option value="">' . esc_html( $show_option_none_text ) . '</option>';
if ( ! empty( $options ) ) {
if ( $product && taxonomy_exists( $attribute ) ) {
$terms = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) );
foreach ( $terms as $term ) {
if ( in_array( $term->slug, $options ) ) {
// HERE Added the function to get the text status
$stock_status = get_stock_status_text( $product, $name, $term->slug );
$stock_attr = get_stock_status_bool( $product, $name, $term->slug );
$html .= '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . ' stock-status="' . $stock_attr . '">' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) . $stock_status ) . '</option>';
}
}
} else {
foreach ( $options as $option ) {
$selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false );
// HERE Added the function to get the text status
$stock_status = get_stock_status_text( $product, $name, $option );
$stock_attr = get_stock_status_bool( $product, $name, $term->slug );
$html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . ' stock-status="' . $stock_attr . '">' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) . $stock_status ) . '</option>';
}
}
}
$html .= '</select>';
endif;
return $html;
}
The main addition is a new function, get_stock_status_bool. Which is a replica of get_stock_status_text except that it returns true or false.
This function is then called right after the get_stock_status_text function is called, and its value is assigned to $stock_attr.
$stock_attr has then been added into the $html value (e.g. <option value=“1 kg” selected=“selected” stock-status=“true” class=“ ...”>1kg - $150 - (In Stock)</option>

trying to style woocommerce dropdown with adding a span inside php code

I have PHP code like this:
$html .= '<option value="' . esc_attr( $term->slug ) . '" '
. selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>'
. esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) . $price_html )
. '</option>';
I want to add <span class="XXX"> before $price_html variable and closing span tag after that, so on I can style the price. I tried the code below but no success:
$html .= '<option value="' . esc_attr( $term->slug ) . '" '
. selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>'
. esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) . '<span class="XXX">' . $price_html . '</span>')
. '</option>';
Any help?
Edit: the code block i am trying to edit:
// Utility function to get the price of a variation from it's attribute value
function get_the_variation_price_html( $product, $name, $term_slug ){
foreach ( $product->get_available_variations() as $variation ){
if($variation['attributes'][$name] == $term_slug ){
return strip_tags( $variation['price_html'] );
}
}
}
// Add the price to the dropdown options items.
add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'show_price_in_attribute_dropdown', 10, 2);
function show_price_in_attribute_dropdown( $html, $args ) {
// Only if there is a unique variation attribute (one dropdown)
if( sizeof($args['product']->get_variation_attributes()) == 1 ) :
$options = $args['options'];
$product = $args['product'];
$attribute = $args['attribute'];
$name = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title( $attribute );
$id = $args['id'] ? $args['id'] : sanitize_title( $attribute );
$class = $args['class'];
$show_option_none = $args['show_option_none'] ? true : false;
$show_option_none_text = $args['show_option_none'] ? $args['show_option_none'] : __( 'Choose an option', 'woocommerce' );
if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {
$attributes = $product->get_variation_attributes();
$options = $attributes[ $attribute ];
}
$html = '<select id="' . esc_attr( $id ) . '" class="' . esc_attr( $class ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" data-show_option_none="' . ( $show_option_none ? 'yes' : 'no' ) . '">';
$html .= '<option value="">' . esc_html( $show_option_none_text ) . '</option>';
if ( ! empty( $options ) ) {
if ( $product && taxonomy_exists( $attribute ) ) {
$terms = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) );
foreach ( $terms as $term ) {
if ( in_array( $term->slug, $options ) ) {
// Get and inserting the price
$price_html = get_the_variation_price_html( $product, $name, $term->slug );
$html .= '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) . $price_html ) . '</option>';
}
}
} else {
foreach ( $options as $option ) {
$selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false );
// Get and inserting the price
$price_html = get_the_variation_price_html( $product, $name, $term->slug );
$html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) . ' = ' . $price_html ) . '</option>';
}
}
}
$html .= '</select>';
endif;
return $html;
}
The reason why you don't see the desired output is in esc_html() function. Also, <span> tag inside a <option> tag doesn't get any CSS styles. You have to place desired class="XXX" inside the <option> tag.
For now you have something like:
<option value="123">escaped_price_html</option>
Replace lines:
'<option value="' . esc_attr( $term->slug ) . '" '
with
'<option class="XXX" value="' . esc_attr( $term->slug ) . '" '
and
. esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) . '<span class="XXX">' . $price_html . '</span>')
with
. esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) . $price_html)
which will change the structure on:
<option class="XXX" value="123">escaped_price_html</option>
.x{
color: blue;
font-weight: bold;
}
<select>
<option value="1"><span class="x">TEXT1</span></option>
<option class="x" value="2"><span>TEXT2</span></option>
<option value="3"><span class="x">TEXT3</span></option>
</select>

display by stock quantity woocommorce

I'm preparing a shopping site with woocommerce. As in the sample link, I show the product stock information in the drop down menu. this is the code I use:
$html = '<select id="' . esc_attr( $id ) . '" class="' . esc_attr( $class ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" data-show_option_none="' . ( $show_option_none ? 'yes' : 'no' ) . '">';
$html .= '<option value="">' . esc_html( $show_option_none_text ) . '</option>';
if ( ! empty( $options ) ) {
if ( $product && taxonomy_exists( $attribute ) ) {
// Get terms if this is a taxonomy - ordered. We need the names too.
$terms = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) );
foreach ( $terms as $term ) {
if ( in_array( $term->slug, $options ) ) {
$stock_status = get_variation_stock_status( $product, $name, $term->slug );
$html .= '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ).$stock_status ) . '</option>';
}
}
} else {
foreach ( $options as $option ) {
// This handles < 2.4.0 bw compatibility where text attributes were not sanitized.
$selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false );
$html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) ) . '</option>';
}
}
}
$html .= '</select>';
return $html;
}
function get_variation_stock_status( $product, $name, $term_slug ){
foreach ( $product->get_available_variations() as $variation ){
if($variation['attributes'][$name] == $term_slug ){
$variation_obj = wc_get_product( $variation['variation_id'] );
$stock_qty = $variation_obj->get_stock_quantity();
break;
}
}
return $stock_qty == 0 ? ' - ' . __(pll__('Stokta Yok'), 'mytheme') : ' - ' . $stock_qty . ' ' . __(pll__('adet Stokta'), 'mytheme-');
}
If the stock quantity is over 20, I don't want to show this information. how can I do that?
Just add a check to the get_variation_stock_status function:
function get_variation_stock_status( $product, $name, $term_slug ){
foreach ( $product->get_available_variations() as $variation ){
if ( $variation['attributes'][$name] == $term_slug ) {
$variation_obj = wc_get_product( $variation['variation_id'] );
// if the stock of the product is greater than 20 it returns
if ( $variation_obj->get_stock_quantity() > 20 ) {
return '';
}
$stock_qty = $variation_obj->get_stock_quantity();
break;
}
}
return $stock_qty == 0 ? ' - (Out Of Stock)' : ' - ' . $stock_qty . ' In Stock';
}
The entire code (from the link in your comment) has been tested and works. Add it to your active theme's functions.php.

Add a custom class to variation options displaying "Out Of Stock" in Woocommerce

I'm using "https://stackoverflow.com/questions/47180058/how-to-add-variation-stock-status-to-woocommerce-product-variation-dropdown/47189725#47189725" answer code to show the text "In Stock" or "Out of stock" according to the stock status of the variation.
This is what I get:
I need to add a custom class only to these variants that showing text "Out of Stock".
How can this be done?
The following code will add the class outofstock to <option> html tag when the variation is "Out of Stock":
// Function that will check the stock status and display the corresponding additional text
function get_stock_status_text( $product, $name, $term_slug ){
foreach ( $product->get_available_variations() as $variation ){
if($variation['attributes'][$name] == $term_slug )
$stock = $variation['is_in_stock'];
}
return $stock == 1 ? ' - (In Stock)' : ' - (Out of Stock)';
}
// The hooked function that will add the stock status to the dropdown options elements.
add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'show_stock_status_in_dropdown', 10, 2);
function show_stock_status_in_dropdown( $html, $args ) {
// Only if there is a unique variation attribute (one dropdown)
if( sizeof($args['product']->get_variation_attributes()) == 1 ) :
$options = $args['options'];
$product = $args['product'];
$attribute = $args['attribute']; // The product attribute taxonomy
$name = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title( $attribute );
$id = $args['id'] ? $args['id'] : sanitize_title( $attribute );
$class = $args['class'];
$show_option_none = $args['show_option_none'] ? true : false;
$show_option_none_text = $args['show_option_none'] ? $args['show_option_none'] : __( 'Choose an option', 'woocommerce' );
if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {
$attributes = $product->get_variation_attributes();
$options = $attributes[ $attribute ];
}
$html = '<select id="' . esc_attr( $id ) . '" class="' . esc_attr( $class ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" data-show_option_none="' . ( $show_option_none ? 'yes' : 'no' ) . '">';
$html .= '<option value="">' . esc_html( $show_option_none_text ) . '</option>';
if ( ! empty( $options ) ) {
if ( $product && taxonomy_exists( $attribute ) ) {
$terms = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) );
foreach ( $terms as $term ) {
if ( in_array( $term->slug, $options ) ) {
// HERE Added the function to get the text status
$stock_status = get_stock_status_text( $product, $name, $term->slug );
// HERE we add a custom class to <option> that are "Out of stock"
$option_class = $stock_status === ' - (Out of Stock)' ? ' class="outofstock"' : '';
$html .= '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . $option_class . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) . $stock_status ) . '</option>';
}
}
} else {
foreach ( $options as $option ) {
$selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false );
// HERE Added the function to get the text status
$stock_status = get_the_stock_status( $product, $name, $option );
// HERE we add a custom class to <option> that are "Out of stock"
$option_class = $stock_status === ' - (Out of Stock)' ? ' class="outofstock"' : '';
$html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . $option_class . '>' .
esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) . $stock_status ) . '</option>';
}
}
}
$html .= '</select>';
endif;
return $html;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.

Custom textbox in wordpress woocommerce

Is it possible to get a normal text instead to use a slug? I need to use this custom code for Wordpress WooCommerce product page. The small problem the output get small letters and line "-" between words.
<?php /*CUSTOM MID*/ else: ?>
<td class="value">
<select id="<?php echo esc_attr( sanitize_title($name) ); ?>"
name="attribute_<?php echo sanitize_title($name); ?>">
<option value="">
<?php echo __('Choose an option', 'woocommerce') ?>…
</option>
<?php
if ( is_array( $options ) ) {
if ( empty( $_POST ) )
$selected_value = ( isset( $selected_attributes[ sanitize_title( $name ) ] ) ) ? $selected_attributes[ sanitize_title( $name ) ] : '';
else
$selected_value = isset( $_POST[ 'attribute_' . sanitize_title( $name ) ] ) ? $_POST[ 'attribute_' . sanitize_title( $name ) ] : '';
// Get terms if this is a taxonomy - ordered
if ( taxonomy_exists( sanitize_title( $name ) ) ) {
$terms = get_terms( sanitize_title($name), array('menu_order' => 'ASC') );
foreach ( $terms as $term ) {
if ( ! in_array( $term->slug, $options ) ) continue;
echo '<option value="' . $term->slug . '" ' . selected( $selected_value, $term->slug, false ) . '>' . apply_filters( 'woocommerce_variation_option_name', $term->name ) . '</option>';
}
} else {
foreach ( $options as $option )
echo '<option value="' . $option . '" ' . selected( $selected_value, $option, false ) . '>' . apply_filters( 'woocommerce_variation_option_name', $option ) . '</option>';
}
}
?>
</select> <?php
if ( sizeof($attributes) == $loop )
echo '<a class="reset_variations" href="#reset">'.__('Clear selection', 'woocommerce').'</a>';
?></td>
remove - you can do it by pure php using str_replace
You have to give us more of your code to understand it.

Categories