CMB Metabox and Select Dropdown - wordpress

i am having problem with CMB metabox and select dropdown. I can see only first letter of value for example if my select box have in array Banana, Orange, Apple it prints only first letter both in admin and website (frontend). What can cause that problem?
array(
'name' => 'Test Radio',
'id' => $prefix . 'test_radio',
'type' => 'radio',
'options' => array(
'standard' => __( 'Banana', 'cmb' ),
'custom' => __( 'Orange', 'cmb' ),
'none' => __( 'Apple', 'cmb' ),
),
),

I had the same problem. This works for me :
array(
'name' => 'Test Select',
'id' => $prefix . 'test_select',
'type' => 'select',
'options' => array(
array(
'name' => __( 'Banana', 'cmb' ),
'value' => 'banana',
),
array(
'name' => __( 'Orange', 'cmb' ),
'value' => 'orange',
),
array(
'name' => __( 'Apple', 'cmb' ),
'value' => 'apple',
),
),
),

You are using a radio type field and need the select type field. Try this:
array(
'name' => 'Test Select',
'id' => $prefix . 'test_select',
'type' => 'select',
'options' => array(
'banana' => __( 'Banana', 'cmb' ),
'orange' => __( 'Orange', 'cmb' ),
'apple' => __( 'Apple', 'cmb' ),
),
),

Related

How to return HTML in woocommerce dropdown select

I have this code for the woocommerce dropdown select:
//add stock qty and status to variation dropdown
add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'variations_options_html_callback', 10, 2);
function variations_options_html_callback( $html, $args ){
$args = wp_parse_args( apply_filters( 'woocommerce_dropdown_variation_attribute_options_args', $args ), array(
'options' => false,
'attribute' => false,
'product' => false,
'selected' => false,
'name' => '',
'id' => '',
'class' => '',
'show_option_none' => __( 'Choose an option', 'woocommerce' ),
) );
$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' ); // We'll do our best to hide the placeholder, but we'll need to show something when resetting options.
if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {
$attributes = $product->get_variation_attributes();
$options = $attributes[ $attribute ];
}
$html = '<div class="custom-select"><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></div>';
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();
$stock_order = $product->is_on_backorder();
break;
}
}
// if the stock of the product is lower than 0 it returns
if ( $variation_obj->get_stock_quantity() <= 0 && $product->is_on_backorder() ) {
return ' ';
}
else {
return $stock_qty == 0 ? ' POWIADOM MNIE' : ' ';
}
}
And I would like the string "POWIADOM MNIE" to be in a different color than the attribute name. when I put HTML in there it doesn't work, just the html gets displayed as a string...
I tried:
$text='<p>'.'POWIADOM MNIE'.'</p>';
and then:
return $stock_qty == 0 ? ' $text ' : ' ';
or simply:
return $stock_qty == 0 ? '<p>'.'POWIADOM MNIE'.'</p>' : ' ';
but neither works. It looks like this:
dropdown select
Can you help me do it, please?

argondash don't work with horizontal layout

Hi and thanks for reading me
I am working with a dashboard made with shiny, argondash and argonr, but I noticed that the horizontal layout does not work correctly, since the windows continue to be displayed vertically. Is there any way to correct this? I would expect it to look like this:
The code is the following:
library(shiny)
library(argonDash)
library(argonR)
shinyApp(
ui = argonDashPage(title = 'Sync the City',
description = 'Testing',
header = argonDashHeader(color = 'primary', separator = TRUE),
sidebar = argonDashSidebar(vertical = FALSE,
brand_logo = "https://elpacientecolombiano.com/wp-content/uploads/2015/09/LOGO-SALUD-MORADO.png",
id = 'sidebar',
argonSidebarHeader(title = 'Main Menu'),
argonSidebarMenu(
argonSidebarItem(tabName = 'landing_page', 'Landing Page'),
argonSidebarItem(tabName = 'map', 'Map'),
argonSidebarItem(tabName = 'network', 'Network')
)
)
),
server = function(input, output){}
)
I added the following arguments to your code :
size = "md" in argonDashSidebar()
style = "display:-webkit-inline-box;" in argonSidebarMenu()
library(shiny)
library(argonDash)
library(argonR)
shinyApp(
ui = argonDashPage(
title = 'Sync the City',
description = 'Testing',
header = argonDashHeader(color = 'primary', separator = TRUE),
sidebar = argonDashSidebar(
vertical = FALSE,
brand_logo = "https://elpacientecolombiano.com/wp-content/uploads/2015/09/LOGO-SALUD-MORADO.png",
id = 'sidebar',
size = "md",
argonSidebarHeader(title = 'Main Menu'),
argonSidebarMenu(
style = "display:-webkit-inline-box;",
argonSidebarItem(tabName = 'landing_page', 'Landing Page'),
argonSidebarItem(tabName = 'map', 'Map'),
argonSidebarItem(tabName = 'network', 'Network')
)
)
),
server = function(input, output){}
)

