I need to add a css style class for only "label" products that are not in stock.
Example Original Code:
<label class="radio-label sw-radio-variation sw-radio-variation-2" title="l" for="l_2">
The code I want:
<label class="out-stock radio-label sw-radio-variation sw-radio-variation-2" title="l" for="l_2">
As you see add to the CSS code out-stock
Thanks
Edit: My product-variation.php is
<?php
/*
** Product variation hook
*/
add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'sw_woocommerce_custom_variation', 10, 2 );
function sw_woocommerce_custom_variation( $html, $args ){
$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'];
if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {
$attributes = $product->get_variation_attributes();
$options = $attributes[ $attribute ];
}
$html = '';
if ( ! empty( $options ) ) {
$html .= '<div class="sw-custom-variation">';
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 $key => $term ) {
$color = get_term_meta( $term->term_id, 'sw_variation_color', true );
$active = ( checked( sanitize_title( $args['selected'] ), $term->slug, false ) ) ? ' selected' : '';
$attributes = ( preg_match( '/color|colors/', $attribute, $match ) && $color != '' ) ? 'class="variation-color" style="background: '. esc_attr( $color ) .'"' : '';
if ( in_array( $term->slug, $options ) ) {
$html .= '<label class="radio-label sw-radio-variation sw-radio-variation-'. esc_attr( $key .' '. $active ) .'" title="'. esc_attr( $term->slug ) .'" for="'. esc_attr( $term->slug . '_' . $key ) . '">';
$html .= '<input type="radio" id="'. esc_attr( $term->slug . '_' . $key ) .'" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" value="' . esc_attr( $term->slug ) . '" '. checked( sanitize_title( $args['selected'] ), $term->slug, false ) .'/>';
$html .= '<span '. $attributes .'>'. $term->name .'</span>';
$html .= '</label>';
}
}
}else {
foreach ( $options as $key => $option ) {
// This handles < 2.4.0 bw compatibility where text attributes were not sanitized.
$checked = sanitize_title( $args['selected'] ) === $args['selected'] ? checked( $args['selected'], sanitize_title( $option ), false ) : checked( $args['selected'], $option, false );
$active = ( $checked ) ? 'selected' : '';
$html .= '<label class="radio-label sw-radio-variation sw-radio-variation-'. esc_attr( $key .' '. $active ) .'" title="'. esc_attr( $option ) .'" for="'. esc_attr( $option . '_' . $key ) . '">';
$html .= '<input type="radio" id="'. esc_attr( $option . '_' . $key ) .'" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" value="' . esc_attr( $option ) . '" '. $checked .'/>';
$html .= '<span>'. $option .'</span>';
$html .= '</label>';
}
}
$html .= '</div>';
}
return $html;
}
/*
** Action hook to color
*/
$sw_attribute_taxonomies = wc_get_attribute_taxonomies();
if ( ! empty( $sw_attribute_taxonomies ) ) {
foreach( $sw_attribute_taxonomies as $attr ){
if( preg_match( '/color|colors/', $attr->attribute_name, $match ) ){
add_action( 'pa_'. $attr->attribute_name .'_add_form_fields', 'sw_woocommerce_variation_fields', 200 );
add_action( 'pa_'. $attr->attribute_name .'_edit_form_fields', 'sw_woocommerce_edit_variation_fields', 200 );
add_action( 'created_term', 'sw_woocommerce_save_variation_fields', 10, 3 );
add_action( 'edit_terms', 'sw_woocommerce_save_variation_fields', 10, 3 );
/* Enqueue Admin js */
add_action( 'admin_enqueue_scripts', 'sw_woocommerce_variation_color_script' );
}
}
}
/*
** Create color
*/
function sw_woocommerce_variation_fields() {
?>
<div class="form-field custom-picker">
<label for="sw_variation_color"><?php _e( 'Color', 'sw_woocommerce' ); ?></label>
<input name="sw_variation_color" id="sw_variation_color" type="text" value="" size="40" class="category-colorpicker"/>
</div>
<?php
}
function sw_woocommerce_edit_variation_fields( $term ) {
$sw_variation_color = get_term_meta( $term->term_id, 'sw_variation_color', true );
?>
<tr class="form-field custom-picker custom-picker-edit">
<th scope="row" valign="top"><label for="sw_variation_color"><?php _e( 'Color', 'sw_woocommerce' ); ?></label></th>
<td>
<input name="sw_variation_color" id="sw_variation_color" type="text" value="<?php echo esc_attr( $sw_variation_color ) ?>" size="40" class="category-colorpicker"/>
</td>
</tr>
<?php
}
/** Save Custom Field Of Category Form */
function sw_woocommerce_save_variation_fields( $term_id, $tt_id = '', $taxonomy = '', $prev_value = '' ) {
if ( isset( $_POST['sw_variation_color'] ) ) {
$term_value = esc_attr( $_POST['sw_variation_color'] );
update_term_meta( $term_id, 'sw_variation_color', $term_value, $prev_value );
}
}
function sw_woocommerce_variation_color_script(){
wp_enqueue_style( 'wp-color-picker' );
wp_enqueue_script('category_color_picker_js', WCURL . '/js/admin/category_color_picker.js', array( 'wp-color-picker' ), false, true);
}
As you can see on line 60 you will find the labels
Will there be any code to add a custom CSS to these labels that create the variables?
Thanks
************** SOLUTION ***********
<?php
// Adiccional Class For Label Variations
function get_variation_stock_text( $product, $name, $term_slug ){
foreach ( $product->get_available_variations() as $variation ){
if($variation['attributes'][$name] == $term_slug ){
$is_in_stock = $variation['is_in_stock'];
$stock_qty = get_post_meta($variation['variation_id'], '_stock', true);
}
}
$in_stock = ' '.__("katayainstock", "woocommerce").'';
$out_of_stock = ' '.__("katayaoutstock", "woocommerce").'';
return $is_in_stock == 1 ? $in_stock : $out_of_stock;
}
/**
* Product variation hook
*/
add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'sw_woocommerce_custom_variation', 10, 2 );
function sw_woocommerce_custom_variation( $html, $args ) {
$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'];
if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {
$attributes = $product->get_variation_attributes();
$options = $attributes[ $attribute ];
}
$html = '';
if ( ! empty( $options ) ) {
$html .= '<div class="sw-custom-variation">';
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 $key => $term ) {
$color = get_term_meta( $term->term_id, 'sw_variation_color', true );
$active = checked( sanitize_title( $args['selected'] ), $term->slug, false ) ? ' selected' : '';
$attributes = ( preg_match( '/color|colors/', $attribute, $match ) && $color !== '' ) ? 'class="variation-color" style="background: ' . esc_attr( $color ) . '"' : '';
if ( in_array( $term->slug, $options, true ) ) {
$stock_text = get_variation_stock_text( $product, $name, $term->slug );
$html .= '<label class="' . $stock_text . ' radio-label sw-radio-variation sw-radio-variation-' . esc_attr( $key . ' ' . $active ) . '" title="' . esc_attr( $term->slug ) . '" for="' . esc_attr( $term->slug . '_' . $key ) . '">';
$html .= '<input type="radio" id="' . esc_attr( $term->slug . '_' . $key ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" value="' . esc_attr( $term->slug ) . '" ' . checked( sanitize_title( $args['selected'] ), $term->slug, false ) . '/>';
$html .= '<span ' . $attributes . '>' . $term->name . '</span>';
$html .= '</label>';
}
}
} else {
foreach ( $options as $key => $option ) {
// This handles < 2.4.0 bw compatibility where text attributes were not sanitized.
$checked = sanitize_title( $args['selected'] ) === $args['selected'] ? checked( $args['selected'], sanitize_title( $option ), false ) : checked( $args['selected'], $option, false );
$active = ( $checked ) ? 'selected' : '';
$html .= '<label class="radio-label sw-radio-variation sw-radio-variation-' . esc_attr( $key . ' ' . $active ) . '" title="' . esc_attr( $option ) . '" for="' . esc_attr( $option . '_' . $key ) . '">';
$html .= '<input type="radio" id="' . esc_attr( $option . '_' . $key ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" value="' . esc_attr( $option ) . '" ' . $checked . '/>';
$html .= '<span>' . $option . '</span>';
$html .= '</label>';
}
}
$html .= '</div>';
}
return $html;
}
/**
* Action hook color
*/
$sw_attribute_taxonomies = wc_get_attribute_taxonomies();
if ( ! empty( $sw_attribute_taxonomies ) ) {
foreach ( $sw_attribute_taxonomies as $attr ) {
if ( preg_match( '/color|colors/', $attr->attribute_name, $match ) ) {
add_action( 'pa_' . $attr->attribute_name . '_add_form_fields', 'sw_woocommerce_variation_fields', 200 );
add_action( 'pa_' . $attr->attribute_name . '_edit_form_fields', 'sw_woocommerce_edit_variation_fields', 200 );
add_action( 'created_term', 'sw_woocommerce_save_variation_fields', 10, 3 );
add_action( 'edit_terms', 'sw_woocommerce_save_variation_fields', 10, 3 );
add_action( 'admin_enqueue_scripts', 'sw_woocommerce_variation_color_script' );
}
}
}
/**
* Create color
*/
function sw_woocommerce_variation_fields() { ?>
<div class="form-field custom-picker">
<label for="sw_variation_color"><?php _e( 'Color', 'sw_woocommerce' ); ?></label>
<input name="sw_variation_color" id="sw_variation_color" type="text" value="" size="40"
class="category-colorpicker"/>
</div>
<?php
}
/**
* #param $term
*/
function sw_woocommerce_edit_variation_fields( $term ) {
$sw_variation_color = get_term_meta( $term->term_id, 'sw_variation_color', true ); ?>
<tr class="form-field custom-picker custom-picker-edit">
<th scope="row" valign="top"><label
for="sw_variation_color"><?php _e( 'Color', 'sw_woocommerce' ); ?></label></th>
<td>
<input name="sw_variation_color" id="sw_variation_color" type="text"
value="<?php echo esc_attr( $sw_variation_color ) ?>" size="40" class="category-colorpicker"/>
</td>
</tr>
<?php
}
/**
* Save custom field of category form
*
* #param $term_id
* #param string $tt_id
* #param string $taxonomy
* #param string $prev_value
*/
function sw_woocommerce_save_variation_fields( $term_id, $tt_id = '', $taxonomy = '', $prev_value = '' ) {
if ( isset( $_POST['sw_variation_color'] ) ) {
$term_value = esc_attr( $_POST['sw_variation_color'] );
update_term_meta( $term_id, 'sw_variation_color', $term_value, $prev_value );
}
}
function sw_woocommerce_variation_color_script() {
wp_enqueue_style( 'wp-color-picker' );
wp_enqueue_script( 'category_color_picker_js', WCURL . '/js/admin/category_color_picker.js', array( 'wp-color-picker' ), false, true );
}
Related
This question already has answers here:
Remove attribute values from product variation title and show them on separate rows
(1 answer)
Display variations attributes values as separate rows in Woocommerce cart / checkout
(1 answer)
Closed 2 years ago.
I noticed when I add to Cart products that have variations, Woocommerce uses 2 templates in Cart or Checkout pages.
if the product has only one or two variations, it uses this template as an example of my product:
<tr class="cart_item">
<td class="product-name">
3-in-1 Rechargeable Trimmer Grinder - CHINA, 3 IN 1 Grooming N5
<strong class="product-quantity">× 2</strong>
</td>
</tr>
If the product has more than two variations, it uses the following:
<tr class="cart_item">
<td class="product-name">
Mini Shaver Hair Trimmer Rechargeable
<strong class="product-quantity">× 1</strong>
<dl class="variation">
<dt class="variation-Size">Size:</dt>
<dd class="variation-Size"><p>Standard</p> </dd>
<dt class="variation-ShipsFrom">Ships From:</dt>
<dd class="variation-ShipsFrom"><p>CHINA</p> </dd>
<dt class="variation-Color">Color:</dt>
<dd class="variation-Color"><p>White</p> </dd>
</dl>
</td>
</tr>
As you notice when there are more than two it uses the class variations.
my question how to modify Woocommerce so that it uses only the 2nd template (more than two variations), in order to simplify code when i want customizing css or javascript.
more details :
review-order.php:
<tbody>
<?php
do_action( 'woocommerce_review_order_before_cart_contents' );
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_checkout_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
?>
<tr class="<?php echo esc_attr( apply_filters( 'woocommerce_cart_item_class', 'cart_item', $cart_item, $cart_item_key ) ); ?>">
<td class="product-name">
<?php echo apply_filters( 'woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key ) . ' '; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<?php echo apply_filters( 'woocommerce_checkout_cart_item_quantity', ' <strong class="product-quantity">' . sprintf( '× %s', $cart_item['quantity'] ) . '</strong>', $cart_item, $cart_item_key ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<?php echo wc_get_formatted_cart_item_data( $cart_item ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
</td>
<td class="product-total">
<?php echo apply_filters( 'woocommerce_cart_item_subtotal', WC()->cart->get_product_subtotal( $_product, $cart_item['quantity'] ), $cart_item, $cart_item_key ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
</td>
</tr>
<?php
}
}
do_action( 'woocommerce_review_order_after_cart_contents' );
?>
</tbody>
cart-item-data.php:
<?php
/**
* Cart item data (when outputting non-flat)
*
* This template can be overridden by copying it to yourtheme/woocommerce/cart/cart-item-data.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 2.4.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
?>
<dl class="variation">
<?php foreach ( $item_data as $data ) : ?>
<dt class="<?php echo sanitize_html_class( 'variation-' . $data['key'] ); ?>"><?php echo wp_kses_post( $data['key'] ); ?>:</dt>
<dd class="<?php echo sanitize_html_class( 'variation-' . $data['key'] ); ?>"><?php echo wp_kses_post( wpautop( $data['display'] ) ); ?></dd>
<?php endforeach; ?>
</dl>
cart.php :
<tbody>
<?php do_action( 'woocommerce_before_cart_contents' ); ?>
<?php
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
$product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
$product_permalink = apply_filters( 'woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink( $cart_item ) : '', $cart_item, $cart_item_key );
?>
<tr class="woocommerce-cart-form__cart-item <?php echo esc_attr( apply_filters( 'woocommerce_cart_item_class', 'cart_item', $cart_item, $cart_item_key ) ); ?>">
<td class="product-remove">
<?php
echo apply_filters( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
'woocommerce_cart_item_remove_link',
sprintf(
'×',
esc_url( wc_get_cart_remove_url( $cart_item_key ) ),
esc_html__( 'Remove this item', 'woocommerce' ),
esc_attr( $product_id ),
esc_attr( $_product->get_sku() )
),
$cart_item_key
);
?>
</td>
<td class="product-thumbnail">
<?php
$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );
if ( ! $product_permalink ) {
echo $thumbnail; // PHPCS: XSS ok.
} else {
printf( '%s', esc_url( $product_permalink ), $thumbnail ); // PHPCS: XSS ok.
}
?>
</td>
<td class="product-name" data-title="<?php esc_attr_e( 'Product', 'woocommerce' ); ?>">
<?php
if ( ! $product_permalink ) {
echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key ) . ' ' );
} else {
echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', sprintf( '%s', esc_url( $product_permalink ), $_product->get_name() ), $cart_item, $cart_item_key ) );
}
do_action( 'woocommerce_after_cart_item_name', $cart_item, $cart_item_key );
// Meta data.
echo wc_get_formatted_cart_item_data( $cart_item ); // PHPCS: XSS ok.
// Backorder notification.
if ( $_product->backorders_require_notification() && $_product->is_on_backorder( $cart_item['quantity'] ) ) {
echo wp_kses_post( apply_filters( 'woocommerce_cart_item_backorder_notification', '<p class="backorder_notification">' . esc_html__( 'Available on backorder', 'woocommerce' ) . '</p>', $product_id ) );
}
?>
</td>
<td class="product-price" data-title="<?php esc_attr_e( 'Price', 'woocommerce' ); ?>">
<?php
echo apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key ); // PHPCS: XSS ok.
?>
</td>
<td class="product-quantity" data-title="<?php esc_attr_e( 'Quantity', 'woocommerce' ); ?>">
<?php
if ( $_product->is_sold_individually() ) {
$product_quantity = sprintf( '1 <input type="hidden" name="cart[%s][qty]" value="1" />', $cart_item_key );
} else {
$product_quantity = woocommerce_quantity_input(
array(
'input_name' => "cart[{$cart_item_key}][qty]",
'input_value' => $cart_item['quantity'],
'max_value' => $_product->get_max_purchase_quantity(),
'min_value' => '0',
'product_name' => $_product->get_name(),
),
$_product,
false
);
}
echo apply_filters( 'woocommerce_cart_item_quantity', $product_quantity, $cart_item_key, $cart_item ); // PHPCS: XSS ok.
?>
</td>
<td class="product-subtotal" data-title="<?php esc_attr_e( 'Subtotal', 'woocommerce' ); ?>">
<?php
echo apply_filters( 'woocommerce_cart_item_subtotal', WC()->cart->get_product_subtotal( $_product, $cart_item['quantity'] ), $cart_item, $cart_item_key ); // PHPCS: XSS ok.
?>
</td>
</tr>
<?php
}
}
?>
<?php do_action( 'woocommerce_cart_contents' ); ?>
<tr>
<td colspan="6" class="actions">
<?php if ( wc_coupons_enabled() ) { ?>
<div class="coupon">
<label for="coupon_code"><?php esc_html_e( 'Coupon:', 'woocommerce' ); ?></label> <input type="text" name="coupon_code" class="input-text" id="coupon_code" value="" placeholder="<?php esc_attr_e( 'Coupon code', 'woocommerce' ); ?>" /> <button type="submit" class="button" name="apply_coupon" value="<?php esc_attr_e( 'Apply coupon', 'woocommerce' ); ?>"><?php esc_attr_e( 'Apply coupon', 'woocommerce' ); ?></button>
<?php do_action( 'woocommerce_cart_coupon' ); ?>
</div>
<?php } ?>
<button type="submit" class="button" name="update_cart" value="<?php esc_attr_e( 'Update cart', 'woocommerce' ); ?>"><?php esc_html_e( 'Update cart', 'woocommerce' ); ?></button>
<?php do_action( 'woocommerce_cart_actions' ); ?>
<?php wp_nonce_field( 'woocommerce-cart', 'woocommerce-cart-nonce' ); ?>
</td>
</tr>
<?php do_action( 'woocommerce_after_cart_contents' ); ?>
</tbody>
wc-product-functions.php :
/**
* Variation Formatting.
*
* Gets a formatted version of variation data or item meta.
*
* #param array|WC_Product_Variation $variation Variation object.
* #param bool $flat Should this be a flat list or HTML list? (default: false).
* #param bool $include_names include attribute names/labels in the list.
* #param bool $skip_attributes_in_name Do not list attributes already part of the variation name.
* #return string
*/
function wc_get_formatted_variation( $variation, $flat = false, $include_names = true, $skip_attributes_in_name = false ) {
$return = '';
if ( is_a( $variation, 'WC_Product_Variation' ) ) {
$variation_attributes = $variation->get_attributes();
$product = $variation;
$variation_name = $variation->get_name();
} else {
$product = false;
$variation_name = '';
// Remove attribute_ prefix from names.
$variation_attributes = array();
if ( is_array( $variation ) ) {
foreach ( $variation as $key => $value ) {
$variation_attributes[ str_replace( 'attribute_', '', $key ) ] = $value;
}
}
}
$list_type = $include_names ? 'dl' : 'ul';
if ( is_array( $variation_attributes ) ) {
if ( ! $flat ) {
$return = '<' . $list_type . ' class="variation">';
}
$variation_list = array();
foreach ( $variation_attributes as $name => $value ) {
// If this is a term slug, get the term's nice name.
if ( taxonomy_exists( $name ) ) {
$term = get_term_by( 'slug', $value, $name );
if ( ! is_wp_error( $term ) && ! empty( $term->name ) ) {
$value = $term->name;
}
}
// Do not list attributes already part of the variation name.
if ( '' === $value || ( $skip_attributes_in_name && wc_is_attribute_in_product_name( $value, $variation_name ) ) ) {
continue;
}
if ( $include_names ) {
if ( $flat ) {
$variation_list[] = wc_attribute_label( $name, $product ) . ': ' . rawurldecode( $value );
} else {
$variation_list[] = '<dt>' . wc_attribute_label( $name, $product ) . ':</dt><dd>' . rawurldecode( $value ) . '</dd>';
}
} else {
if ( $flat ) {
$variation_list[] = rawurldecode( $value );
} else {
$variation_list[] = '<li>' . rawurldecode( $value ) . '</li>';
}
}
}
if ( $flat ) {
$return .= implode( ', ', $variation_list );
} else {
$return .= implode( '', $variation_list );
}
if ( ! $flat ) {
$return .= '</' . $list_type . '>';
}
}
return $return;
}
class-wc-order-item-meta.php :
/**
* Display meta in a formatted list.
*
* #param bool $flat Flat (default: false).
* #param bool $return Return (default: false).
* #param string $hideprefix Hide prefix (default: _).
* #param string $delimiter Delimiter used to separate items when $flat is true.
* #return string|void
*/
public function display( $flat = false, $return = false, $hideprefix = '_', $delimiter = ", \n" ) {
$output = '';
$formatted_meta = $this->get_formatted( $hideprefix );
if ( ! empty( $formatted_meta ) ) {
$meta_list = array();
foreach ( $formatted_meta as $meta ) {
if ( $flat ) {
$meta_list[] = wp_kses_post( $meta['label'] . ': ' . $meta['value'] );
} else {
$meta_list[] = '
<dt class="variation-' . sanitize_html_class( sanitize_text_field( $meta['key'] ) ) . '">' . wp_kses_post( $meta['label'] ) . ':</dt>
<dd class="variation-' . sanitize_html_class( sanitize_text_field( $meta['key'] ) ) . '">' . wp_kses_post( wpautop( make_clickable( $meta['value'] ) ) ) . '</dd>
';
}
}
if ( ! empty( $meta_list ) ) {
if ( $flat ) {
$output .= implode( $delimiter, $meta_list );
} else {
$output .= '<dl class="variation">' . implode( '', $meta_list ) . '</dl>';
}
}
}
$output = apply_filters( 'woocommerce_order_items_meta_display', $output, $this, $flat );
if ( $return ) {
return $output;
} else {
echo $output; // WPCS: XSS ok.
}
}
I need to add a css style class for only "label" products that are not in stock.
Example Original Code:
<label class="radio-label sw-radio-variation sw-radio-variation-2" title="l" for="l_2">
The code I want:
<label class="out-stock radio-label sw-radio-variation sw-radio-variation-2" title="l" for="l_2">
As you see add to the CSS code out-stock
Thanks
Edit: My product-variation.php is
<?php
/*
** Product variation hook
*/
add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'sw_woocommerce_custom_variation', 10, 2 );
function sw_woocommerce_custom_variation( $html, $args ){
$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'];
if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {
$attributes = $product->get_variation_attributes();
$options = $attributes[ $attribute ];
}
$html = '';
if ( ! empty( $options ) ) {
$html .= '<div class="sw-custom-variation">';
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 $key => $term ) {
$color = get_term_meta( $term->term_id, 'sw_variation_color', true );
$active = ( checked( sanitize_title( $args['selected'] ), $term->slug, false ) ) ? ' selected' : '';
$attributes = ( preg_match( '/color|colors/', $attribute, $match ) && $color != '' ) ? 'class="variation-color" style="background: '. esc_attr( $color ) .'"' : '';
if ( in_array( $term->slug, $options ) ) {
$html .= '<label class="radio-label sw-radio-variation sw-radio-variation-'. esc_attr( $key .' '. $active ) .'" title="'. esc_attr( $term->slug ) .'" for="'. esc_attr( $term->slug . '_' . $key ) . '">';
$html .= '<input type="radio" id="'. esc_attr( $term->slug . '_' . $key ) .'" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" value="' . esc_attr( $term->slug ) . '" '. checked( sanitize_title( $args['selected'] ), $term->slug, false ) .'/>';
$html .= '<span '. $attributes .'>'. $term->name .'</span>';
$html .= '</label>';
}
}
}else {
foreach ( $options as $key => $option ) {
// This handles < 2.4.0 bw compatibility where text attributes were not sanitized.
$checked = sanitize_title( $args['selected'] ) === $args['selected'] ? checked( $args['selected'], sanitize_title( $option ), false ) : checked( $args['selected'], $option, false );
$active = ( $checked ) ? 'selected' : '';
$html .= '<label class="radio-label sw-radio-variation sw-radio-variation-'. esc_attr( $key .' '. $active ) .'" title="'. esc_attr( $option ) .'" for="'. esc_attr( $option . '_' . $key ) . '">';
$html .= '<input type="radio" id="'. esc_attr( $option . '_' . $key ) .'" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" value="' . esc_attr( $option ) . '" '. $checked .'/>';
$html .= '<span>'. $option .'</span>';
$html .= '</label>';
}
}
$html .= '</div>';
}
return $html;
}
/*
** Action hook to color
*/
$sw_attribute_taxonomies = wc_get_attribute_taxonomies();
if ( ! empty( $sw_attribute_taxonomies ) ) {
foreach( $sw_attribute_taxonomies as $attr ){
if( preg_match( '/color|colors/', $attr->attribute_name, $match ) ){
add_action( 'pa_'. $attr->attribute_name .'_add_form_fields', 'sw_woocommerce_variation_fields', 200 );
add_action( 'pa_'. $attr->attribute_name .'_edit_form_fields', 'sw_woocommerce_edit_variation_fields', 200 );
add_action( 'created_term', 'sw_woocommerce_save_variation_fields', 10, 3 );
add_action( 'edit_terms', 'sw_woocommerce_save_variation_fields', 10, 3 );
/* Enqueue Admin js */
add_action( 'admin_enqueue_scripts', 'sw_woocommerce_variation_color_script' );
}
}
}
/*
** Create color
*/
function sw_woocommerce_variation_fields() {
?>
<div class="form-field custom-picker">
<label for="sw_variation_color"><?php _e( 'Color', 'sw_woocommerce' ); ?></label>
<input name="sw_variation_color" id="sw_variation_color" type="text" value="" size="40" class="category-colorpicker"/>
</div>
<?php
}
function sw_woocommerce_edit_variation_fields( $term ) {
$sw_variation_color = get_term_meta( $term->term_id, 'sw_variation_color', true );
?>
<tr class="form-field custom-picker custom-picker-edit">
<th scope="row" valign="top"><label for="sw_variation_color"><?php _e( 'Color', 'sw_woocommerce' ); ?></label></th>
<td>
<input name="sw_variation_color" id="sw_variation_color" type="text" value="<?php echo esc_attr( $sw_variation_color ) ?>" size="40" class="category-colorpicker"/>
</td>
</tr>
<?php
}
/** Save Custom Field Of Category Form */
function sw_woocommerce_save_variation_fields( $term_id, $tt_id = '', $taxonomy = '', $prev_value = '' ) {
if ( isset( $_POST['sw_variation_color'] ) ) {
$term_value = esc_attr( $_POST['sw_variation_color'] );
update_term_meta( $term_id, 'sw_variation_color', $term_value, $prev_value );
}
}
function sw_woocommerce_variation_color_script(){
wp_enqueue_style( 'wp-color-picker' );
wp_enqueue_script('category_color_picker_js', WCURL . '/js/admin/category_color_picker.js', array( 'wp-color-picker' ), false, true);
}
As you can see on line 60 you will find the labels
Will there be any code to add a custom CSS to these labels that create the variables?
************** SOLUTION ***********
<?php
// Adiccional Class For Label Variations
function get_variation_stock_text( $product, $name, $term_slug ){
foreach ( $product->get_available_variations() as $variation ){
if($variation['attributes'][$name] == $term_slug ){
$is_in_stock = $variation['is_in_stock'];
$stock_qty = get_post_meta($variation['variation_id'], '_stock', true);
}
}
$in_stock = ' '.__("katayainstock", "woocommerce").'';
$out_of_stock = ' '.__("katayaoutstock", "woocommerce").'';
return $is_in_stock == 1 ? $in_stock : $out_of_stock;
}
/**
* Product variation hook
*/
add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'sw_woocommerce_custom_variation', 10, 2 );
function sw_woocommerce_custom_variation( $html, $args ) {
$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'];
if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {
$attributes = $product->get_variation_attributes();
$options = $attributes[ $attribute ];
}
$html = '';
if ( ! empty( $options ) ) {
$html .= '<div class="sw-custom-variation">';
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 $key => $term ) {
$color = get_term_meta( $term->term_id, 'sw_variation_color', true );
$active = checked( sanitize_title( $args['selected'] ), $term->slug, false ) ? ' selected' : '';
$attributes = ( preg_match( '/color|colors/', $attribute, $match ) && $color !== '' ) ? 'class="variation-color" style="background: ' . esc_attr( $color ) . '"' : '';
if ( in_array( $term->slug, $options, true ) ) {
$stock_text = get_variation_stock_text( $product, $name, $term->slug );
$html .= '<label class="' . $stock_text . ' radio-label sw-radio-variation sw-radio-variation-' . esc_attr( $key . ' ' . $active ) . '" title="' . esc_attr( $term->slug ) . '" for="' . esc_attr( $term->slug . '_' . $key ) . '">';
$html .= '<input type="radio" id="' . esc_attr( $term->slug . '_' . $key ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" value="' . esc_attr( $term->slug ) . '" ' . checked( sanitize_title( $args['selected'] ), $term->slug, false ) . '/>';
$html .= '<span ' . $attributes . '>' . $term->name . '</span>';
$html .= '</label>';
}
}
} else {
foreach ( $options as $key => $option ) {
// This handles < 2.4.0 bw compatibility where text attributes were not sanitized.
$checked = sanitize_title( $args['selected'] ) === $args['selected'] ? checked( $args['selected'], sanitize_title( $option ), false ) : checked( $args['selected'], $option, false );
$active = ( $checked ) ? 'selected' : '';
$html .= '<label class="radio-label sw-radio-variation sw-radio-variation-' . esc_attr( $key . ' ' . $active ) . '" title="' . esc_attr( $option ) . '" for="' . esc_attr( $option . '_' . $key ) . '">';
$html .= '<input type="radio" id="' . esc_attr( $option . '_' . $key ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" value="' . esc_attr( $option ) . '" ' . $checked . '/>';
$html .= '<span>' . $option . '</span>';
$html .= '</label>';
}
}
$html .= '</div>';
}
return $html;
}
/**
* Action hook color
*/
$sw_attribute_taxonomies = wc_get_attribute_taxonomies();
if ( ! empty( $sw_attribute_taxonomies ) ) {
foreach ( $sw_attribute_taxonomies as $attr ) {
if ( preg_match( '/color|colors/', $attr->attribute_name, $match ) ) {
add_action( 'pa_' . $attr->attribute_name . '_add_form_fields', 'sw_woocommerce_variation_fields', 200 );
add_action( 'pa_' . $attr->attribute_name . '_edit_form_fields', 'sw_woocommerce_edit_variation_fields', 200 );
add_action( 'created_term', 'sw_woocommerce_save_variation_fields', 10, 3 );
add_action( 'edit_terms', 'sw_woocommerce_save_variation_fields', 10, 3 );
add_action( 'admin_enqueue_scripts', 'sw_woocommerce_variation_color_script' );
}
}
}
/**
* Create color
*/
function sw_woocommerce_variation_fields() { ?>
<div class="form-field custom-picker">
<label for="sw_variation_color"><?php _e( 'Color', 'sw_woocommerce' ); ?></label>
<input name="sw_variation_color" id="sw_variation_color" type="text" value="" size="40"
class="category-colorpicker"/>
</div>
<?php
}
/**
* #param $term
*/
function sw_woocommerce_edit_variation_fields( $term ) {
$sw_variation_color = get_term_meta( $term->term_id, 'sw_variation_color', true ); ?>
<tr class="form-field custom-picker custom-picker-edit">
<th scope="row" valign="top"><label
for="sw_variation_color"><?php _e( 'Color', 'sw_woocommerce' ); ?></label></th>
<td>
<input name="sw_variation_color" id="sw_variation_color" type="text"
value="<?php echo esc_attr( $sw_variation_color ) ?>" size="40" class="category-colorpicker"/>
</td>
</tr>
<?php
}
/**
* Save custom field of category form
*
* #param $term_id
* #param string $tt_id
* #param string $taxonomy
* #param string $prev_value
*/
function sw_woocommerce_save_variation_fields( $term_id, $tt_id = '', $taxonomy = '', $prev_value = '' ) {
if ( isset( $_POST['sw_variation_color'] ) ) {
$term_value = esc_attr( $_POST['sw_variation_color'] );
update_term_meta( $term_id, 'sw_variation_color', $term_value, $prev_value );
}
}
function sw_woocommerce_variation_color_script() {
wp_enqueue_style( 'wp-color-picker' );
wp_enqueue_script( 'category_color_picker_js', WCURL . '/js/admin/category_color_picker.js', array( 'wp-color-picker' ), false, true );
}
Here try this. And one tip, improve your code formatting because one day you need to edit this again.
<?php
/**
* Product variation hook
*/
add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'sw_woocommerce_custom_variation', 10, 2 );
function sw_woocommerce_custom_variation( $html, $args ) {
$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'];
if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {
$attributes = $product->get_variation_attributes();
$options = $attributes[ $attribute ];
}
$html = '';
if ( ! empty( $options ) ) {
$html .= '<div class="sw-custom-variation">';
if ( $product && taxonomy_exists( $attribute ) ) {
//Get product stock
$stock = $product->get_stock_quantity();
// 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 $key => $term ) {
$color = get_term_meta( $term->term_id, 'sw_variation_color', true );
$active = checked( sanitize_title( $args['selected'] ), $term->slug, false ) ? ' selected' : '';
$attributes = ( preg_match( '/color|colors/', $attribute, $match ) && $color !== '' ) ? 'class="variation-color" style="background: ' . esc_attr( $color ) . '"' : '';
if ( in_array( $term->slug, $options, true ) ) {
$additional_class = '';
if ( ! empty( $stock ) && $stock < 1 ) {
$additional_class = 'out-stock';
}
$html .= '<label class="' . $additional_class . ' radio-label sw-radio-variation sw-radio-variation-' . esc_attr( $key . ' ' . $active ) . '" title="' . esc_attr( $term->slug ) . '" for="' . esc_attr( $term->slug . '_' . $key ) . '">';
$html .= '<input type="radio" id="' . esc_attr( $term->slug . '_' . $key ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" value="' . esc_attr( $term->slug ) . '" ' . checked( sanitize_title( $args['selected'] ), $term->slug, false ) . '/>';
$html .= '<span ' . $attributes . '>' . $term->name . '</span>';
$html .= '</label>';
}
}
} else {
foreach ( $options as $key => $option ) {
// This handles < 2.4.0 bw compatibility where text attributes were not sanitized.
$checked = sanitize_title( $args['selected'] ) === $args['selected'] ? checked( $args['selected'], sanitize_title( $option ), false ) : checked( $args['selected'], $option, false );
$active = ( $checked ) ? 'selected' : '';
$html .= '<label class="radio-label sw-radio-variation sw-radio-variation-' . esc_attr( $key . ' ' . $active ) . '" title="' . esc_attr( $option ) . '" for="' . esc_attr( $option . '_' . $key ) . '">';
$html .= '<input type="radio" id="' . esc_attr( $option . '_' . $key ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" value="' . esc_attr( $option ) . '" ' . $checked . '/>';
$html .= '<span>' . $option . '</span>';
$html .= '</label>';
}
}
$html .= '</div>';
}
return $html;
}
/**
* Action hook color
*/
$sw_attribute_taxonomies = wc_get_attribute_taxonomies();
if ( ! empty( $sw_attribute_taxonomies ) ) {
foreach ( $sw_attribute_taxonomies as $attr ) {
if ( preg_match( '/color|colors/', $attr->attribute_name, $match ) ) {
add_action( 'pa_' . $attr->attribute_name . '_add_form_fields', 'sw_woocommerce_variation_fields', 200 );
add_action( 'pa_' . $attr->attribute_name . '_edit_form_fields', 'sw_woocommerce_edit_variation_fields', 200 );
add_action( 'created_term', 'sw_woocommerce_save_variation_fields', 10, 3 );
add_action( 'edit_terms', 'sw_woocommerce_save_variation_fields', 10, 3 );
add_action( 'admin_enqueue_scripts', 'sw_woocommerce_variation_color_script' );
}
}
}
/**
* Create color
*/
function sw_woocommerce_variation_fields() { ?>
<div class="form-field custom-picker">
<label for="sw_variation_color"><?php _e( 'Color', 'sw_woocommerce' ); ?></label>
<input name="sw_variation_color" id="sw_variation_color" type="text" value="" size="40"
class="category-colorpicker"/>
</div>
<?php
}
/**
* #param $term
*/
function sw_woocommerce_edit_variation_fields( $term ) {
$sw_variation_color = get_term_meta( $term->term_id, 'sw_variation_color', true ); ?>
<tr class="form-field custom-picker custom-picker-edit">
<th scope="row" valign="top"><label
for="sw_variation_color"><?php _e( 'Color', 'sw_woocommerce' ); ?></label></th>
<td>
<input name="sw_variation_color" id="sw_variation_color" type="text"
value="<?php echo esc_attr( $sw_variation_color ) ?>" size="40" class="category-colorpicker"/>
</td>
</tr>
<?php
}
/**
* Save custom field of category form
*
* #param $term_id
* #param string $tt_id
* #param string $taxonomy
* #param string $prev_value
*/
function sw_woocommerce_save_variation_fields( $term_id, $tt_id = '', $taxonomy = '', $prev_value = '' ) {
if ( isset( $_POST['sw_variation_color'] ) ) {
$term_value = esc_attr( $_POST['sw_variation_color'] );
update_term_meta( $term_id, 'sw_variation_color', $term_value, $prev_value );
}
}
function sw_woocommerce_variation_color_script() {
wp_enqueue_style( 'wp-color-picker' );
wp_enqueue_script( 'category_color_picker_js', WCURL . '/js/admin/category_color_picker.js', array( 'wp-color-picker' ), false, true );
}
I followed the steps in this article: cssigniter.com/add-rating-wordpress-comment-system to add a star rating to the comments system... but when I list the comments with the code below the stars are not showing up I have tried what seems like a million things and I can not seem to figure out why the stars are not showing up.
Here is the code I am using to pull the comments
if ( is_user_logged_in() ) {
$user_id = get_current_user_id(); $args = array( 'status' => 'approve', 'user_id' => $user_id );
$comments = get_comments($args);
foreach($comments as $comment) : echo '<p>';
$post_id = $comment->comment_post_ID;
$member_name = get_post( $comment->comment_post_ID );
echo ( ' <div style="color: #00205a;"> ' . mysql2date(get_option('date_format'), $comment->comment_date) . ' - </div>' . '<a style="color:#a27747;" href="' . get_permalink( $comment->comment_post_ID ) . '">' . $member_name->post_title . '</a><br />' . '(stars go here)' . '<br />' . $comment->comment_content ). '<br /><br />';
echo '</p>';
endforeach;
}
if ( is_user_logged_in() ) {
$user_id = get_current_user_id(); $args = array( 'status' => 'approve', 'user_id' => $user_id );
$comments = get_comments($args);
foreach($comments as $comment) : echo '<p>';
$member_name = get_post( $comment->comment_post_ID );
if ( $rating = get_comment_meta( $comment->comment_ID, 'rating', true ) ) {
$stars = '<p class="stars">';
for ( $i = 1; $i <= $rating; $i++ ) {
$stars .= '<span class="dashicons dashicons-star-filled"></span>';
}
$stars .= '</p>';
}
echo ( ' <div style="color: #00205a;"> ' . mysql2date(get_option('date_format'), $comment->comment_date) . ' - </div>' . '<a style="color:#a27747;" href="' . get_permalink( $comment->comment_post_ID ) . '">' . $member_name->post_title . '</a><br />'. $stars . '<br />' . $comment->comment_content ). '<br /><br />';
echo '</p>';
endforeach;
}
I would like to be able to add a custom CSS class to my WooCommerce checkout fields. I'm using twitter Bootstrap and I would like to be able to use their .form-control class.
I looked in the woocommerce templates folder in form-billing.php but I'm not sure where to add the .form-control class to each text field.
Here is the code for form-billing.php
<?php
/**
* Checkout billing information form
*
* #author WooThemes
* #package WooCommerce/Templates
* #version 2.1.2
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
?>
<div class="woocommerce-billing-fields">
<?php if ( WC()->cart->ship_to_billing_address_only() && WC()->cart->needs_shipping() ) : ?>
<h3><?php _e( 'Billing & Shipping', 'woocommerce' ); ?></h3>
<?php else : ?>
<h3><?php _e( 'Billing Details', 'woocommerce' ); ?></h3>
<?php endif; ?>
<?php do_action( 'woocommerce_before_checkout_billing_form', $checkout ); ?>
<?php foreach ( $checkout->checkout_fields['billing'] as $key => $field ) : ?>
<?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>
<?php endforeach; ?>
<?php do_action('woocommerce_after_checkout_billing_form', $checkout ); ?>
<?php if ( ! is_user_logged_in() && $checkout->enable_signup ) : ?>
<?php if ( $checkout->enable_guest_checkout ) : ?>
<p class="form-row form-row-wide create-account">
<input class="input-checkbox" id="createaccount" <?php checked( ( true === $checkout->get_value( 'createaccount' ) || ( true === apply_filters( 'woocommerce_create_account_default_checked', false ) ) ), true) ?> type="checkbox" name="createaccount" value="1" /> <label for="createaccount" class="checkbox"><?php _e( 'Create an account?', 'woocommerce' ); ?></label>
</p>
<?php endif; ?>
<?php do_action( 'woocommerce_before_checkout_registration_form', $checkout ); ?>
<?php if ( ! empty( $checkout->checkout_fields['account'] ) ) : ?>
<div class="create-account">
<p><?php _e( 'Create an account by entering the information below. If you are a returning customer please login at the top of the page.', 'woocommerce' ); ?></p>
<?php foreach ( $checkout->checkout_fields['account'] as $key => $field ) : ?>
<?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>
<?php endforeach; ?>
<div class="clear"></div>
</div>
<?php endif; ?>
<?php do_action( 'woocommerce_after_checkout_registration_form', $checkout ); ?>
<?php endif; ?>
</div>
Do I need to to look in another template file?
Thanks
icc97's answer is almost there but doesn't work.
I took icc97's answer, and debugged it:
add_filter('woocommerce_checkout_fields', 'addBootstrapToCheckoutFields' );
public function addBootstrapToCheckoutFields($fields) {
foreach ($fields as &$fieldset) {
foreach ($fieldset as &$field) {
// if you want to add the form-group class around the label and the input
$field['class'][] = 'form-group';
// add form-control to the actual input
$field['input_class'][] = 'form-control';
}
}
return $fields;
}
As #Peanuts pointed out, what if we want to add CSS classes to certain input types only?
After experimenting with the solutions posted here so far, (thanks to everybody!), I've came up with a mod using a simple "switch case", in which the logic is taken from /woocommerce/includes/wc-template-functions.php. The function allow us to target all the input types at once, be it the default input types or even custom ones.
There's no need to rewrite the woocommerce_form_field function to simply change the $defaults args for the input types. It is valid to mention that the function also allow us to add much more than just css classes to the fields.
-- So here's the function --
/*********************************************************************************************/
/** WooCommerce - Modify each individual input type $args defaults /**
/*********************************************************************************************/
add_filter('woocommerce_form_field_args','wc_form_field_args',10,3);
function wc_form_field_args( $args, $key, $value = null ) {
/*********************************************************************************************/
/** This is not meant to be here, but it serves as a reference
/** of what is possible to be changed. /**
$defaults = array(
'type' => 'text',
'label' => '',
'description' => '',
'placeholder' => '',
'maxlength' => false,
'required' => false,
'id' => $key,
'class' => array(),
'label_class' => array(),
'input_class' => array(),
'return' => false,
'options' => array(),
'custom_attributes' => array(),
'validate' => array(),
'default' => '',
);
/*********************************************************************************************/
// Start field type switch case
switch ( $args['type'] ) {
case "select" : /* Targets all select input type elements, except the country and state select input types */
$args['class'][] = 'form-group'; // Add a class to the field's html element wrapper - woocommerce input types (fields) are often wrapped within a <p></p> tag
$args['input_class'] = array('form-control', 'input-lg'); // Add a class to the form input itself
//$args['custom_attributes']['data-plugin'] = 'select2';
$args['label_class'] = array('control-label');
$args['custom_attributes'] = array( 'data-plugin' => 'select2', 'data-allow-clear' => 'true', 'aria-hidden' => 'true', ); // Add custom data attributes to the form input itself
break;
case 'country' : /* By default WooCommerce will populate a select with the country names - $args defined for this specific input type targets only the country select element */
$args['class'][] = 'form-group single-country';
$args['label_class'] = array('control-label');
break;
case "state" : /* By default WooCommerce will populate a select with state names - $args defined for this specific input type targets only the country select element */
$args['class'][] = 'form-group'; // Add class to the field's html element wrapper
$args['input_class'] = array('form-control', 'input-lg'); // add class to the form input itself
//$args['custom_attributes']['data-plugin'] = 'select2';
$args['label_class'] = array('control-label');
$args['custom_attributes'] = array( 'data-plugin' => 'select2', 'data-allow-clear' => 'true', 'aria-hidden' => 'true', );
break;
case "password" :
case "text" :
case "email" :
case "tel" :
case "number" :
$args['class'][] = 'form-group';
//$args['input_class'][] = 'form-control input-lg'; // will return an array of classes, the same as bellow
$args['input_class'] = array('form-control', 'input-lg');
$args['label_class'] = array('control-label');
break;
case 'textarea' :
$args['input_class'] = array('form-control', 'input-lg');
$args['label_class'] = array('control-label');
break;
case 'checkbox' :
break;
case 'radio' :
break;
default :
$args['class'][] = 'form-group';
$args['input_class'] = array('form-control', 'input-lg');
$args['label_class'] = array('control-label');
break;
}
return $args;
}
The function above completely solved the issue of targeting the checkout form inputs all at once, which is really straight forward for the checkout form default input types or even custom new ones. It seems not to be possible though, to print each input type html output without creating a new function as #abhisek shows on his answer.
Bonus
It seems that the function might also affect other forms fields printed by WooCommerce's functions or templates outside the checkout page.
I've managed to conditionally apply the function only when on the checkout page by using the is_page() function. Your checkout page might have a different slug, so change that to reflect it accordingly.
If you need to apply the function only for the checkout page, do as follows:
Comment add_filter()
//add_filter('woocommerce_form_field_args','wc_form_field_args', 10, 3);
And use add_action instead
add_action('woocommerce_form_field_args', 'wc_form_field_args', 10, 3);
function wc_form_field_args( $args, $key, $value = null ) {
...
// Right after
return $args;
// Place the following
if ( !is_page('checkout') ) {
add_filter('woocommerce_form_field_args','wc_form_field_args', 10, 3);
} else {
remove_filter('woocommerce_form_field_args','wc_form_field_args', 10, 3);
}
}
After that, the function will only affect the checkout page form.
#Chetan's link and run answer does kind of give you what you want, but as ever they're never very good answers.
The best resource for this is the WooCommerce Codex page on Customizing checkout fields using actions and filters.
In the 'Customizing WooCommerce Checkout Field Labels and Placeholder Text' in Chetan's page you have the following code which you need to add to your functions.php, I've modified it in such a way that it should do what you want, but I haven't tested the code:
// Hook in
add_filter( 'woocommerce_checkout_fields' , 'my_theme_custom_override_checkout_fields' );
// Our hooked in function - $fields is passed via the filter!
function my_theme_custom_override_checkout_fields( $fields ) {
foreach ($fields as $fieldset) {
foreach ($fieldset as $field) {
$field['class'] = array('form-control');
}
}
return $fields;
}
I am fixed the issue by defining a custom function. The logic is taken directly from wc-template-functions.php (I am not sure if this is the right way but it does solve the problem).
/*
* Custom form field function for Bootstrap 3
*/
function bootstrap_woocommerce_form_field( $key, $args, $value = null ) {
$defaults = array(
'type' => 'text',
'label' => '',
'placeholder' => '',
'maxlength' => false,
'required' => false,
'class' => array(),
'label_class' => array(),
'input_class' => array(),
'return' => false,
'options' => array(),
'custom_attributes' => array(),
'validate' => array(),
'default' => '',
);
$args = wp_parse_args( $args, $defaults );
if ( ( ! empty( $args['clear'] ) ) ) $after = '<div class="clear"></div>'; else $after = '';
if ( $args['required'] ) {
$args['class'][] = 'validate-required';
$required = ' <abbr class="required" title="' . esc_attr__( 'required', 'woocommerce' ) . '">*</abbr>';
} else {
$required = '';
}
$args['maxlength'] = ( $args['maxlength'] ) ? 'maxlength="' . absint( $args['maxlength'] ) . '"' : '';
if ( is_string( $args['label_class'] ) )
$args['label_class'] = array( $args['label_class'] );
if ( is_null( $value ) )
$value = $args['default'];
// Custom attribute handling
$custom_attributes = array();
if ( ! empty( $args['custom_attributes'] ) && is_array( $args['custom_attributes'] ) )
foreach ( $args['custom_attributes'] as $attribute => $attribute_value )
$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"';
if ( ! empty( $args['validate'] ) )
foreach( $args['validate'] as $validate )
$args['class'][] = 'validate-' . $validate;
switch ( $args['type'] ) {
case "country" :
$countries = $key == 'shipping_country' ? WC()->countries->get_shipping_countries() : WC()->countries->get_allowed_countries();
if ( sizeof( $countries ) == 1 ) {
$field = '<div class="form-group form-row ' . esc_attr( implode( ' ', $args['class'] ) ) .'" id="' . esc_attr( $key ) . '_field">';
if ( $args['label'] )
$field .= '<label class="' . implode( ' ', $args['label_class'] ) .'">' . $args['label'] . '</label>';
$field .= '<strong>' . current( array_values( $countries ) ) . '</strong>';
$field .= '<input type="hidden" name="' . esc_attr( $key ) . '" id="' . esc_attr( $key ) . '" value="' . current( array_keys($countries ) ) . '" ' . implode( ' ', $custom_attributes ) . ' class="country_to_state" />';
$field .= '</div>' . $after;
} else {
$field = '<div class="form-group form-row ' . esc_attr( implode( ' ', $args['class'] ) ) .'" id="' . esc_attr( $key ) . '_field">'
. '<label for="' . esc_attr( $key ) . '" class="' . implode( ' ', $args['label_class'] ) .'">' . $args['label'] . $required . '</label>'
. '<select name="' . esc_attr( $key ) . '" id="' . esc_attr( $key ) . '" class="country_to_state form-control country_select" ' . implode( ' ', $custom_attributes ) . '>'
. '<option value="">'.__( 'Select a country…', 'woocommerce' ) .'</option>';
foreach ( $countries as $ckey => $cvalue )
$field .= '<option value="' . esc_attr( $ckey ) . '" '.selected( $value, $ckey, false ) .'>'.__( $cvalue, 'woocommerce' ) .'</option>';
$field .= '</select>';
$field .= '<noscript><input type="submit" name="woocommerce_checkout_update_totals" value="' . __( 'Update country', 'woocommerce' ) . '" /></noscript>';
$field .= '</div>' . $after;
}
break;
case "state" :
/* Get Country */
$country_key = $key == 'billing_state'? 'billing_country' : 'shipping_country';
if ( isset( $_POST[ $country_key ] ) ) {
$current_cc = wc_clean( $_POST[ $country_key ] );
} elseif ( is_user_logged_in() ) {
$current_cc = get_user_meta( get_current_user_id() , $country_key, true );
if ( ! $current_cc) {
$current_cc = apply_filters('default_checkout_country', (WC()->customer->get_country()) ? WC()->customer->get_country() : WC()->countries->get_base_country());
}
} elseif ( $country_key == 'billing_country' ) {
$current_cc = apply_filters('default_checkout_country', (WC()->customer->get_country()) ? WC()->customer->get_country() : WC()->countries->get_base_country());
} else {
$current_cc = apply_filters('default_checkout_country', (WC()->customer->get_shipping_country()) ? WC()->customer->get_shipping_country() : WC()->countries->get_base_country());
}
$states = WC()->countries->get_states( $current_cc );
if ( is_array( $states ) && empty( $states ) ) {
$field = '<div class="form-group form-row ' . esc_attr( implode( ' ', $args['class'] ) ) .'" id="' . esc_attr( $key ) . '_field" style="display: none">';
if ( $args['label'] )
$field .= '<label for="' . esc_attr( $key ) . '" class="' . implode( ' ', $args['label_class'] ) .'">' . $args['label'] . $required . '</label>';
$field .= '<input type="hidden" class="hidden" name="' . esc_attr( $key ) . '" id="' . esc_attr( $key ) . '" value="" ' . implode( ' ', $custom_attributes ) . ' placeholder="' . esc_attr( $args['placeholder'] ) . '" />';
$field .= '</div>' . $after;
} elseif ( is_array( $states ) ) {
$field = '<div class="form-group form-row ' . esc_attr( implode( ' ', $args['class'] ) ) .'" id="' . esc_attr( $key ) . '_field">';
if ( $args['label'] )
$field .= '<label for="' . esc_attr( $key ) . '" class="' . implode( ' ', $args['label_class'] ) .'">' . $args['label']. $required . '</label>';
$field .= '<select name="' . esc_attr( $key ) . '" id="' . esc_attr( $key ) . '" class="state_select form-control " ' . implode( ' ', $custom_attributes ) . ' placeholder="' . esc_attr( $args['placeholder'] ) . '">
<option value="">'.__( 'Select a state…', 'woocommerce' ) .'</option>';
foreach ( $states as $ckey => $cvalue )
$field .= '<option value="' . esc_attr( $ckey ) . '" '.selected( $value, $ckey, false ) .'>'.__( $cvalue, 'woocommerce' ) .'</option>';
$field .= '</select>';
$field .= '</div>' . $after;
} else {
$field = '<div class="form-group form-row ' . esc_attr( implode( ' ', $args['class'] ) ) .'" id="' . esc_attr( $key ) . '_field">';
if ( $args['label'] )
$field .= '<label for="' . esc_attr( $key ) . '" class="' . implode( ' ', $args['label_class'] ) .'">' . $args['label']. $required . '</label>';
$field .= '<input type="text" class="form-control input-text ' . implode( ' ', $args['input_class'] ) .'" value="' . esc_attr( $value ) . '" placeholder="' . esc_attr( $args['placeholder'] ) . '" name="' . esc_attr( $key ) . '" id="' . esc_attr( $key ) . '" ' . implode( ' ', $custom_attributes ) . ' />';
$field .= '</div>' . $after;
}
break;
case "textarea" :
$field = '<div class="form-group form-row ' . esc_attr( implode( ' ', $args['class'] ) ) .'" id="' . esc_attr( $key ) . '_field">';
if ( $args['label'] )
$field .= '<label for="' . esc_attr( $key ) . '" class="' . implode( ' ', $args['label_class'] ) .'">' . $args['label']. $required . '</label>';
$field .= '<textarea name="' . esc_attr( $key ) . '" class="form-control input-text ' . implode( ' ', $args['input_class'] ) .'" id="' . esc_attr( $key ) . '" placeholder="' . esc_attr( $args['placeholder'] ) . '"' . ( empty( $args['custom_attributes']['rows'] ) ? ' rows="2"' : '' ) . ( empty( $args['custom_attributes']['cols'] ) ? ' cols="5"' : '' ) . implode( ' ', $custom_attributes ) . '>'. esc_textarea( $value ) .'</textarea>
</div>' . $after;
break;
case "checkbox" :
$field = '<div class="checkbox form-row ' . esc_attr( implode( ' ', $args['class'] ) ) .'" id="' . esc_attr( $key ) . '_field">
<label for="' . esc_attr( $key ) . '" class="checkbox ' . implode( ' ', $args['label_class'] ) .'" ' . implode( ' ', $custom_attributes ) . '>
<input type="' . esc_attr( $args['type'] ) . '" class="input-checkbox" name="' . esc_attr( $key ) . '" id="' . esc_attr( $key ) . '" value="1" '.checked( $value, 1, false ) .' />'
. $args['label'] . $required . '</label>
</div>' . $after;
break;
case "password" :
$field = '<div class="form-group form-row ' . esc_attr( implode( ' ', $args['class'] ) ) .'" id="' . esc_attr( $key ) . '_field">';
if ( $args['label'] )
$field .= '<label for="' . esc_attr( $key ) . '" class="' . implode( ' ', $args['label_class'] ) .'">' . $args['label']. $required . '</label>';
$field .= '<input type="password" class="form-control input-text ' . implode( ' ', $args['input_class'] ) .'" name="' . esc_attr( $key ) . '" id="' . esc_attr( $key ) . '" placeholder="' . esc_attr( $args['placeholder'] ) . '" value="' . esc_attr( $value ) . '" ' . implode( ' ', $custom_attributes ) . ' />
</div>' . $after;
break;
case "text" :
$field = '<div class="form-group form-row ' . esc_attr( implode( ' ', $args['class'] ) ) .'" id="' . esc_attr( $key ) . '_field">';
if ( $args['label'] )
$field .= '<label for="' . esc_attr( $key ) . '" class="' . implode( ' ', $args['label_class'] ) .'">' . $args['label'] . $required . '</label>';
$field .= '<input type="text" class="form-control input-text ' . implode( ' ', $args['input_class'] ) .'" name="' . esc_attr( $key ) . '" id="' . esc_attr( $key ) . '" placeholder="' . esc_attr( $args['placeholder'] ) . '" '.$args['maxlength'].' value="' . esc_attr( $value ) . '" ' . implode( ' ', $custom_attributes ) . ' />
</div>' . $after;
break;
case "select" :
$options = '';
if ( ! empty( $args['options'] ) )
foreach ( $args['options'] as $option_key => $option_text )
$options .= '<option value="' . esc_attr( $option_key ) . '" '. selected( $value, $option_key, false ) . '>' . esc_attr( $option_text ) .'</option>';
$field = '<div class="form-group form-row ' . esc_attr( implode( ' ', $args['class'] ) ) .'" id="' . esc_attr( $key ) . '_field">';
if ( $args['label'] )
$field .= '<label for="' . esc_attr( $key ) . '" class="' . implode( ' ', $args['label_class'] ) .'">' . $args['label']. $required . '</label>';
$field .= '<select name="' . esc_attr( $key ) . '" id="' . esc_attr( $key ) . '" class="select form-control" ' . implode( ' ', $custom_attributes ) . '>
' . $options . '
</select>
</div>' . $after;
break;
default :
$field = apply_filters( 'woocommerce_form_field_' . $args['type'], '', $key, $args, $value );
break;
}
if ( $args['return'] ) return $field; else echo $field;
}
Don't forget to replace all occurrences of woocommerce_form_field with bootstrap_woocommerce_form_field Hope it helps!
I need a code to get the Parent Menu Nav Item (Not Parent Page Nav Item) from a special Menu Item in Wordpress.
Example: get_parent_menu_nav_item($item->ID)
I spent a lot of time in google by this problem, but no solution.
My existing Code for my Menu (for example):
<?php
class MV_Cleaner_Walker_Nav_Menu extends Walker {
var $tree_type = array( 'post_type', 'taxonomy', 'custom' );
var $db_fields = array( 'parent' => 'menu_item_parent', 'id' => 'db_id' );
function start_lvl(&$output, $depth) {
$indent = str_repeat("\t", $depth);
$output .= "\n$indent<ul class=\"sub-menu\">\n";
}
function end_lvl(&$output, $depth) {
$indent = str_repeat("\t", $depth);
$output .= "$indent</ul>\n";
}
function start_el(&$output, $item, $depth, $args) {
global $wp_query;
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
$class_names = $value = '';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$classes = in_array( 'current-menu-item', $classes ) ? array( 'current-menu-item' ) : array();
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
$class_names = strlen( trim( $class_names ) ) > 0 ? ' class="' . esc_attr( $class_names ) . '"' : '';
$id = apply_filters( 'nav_menu_item_id', '', $item, $args );
$id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';
$output .= $indent . '<li' . $value . $class_names .' id="NEED PARENT ID">';
$attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
$attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
$attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
$attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';
$item_output = $args->before;
$item_output .= '<a'. $attributes .'>';
$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
$item_output .= '</a>';
$item_output .= $args->after;
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
function end_el(&$output, $item, $depth) {
$output .= "</li>\n";
}
}
?>
I hope you can help me, thanks a lot for answers.
This is an old thread, but today I have a requeriment to show parent menu title and I solved it using some code from core, just load the metadata using $item->menu_item_parent and then use the right function to get the parent object
$object_id = get_post_meta( $item->menu_item_parent, '_menu_item_object_id', true );
$object = get_post_meta( $item->menu_item_parent, '_menu_item_object', true );
$type = get_post_meta( $item->menu_item_parent, '_menu_item_type', true );
if ( 'post_type' == $type ) {
$title = get_post( $object_id )->post_title;
} elseif ( 'taxonomy' == $type) {
$title = get_term( $object_id, $object )->name;
}