I am trying to get the second value from the array and label it $icon however I am still learning PHP. No matter what I try nothing has helped. This is for a WooCommerce website running a custom theme. The below code has been placed in the functions.php file.
$catalog_orderby = apply_filters('woocommerce_catalog_orderby', array(
'menu_order' => __( 'Default', '', 'woocommerce' ),
'date' => __( 'Whats New', '', 'woocommerce' ),
'popularity' => __( 'Popularity', '', 'woocommerce' ),
'rating' => __( 'Average Rating', '', 'woocommerce' ),
'price' => __( 'Price: Low to High', '', 'woocommerce' ),
'price-desc' => __( 'Price: High to Low', '', 'woocommerce' )
));
foreach( $catalog_orderby as $id => $name ) {
echo '<li><i class="fa">' . $icon . '</i>' . esc_attr( $name ) . '</li>';
};
Any help with this would be greatly appreciated!
The __() function seems to be for fetching a string translation, so you'd need to pass the icon string as a separate variable. You can do that using a multidimensional array.
$catalog_orderby = apply_filters('woocommerce_catalog_orderby', array(
'menu_order' => array(__( 'Default', 'woocommerce' ), ''),
'date' => array(__( 'Whats New', 'woocommerce' ), ''),
'popularity' => array(__( 'Popularity', 'woocommerce' ), ''),
'rating' => array(__( 'Average Rating', 'woocommerce' ), ''),
'price' => array(__( 'Price: Low to High', 'woocommerce' ), ''),
'price-desc' => array(__( 'Price: High to Low', 'woocommerce' ), '')
));
foreach( $catalog_orderby as $id => $item ) {
echo '<li><i class="fa">' . $item[1] . '</i>' . esc_attr( $item[0] ) . '</li>';
};
Why are you using the _() function, it appears to be a translation function?
If you change the __('Default', '', 'woocommerce') to array('Default', '', 'woocommerce') you can access it in the loop by doing $name[1]
Related
I face a problem regarding woocommerce_wp_select . I am adding new fields to the single product page. First, I add the options by the following codes:
$title_110 = array(
'id' => 'custom_text_field_title_110',
'label' => __( 'Awards', 'rasa_store' ),
'desc_tip' => true,
'description' => __( 'Select an option.', 'ctwc' ),
'options' => array(
'' => __( 'Select Option', 'woocommerce' ),
'0' => __('This product does not win any awards', 'woocommerce' ),
'1' => __('This product win on award.', 'woocommerce' ),
'2' => __('This product win 2 award.', 'woocommerce' ),
'3' => __('This product win 3 award.', 'woocommerce' ),
'4' => __('This product very famous.', 'woocommerce' )
),
);
woocommerce_wp_select( $title_110 );
Than I save it.
$attribute_110 = wc_get_product( $post_id );
$title_top_110 = isset( $_POST['custom_text_field_title_110'] ) ? $_POST['custom_text_field_title_110'] : '';
$attribute_110->update_meta_data( 'custom_text_field_title_110', sanitize_text_field( $title_top_110 ) );
$attribute_110->save();
But in front page of single product page, while I use :
$attribute_11 = wc_get_product ( $post->ID );
$title_top_110 = $attribute_11->get_meta( 'custom_text_field_title_110' );
if( $title_top_110 ) {
printf(
'<div class="row">
<div class="col-md-4">
<img class="img-fluid box-10-2" src="%s/img/award-icon.png">
</div>
<div class="col-md-8 box-10">
<p class="card-text box-10-1">%s</p>
</div>
</div>
',
esc_html( get_bloginfo('template_directory') ),
esc_html( $title_top_110 )
);
}
Instead of printing This product does not win any awards I see 0.
I am looking for finding a way to fix it. I test the following methods, and they do not work:
1. Replaced update_post_meta() by get_post_meta()
2. Replaced esc_html to esc_sql
There are some different ways:
You need an additional custom function for your dropdown options this way:
function custom_field_options_title_110() {
return array(
'' => __( 'Select Option', 'woocommerce' ),
'0' => __('This product does not win any awards', 'woocommerce' ),
'1' => __('This product win on award.', 'woocommerce' ),
'2' => __('This product win 2 award.', 'woocommerce' ),
'3' => __('This product win 3 award.', 'woocommerce' ),
'4' => __('This product very famous.', 'woocommerce' )
);
}
Then you will call that function everywhere is needed:
In backend on your woocommerce_wp_select() code:
woocommerce_wp_select( array(
'id' => 'custom_text_field_title_110',
'label' => __( 'Awards', 'rasa_store' ),
'desc_tip' => true,
'description' => __( 'Select an option.', 'ctwc' ),
'options' => custom_field_options_title_110(), // <== Here we call our options function
) );
And now on frontend for single product page:
$attribute_11 = wc_get_product ( $post->ID );
$title_top_110 = $attribute_11->get_meta( 'custom_text_field_title_110' );
if( ! empty($title_top_110) ) {
printf( '<div class="row"><div class="col-md-4"><img class="img-fluid box-10-2" src="%s"></div>
<div class="col-md-8 box-10"><p class="card-text box-10-1">%s</p></div></div>',
esc_html( get_bloginfo('template_directory') . '/img/award-icon.png' ),
esc_html( custom_field_options_title_110()[$title_top_110] ) // <== HERE we use it
);
}
It should work…
Another alternative is to have the same keys and values in your 'options' array like:
$options_title_110 = array( '' => __( 'Select Option', 'woocommerce' ) );
foreach ( array(
__('This product does not win any awards', 'woocommerce' ),
__('This product win on award.', 'woocommerce' ),
__('This product win 2 award.', 'woocommerce' ),
__('This product win 3 award.', 'woocommerce' ),
__('This product very famous.', 'woocommerce' )
) as $label ) {
$options_title_110[$label] = $label;
}
woocommerce_wp_select( array(
'id' => 'custom_text_field_title_110',
'label' => __( 'Awards', 'rasa_store' ),
'desc_tip' => true,
'description' => __( 'Select an option.', 'ctwc' ),
'options' => $options_title_110,
) );
Then the custom field selected value will be saved on backend and displayed on front end.
You can set the options as you where doing, so this part should work as intended
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_text_field_title_110' );
function woo_add_custom_text_field_title_110(){
$title_110 = array(
'id' => 'custom_text_field_title_110',
'label' => __( 'Awards', 'rasa_store' ),
'desc_tip' => true,
'description' => __( 'Select an option.', 'ctwc' ),
'options' => array(
'' => __( 'Select Option', 'woocommerce' ),
'0' => __('This product does not win any awards', 'woocommerce' ),
'1' => __('This product win on award.', 'woocommerce' ),
'2' => __('This product win 2 award.', 'woocommerce' ),
'3' => __('This product win 3 award.', 'woocommerce' ),
'4' => __('This product very famous.', 'woocommerce' )
),
);
woocommerce_wp_select( $title_110 );
}
To save the fields
// Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_text_field_title_110_save' );
function woo_add_custom_text_field_title_110_save( $post_id ){
// Select
$title_top_110 = $_POST['custom_text_field_title_110'];
if( !empty( $title_top_110 ) )
update_post_meta( $post_id, 'custom_text_field_title_110', esc_attr( $title_top_110 ) );
else {
update_post_meta( $post_id, 'custom_text_field_title_110', '' );
}
}
And to output the data on single product page (this code needs to be modified to output it inside the loop)
$title_top_110 = get_post_meta( 'custom_text_field_title_110', $post->ID, true );
if( $title_top_110 ) {
printf(
'<div class="row">
<div class="col-md-4">
<img class="img-fluid box-10-2" src="%1$s/img/award-icon.png">
</div>
<div class="col-md-8 box-10">
<p class="card-text box-10-1">%2$s</p>
</div>
</div>
',
esc_html( get_bloginfo('template_directory') ),
esc_html( $title_top_110 )
);
}
I am modifying a wordpress theme 'jobify' . In their demo they have a 'search hero' widget with a button which takes you to a jobs listings page. Inspecting in chrome:
<form class="job_search_form job_search_form--flat"
action="https://jobify-demos.astoundify.com/classic/find-a-job/"
method="GET">
Their 'find-a-job' page which you get taken to with the button has google maps which I don't want. So I have created a new [jobs] page I named 'job-search' without the map. If I edit the above url to that in chrome inspector then the button outputs the search results correctly on my new page. following this
I have now spent 2 days going through all the widget php code trying to find where I change that GET command url on this widget button from 'find-a-job' to 'job-search'. Adjusting this on a newly made search page seems straightforward as seen on this SO question
I have read about customising their widgets here and thought it would be in the class-widget-search-hero.php file (below). I have also checked its scss file but don't see anything there. So many hours looking, is it something as simple as deleting one of the two [jobs] pages? Thank you.
<?php
/**
* Home: Search Hero
*
* #package Jobify
* #category Widget
* #since 3.0.0
*/
class Jobify_Widget_Search_Hero extends Jobify_Widget {
public function __construct() {
$this->widget_description = __( 'Display a "hero" search area.', 'jobify' );
$this->widget_id = 'jobify_widget_search_hero';
$this->widget_cssclass = 'widget--home-hero-search';
$this->widget_name = __( 'Jobify - Page: Search Hero', 'jobify' );
$this->control_ops = array(
'width' => 400,
);
$this->settings = array(
'home widgetized' => array(
'std' => __( 'Homepage/Widgetized', 'jobify' ),
'type' => 'widget-area',
),
'height' => array(
'type' => 'select',
'std' => 'medium',
'label' => __( 'Hero Height', 'jobify' ),
'options' => array(
'small' => __( 'Small', 'jobify' ),
'medium' => __( 'Medium', 'jobify' ),
'large' => __( 'Large', 'jobify' ),
),
),
'margin' => array(
'type' => 'checkbox',
'std' => 1,
'label' => __( 'Add standard spacing above/below widget', 'jobify' ),
),
'text_color' => array(
'type' => 'colorpicker',
'std' => '#ffffff',
'label' => __( 'Text Color:', 'jobify' ),
),
'title' => array(
'type' => 'text',
'std' => '',
'label' => __( 'Title:', 'jobify' ),
),
'description' => array(
'type' => 'text',
'std' => '',
'label' => __( 'Description:', 'jobify' ),
'rows' => 5,
),
'image' => array(
'type' => 'image',
'std' => '',
'label' => __( 'Background Image:', 'jobify' ),
),
'background_position' => array(
'type' => 'select',
'std' => 'center center',
'label' => __( 'Image Position:', 'jobify' ),
'options' => array(
'left top' => __( 'Left Top', 'jobify' ),
'left center' => __( 'Left Center', 'jobify' ),
'left bottom' => __( 'Left Bottom', 'jobify' ),
'right top' => __( 'Right Top', 'jobify' ),
'right center' => __( 'Right Center', 'jobify' ),
'right bottom' => __( 'Right Bottom', 'jobify' ),
'center top' => __( 'Center Top', 'jobify' ),
'center center' => __( 'Center Center', 'jobify' ),
'center bottom' => __( 'Center Bottom', 'jobify' ),
'center top' => __( 'Center Top', 'jobify' ),
),
),
'cover_overlay' => array(
'type' => 'checkbox',
'std' => 1,
'label' => __( 'Use transparent overlay', 'jobify' ),
),
);
if ( jobify()->get( 'wp-job-manager-resumes' ) ) {
$what = array(
'what' => array(
'type' => 'select',
'std' => 'job',
'label' => __( 'Search:', 'jobify' ),
'options' => array(
'job' => __( 'Jobs', 'jobify' ),
'resume' => __( 'Resumes', 'jobify' ),
),
),
);
$this->settings = $what + $this->settings;
}
parent::__construct();
}
function widget( $args, $instance ) {
extract( $args );
$text_align = isset( $instance['text_align'] ) ? esc_attr( $instance['text_align'] ) : 'left';
$background_position = isset( $instance['background_position'] ) ? esc_attr( $instance['background_position'] ) : 'center center';
$overlay = isset( $instance['cover_overlay'] ) && 1 == $instance['cover_overlay'] ? 'has-overlay' : 'no-overlay';
$margin = isset( $instance['margin'] ) && 1 == $instance['margin'] ? true : false;
$height = isset( $instance['height'] ) ? esc_attr( $instance['height'] ) : 'medium';
if ( ! $margin ) {
$before_widget = str_replace( 'widget--home ', 'widget--home widget--home--no-margin ', $before_widget );
}
$image = isset( $instance['image'] ) ? esc_url( $instance['image'] ) : null;
$content = $this->assemble_content( $instance );
$what = isset( $instance['what'] ) ? esc_attr( $instance['what'] ) : 'job';
global $is_flat;
$is_flat = true;
ob_start();
?>
<?php echo $before_widget; ?>
<div class="hero-search hero-search--<?php echo esc_attr( $overlay ); ?> hero-search--height-<?php echo esc_attr( $height ); ?>" style="background-image:url(<?php echo $image; ?>); ?>; background-position: <?php echo $background_position; ?>">
<div class="container">
<?php echo $content; ?>
<?php locate_template( array( $what . '-filters-flat.php' ), true, false ); ?>
</div>
</div>
<?php echo $after_widget; ?>
<?php
$content = ob_get_clean();
echo apply_filters( $this->widget_id, $content );
}
private function assemble_content( $instance ) {
$text_color = isset( $instance['text_color'] ) ? esc_attr( $instance['text_color'] ) : '#fff';
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
$content = isset( $instance['description'] ) ? $instance['description'] : '';
$output = '<div class="hero-search__content" style="color:' . $text_color . '">';
$output .= '<h2 class="hero-search__title" style="color:' . $text_color . '">' . $title . '</h2>';
$output .= wpautop( $content );
$output .= '</div>';
return $output;
}
}
The solution (if not technical answer) was actually very easy once found.
After reading this tutorial and seeing the line:
$action_page_id = get_option('job_manager_jobs_page_id');
Then searching "job_manager_jobs_page_id" and finding this on github I discovered it exists under the job manager settings /wp-admin/edit.php?post_type=job_listing&page=job-manager-settings#settings-job_pages where you just change the 'Job Listings' page in the drop down from 'find a job' to my new 'job search' page. All working.
Please bear with me if I am not asking this question correctly. I am trying to modify a WooCommerce custom field to make it required.
The function is below:
public function delivery_checkout_field( $checkout )
{
echo '<div class="checkout-delivery" id="delivery_checkout_field">';
echo '<input class="js-picker-object" type="hidden" value="' . esc_attr( json_encode( $this->delivery_get_picker_object() ) ) . '" />';
echo '<h3 class="with-description">' . __( 'Delivery Schedule', 'gastro-core' ) . '</h3>';
echo '<p class="description">Enter your desired delivery time or leave blank, if you don\'t have one.</p>';
woocommerce_form_field( 'delivery_date', array(
'type' => 'text',
'class' => array( 'delivery-field form-row-first' ),
'label' => __( 'Delivery Date' ),
'placeholder' => __( 'Enter delivery date.' ),
), $checkout->get_value( 'delivery_date' ) );
woocommerce_form_field( 'delivery_time', array(
'type' => 'text',
'class' => array( 'delivery-field form-row-last' ),
'label' => __( 'Delivery Time' ),
'placeholder' => __( 'Enter delivery time.' ),
), $checkout->get_value( 'delivery_time' ) );
echo '</div>';
}
This section saves the fields.
// Save delivery data
public function delivery_checkout_field_update( $order_id )
{
if ( !empty( $_POST['delivery_date'] ) ) {
update_post_meta(
$order_id,
'Delivery Date',
sanitize_text_field( $_POST['delivery_date'] )
);
}
if ( !empty( $_POST['delivery_time'] ) ) {
update_post_meta(
$order_id,
'Delivery Time',
sanitize_text_field( $_POST['delivery_time'] )
);
}
}
For the required HTML5 attribute and other custom attributes woocommerce function has the custom_attributes option.
'custom_attributes' => array( 'required' => 'required' )
I've put together a WooCommerce store that uses variable products to sell tickets to events based on location and date.
When a user adds a ticket to their basket and checks out I'm trying to get a series of custom fields to show based on the number of tickets they have purchased. So for example, the fields I need to capture are:
Title
Name
Organisation
Position
Email
Tel
These will be captured for each person attending the event (i.e. the number of tickets being sold). It also needs to be able to capture this information for each event that's being attended.
I stumbled upon a piece of code on GitHub - https://gist.github.com/joslex/43a5c90af83f9024fd80a71495eac4fc - which seems to hold the answer - the only problem is this part of the code seems to trip it up:
$items = $woocommerce->cart->get_cart();
I've read that it has something to do with calling cart->get_cart(); after wp_loaded but if I want these fields to appear before woocommerce_before_order_notes I'm not sure how to avoid calling cart->get_cart();
Here's the full code from GitHub:
global $woocommerce;
$items = $woocommerce->cart->get_cart();
$i = 1;
foreach($items as $item => $values) {
$_product = $values['data']->post;
$quantity = $values['quantity'];
$x = 1;
while ($x <= $quantity) {
echo '<div class="camper-group"><h3>' . $_product->post_title . __(' - Camper ' . $x . ' Information') .'</h3>';
woocommerce_form_field( 'camper_name_' . $x, array(
'type' => 'text',
'class' => array('camper form-row-wide'),
'label' => __('Camper ' . $x . ' Name'),
'placeholder' => __(''),
), $checkout->get_value( 'camper_name_' . $x ));
woocommerce_form_field( 'camper_grade_' . $x, array(
'type' => 'select',
'class' => array('camper form-row-wide'),
'label' => __('Camper ' . $x . ' Grade'),
'placeholder' => __(''),
'options' => array(
'blank' => __( 'Select a grade', 'wps' ),
'1st' => __( '1st', 'wps' ),
'2nd' => __( '2nd', 'wps' ),
'3rd' => __( '3rd', 'wps' ),
'4th' => __( '4th', 'wps' ),
'5th' => __( '5th', 'wps' ),
'6th' => __( '6th', 'wps' ),
'7th' => __( '7th', 'wps' ),
'8th' => __( '8th', 'wps' ),
'9th' => __( '9th', 'wps' ),
'10th' => __( '10th', 'wps' )
)
), $checkout->get_value( 'camper_grade_' . $x ));
woocommerce_form_field( 'camper_school_' .$x, array(
'type' => 'text',
'class' => array('camper form-row-wide'),
'label' => __('Camper ' . $x . ' School Name'),
'placeholder' => __('The school your child will attend next fall'),
), $checkout->get_value( 'camper_school_' . $x ));
echo '</div>';
$x++;
}
$i++;
}
I'm working on a Wordpress site which has a drop-down/select list for ordering products.
I'd really like to style this nicely and have found a pretty good set of styles courtesy of codrops I'd like to use.
However the HTML for this uses UL LI list rather than the standard select.
I need to try and convert the following code:
<select name="orderby" class="orderby">
<?php
$catalog_orderby = apply_filters( 'woocommerce_catalog_orderby', array(
'menu_order' => __( 'Default sorting', 'woocommerce' ),
'popularity' => __( 'Sort by popularity', 'woocommerce' ),
'rating' => __( 'Sort by average rating', 'woocommerce' ),
'date' => __( 'Sort by newness', 'woocommerce' ),
'price' => __( 'Sort by price: low to high', 'woocommerce' ),
'price-desc' => __( 'Sort by price: high to low', 'woocommerce' )
) );
if ( get_option( 'woocommerce_enable_review_rating' ) == 'no' )
unset( $catalog_orderby['rating'] );
foreach ( $catalog_orderby as $id => $name )
echo '<option value="' . esc_attr( $id ) . '" ' . selected( $orderby, $id, false ) . '>' . esc_attr( $name ) . '</option>';
?>
into something resembling this:
<div class="wrapper-dropdown">
<span>I'm kinda the label!</span>
<ul class="dropdown">
<li>I'm hidden!</li>
<li>Me too!</li>
<li>So do I.</li>
</ul>
</div>
<div class="wrapper-dropdown">
<span>I'm kinda the label!</span>
<ul class="dropdown">
<?php
$catalog_orderby = apply_filters( 'woocommerce_catalog_orderby', array(
'menu_order' => __( 'Default sorting', 'woocommerce' ),
'popularity' => __( 'Sort by popularity', 'woocommerce' ),
'rating' => __( 'Sort by average rating', 'woocommerce' ),
'date' => __( 'Sort by newness', 'woocommerce' ),
'price' => __( 'Sort by price: low to high', 'woocommerce' ),
'price-desc' => __( 'Sort by price: high to low', 'woocommerce' )
) );
if ( get_option( 'woocommerce_enable_review_rating' ) == 'no' )
unset( $catalog_orderby['rating'] );
foreach ( $catalog_orderby as $id => $name )
echo '<li>' . esc_attr( $name ) . '</li>';
?>
</ul>
</div>
Just switch out the echo <option> code with echo <li> code