Display different selection menus based on response to previous menu in Shiny

I'm trying to dynamically display different selection menus in shiny based on a user response to a previous menu. For example if the user selects a 'campaign_id' of Launch Sequence, they should get a drop down menu with the appropriate options. However, if they select Sale Event they should get a numeric text entry for the discount %. I've created all the appropriate selection menus, but now I need to figure out how to dynamically display them. I tried using ifelse logic based on the response to the previous question, but it always displays the menu associated with all false responses.
I suspect that when the logic is evaluating the previous response that response isn't formatted correctly, or has some html in there so the logic always evaluates to false. I've included a simplified example of what I'm experiencing. Any help is greatly appreciated.
library(shiny)
library(tidyverse)
# data referenced 1
cid <- tibble(
name = c(
'Free Content',
'Launch Sequence',
'Mega Sale',
'Sale Email',
'Sale Reminder',
'VSL Push'
),
abbr = c('FC',
'LS',
'MS',
'SE',
'SR',
'VP')
)
# data referenced 2
csid <- tibble(
name = c(
'Free Content',
'Launch Sequence',
'Launch Sequence',
'Launch Sequence',
'Launch Sequence',
'Launch Sequence',
'Mega Sale',
'Sale Email',
'Sale Reminder',
'VSL Push'
),
subname = c(
'topic text entry',
'Launch Sequence1',
'Launch Sequence2',
'Launch Sequence3',
'Launch Sequence4',
'Launch Sequence5',
'NA',
'discount numeric entry',
'NA',
'url'
),
abbr = c(
'NA1',
'LS1',
'LS2',
'LS3',
'LS4',
'LS5',
'NA2',
'NA3',
'NA4',
'NA5'
)
)
ui <- fluidPage(
titlePanel("Example Selection"),
sidebarLayout(
sidebarPanel(
selectInput(
inputId = 'campaign_id',
label = 'Campaign ID',
choices = c(cid$name)
),
uiOutput(ifelse(
# which selection menu to display based on campaign_id
'cid' == 'Launch Sequence',
'ls',
ifelse(
'cid' == 'Free Content',
'fc',
ifelse(
'cid' == 'Sale Email',
'se',
ifelse('cid' == 'VSL Push',
'vp',
'mssr')
)
)
)),
textInput(
inputId = 'date',
label = 'Launch Date',
placeholder = paste0('use date format "', Sys.Date(), '"')
)
),
############### Main panel for displaying outputs ----
mainPanel(
textOutput(outputId = "campaign_name"))
))
############## server functions --------------------------------------
server <- function(input, output) {
# build campaign sub-id list based on campaign selected ----
cidselected <- reactive({
input$campaign_id
})
output$cid <- renderText({
cidselected()
})
output$ls = renderUI({
selectInput(
inputId = 'campaign_subid',
label = 'Campaign Sub-ID',
choices = c(
'Launch Sequence1',
'Launch Sequence2',
'Launch Sequence3',
'Launch Sequence4',
'Launch Sequence5'
)
)
})
output$fc = renderUI({
textInput(inputId = 'campaign_subid',
label = 'Campaign Sub-ID',
placeholder = '1-3 word description of main topic')
})
output$vp = renderUI({
textInput(inputId = 'campaign_subid',
label = 'Campaign Sub-ID',
placeholder = 'enter VSL url here')
})
output$mssr = renderUI({
selectInput(inputId = 'campaign_subid',
label = 'Campaign Sub-ID',
choices = c('N/A'))
})
output$se = renderUI({
numericInput(
inputId = 'campaign_subid',
label = 'Campaign Sub-ID (enter as whole number 40% = 40)',
value = 0,
min = 0,
max = 100
)
})
output$campaign_name <-
renderText({
# display combined name with abbreviations
cid = cid$abbr[cid$name == input$campaign_id]
csid = input$campaign_subid
date = gsub('-', '', input$date, fixed = T)
paste(cid,
csid,
date,
sep = '-')
})
}
shinyApp(ui = ui, server = server)
It sounds like you're looking for conditional panels. There are more complex methods if you have more input selections/combinations (you can create a uiOutput which is dynamically created in server.R, for one), but this is the simple and straightforward way to do what you want.

How to echo "# in stock" from product page on WooCommerce?

I try to print again "In stock" inside the product page but I cant find solution how can I echo it. On the image is by default but I want to have it again on the same page, how can i echo / print that.
Thanks
>>>1 in stock Image <<<
function wc_dropdown_variation_attribute_options( $args = array() ) {
global $product;
$variations = $product->get_available_variations();
$args = wp_parse_args( apply_filters( 'woocommerce_dropdown_variation_attribute_options_args', $args ), array(
'options' => false,
'attribute' => false,
'product' => false,
'selected' => false,
'name' => '',
'id' => '',
'class' => '',
'show_option_none' => __( 'Choose an option', 'woocommerce' ),
) );
$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 = '<select id="' . esc_attr( $id ) . '" class="' . esc_attr( $class ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '">';
if ( $args['show_option_none'] ) {
$html .= '<option value="">' . esc_html( $args['show_option_none'] ) . '</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 ) ) {
$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 ) ) . '</option>';
}
}
} else {*/
foreach ( $options as $option ) {
foreach ($variations as $variation) {
if($variation['attributes'][$name] == $option) {
$stock = esc_html($variation['max_qty']);
}
}
if( $stock == 0) {
$stock_text = ' - (Out of Stock)';
$class = 'option-out-of-stock';
$disabled = 'disabled';
} elseif ($stock < 5 ) {
$stock_text = ' - Only ' . $stock . ' left!';
$class= 'option-hurry';
$disabled = '';
} elseif ($stock < 6) {
$stock_text = ' - Only a few left!';
$class = 'option-few';
$disabled = '';
} else {
$stock_text = ' - (In Stock)';
$class = '';
$disabled = '';
}
// 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 . ' class="'.$class.'" '.$disabled.'>' . $option . $stock_text .'</option>';
//}
}
}
$html .= '</select>';
echo apply_filters( 'woocommerce_dropdown_variation_attribute_options_html', $html, $args );
}

Visual Composer > Dependency not working

I am creating a plugin and trying to implement Visual Composer in my plugin codes.
I am using the following codes for implementing dependency.
$params = array(
array(
"type" => "checkbox",
"heading" => "Checkbox 1",
"param_name" => "check1",
),
array(
"type" => "textfield",
"heading" => "My textbox",
"param_name" => "text1",
"group" => "Group 1",
"dependency" => array(
"element" => "check1",
"value" => "true"
)
),
);
vc_map( array(
"name" => "My Shortcode",
"base" => "my_shortcode",
"class" => "",
"category" => "ABC",
"params" => $params
));
But these codes are not working.
$params = array(
array(
"type" => "checkbox",
"heading" => "Checkbox 1",
"param_name" => "check1",
"value" => array(
"" => "true"
),
),
array(
"type" => "textfield",
"heading" => "My textbox",
"param_name" => "text1",
"group" => "Group 1",
"dependency" => array(
"element" => "check1",
"value" => "true"
)
),
);
vc_map( array(
"name" => "My Shortcode",
"base" => "my_shortcode",
"class" => "",
"category" => "ABC",
"params" => $params
));
Just set the value "value" => array("" => "true"), in parent array.
$params = array(
array(
"type" => "checkbox",
"heading" => "Checkbox 1",
"param_name" => "check1",
),
array(
"type" => "textfield",
"heading" => "My textbox",
"param_name" => "text1",
"group" => "Group 1",
"dependency" => array(
"element" => "check1",
"value" => "1"
)
),
);
vc_map( array(
"name" => "My Shortcode",
"base" => "my_shortcode",
"class" => "",
"category" => "ABC",
"params" => $params
));
You can use like this.

Resources