Conditionally unset checkout field in woocommerce - php

How can I conditionally unset the other two fields from server side and remove the required validation from it?
Here is how the form looks like:
Here is the code:
function bs_filter_checkout_fields($fields){
$fields['billing'] = array(
'add_type' => array(
'type' => 'radio',
'label' => __( 'Address Type' ),
'options' => array( 'house' => __( 'House' ), 'building' => __( 'Building' ), 'office' => __( 'Office' ) ),
'required' => true
),
'add_house_name' => array(
'type' => 'text',
'required' => true,
'placeholder' => __( 'House Name/Number' ),
'label' => __( 'House Name/Number' )
),
'add_building_name' => array(
'type' => 'text',
'required' => true,
'placeholder' => __( 'Building Name/Number' ),
'label' => __( 'Building Name/Number' )
),
'add_office_name' => array(
'type' => 'text',
'required' => true,
'placeholder' => __( 'Office Name/Number' ),
'label' => __( 'Office Name/Number' )
),
);
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'bs_filter_checkout_fields' );
function bs_conditional_scripts() {
wc_enqueue_js( "
$( '#add_type_house' ).change( function () {
if ( $( this ).is( ':checked' ) ) {
$('#add_building_name_field').hide();
$('#add_office_name_field').hide();
$( '#add_house_name_field' ).show();
}
} ).change();
$( '#add_type_building' ).change( function () {
if ( $( this ).is( ':checked' ) ) {
$( '#add_house_name_field' ).hide();
$('#add_office_name_field').hide();
$('#add_building_name_field').show();
}
} ).change();
$( '#add_type_office' ).change( function () {
if ( $( this ).is( ':checked' ) ) {
$( '#add_house_name_field' ).hide();
$('#add_building_name_field').hide();
$('#add_office_name_field').show();
}
} ).change();
" );
}
add_action( 'wp_enqueue_scripts', 'bs_conditional_scripts' );

function bs_filter_checkout_fields($fields){
$fields['billing'] = array(
'add_type' => array(
'type' => 'radio',
'label' => __( 'Address Type' ),
'options' => array( 'house' => __( 'House' ), 'building' => __( 'Building' ), 'office' => __( 'Office' ) ),
'required' => true
),
'add_house_name' => array(
'type' => 'text',
'required' => true,
'placeholder' => __( 'House Name/Number' ),
'label' => __( 'House Name/Number' )
),
'add_building_name' => array(
'type' => 'text',
'required' => true,
'placeholder' => __( 'Building Name/Number' ),
'label' => __( 'Building Name/Number' )
),
'add_office_name' => array(
'type' => 'text',
'required' => true,
'placeholder' => __( 'Office Name/Number' ),
'label' => __( 'Office Name/Number' )
),
);
if( true ){ // pass conditional statement here
unset($fields['billing']['add_house_name']); // remove field
$fields['billing']['add_building_name']['required'] = false; // remove required validation
}
return $fields;
}

Related

Updating WooCommerce Admin Shipping Order Fields

In functions.php I am trying to use this, but it is not working:
function rt_woocommerce_admin_shipping_fields( $fields ) {
$fields['first_name']['value'] = $_GET['f'];
$fields['last_name']['value'] = $_GET['l'];
$fields['address_1']['value'] = $_GET['a'];
$fields['address_2']['value'] = $_GET['b'];
// etc
// etc
return $fields;
}
add_filter( 'woocommerce_admin_shipping_fields', 'rt_woocommerce_admin_shipping_fields' );
woocommerce_admin_billing_fields() works but the shipping function does not. Any advice? I need to update the fields with $_GET variables on page load. This works perfectly for the billing fields.
The array has label and show indexes for each item in shipping fields. There is no value index by default. See the woocommerce_admin_shipping_fields filter below.
self::$shipping_fields = apply_filters(
'woocommerce_admin_shipping_fields',
array(
'first_name' => array(
'label' => __( 'First name', 'woocommerce' ),
'show' => false,
),
'last_name' => array(
'label' => __( 'Last name', 'woocommerce' ),
'show' => false,
),
'company' => array(
'label' => __( 'Company', 'woocommerce' ),
'show' => false,
),
'address_1' => array(
'label' => __( 'Address line 1', 'woocommerce' ),
'show' => false,
),
'address_2' => array(
'label' => __( 'Address line 2', 'woocommerce' ),
'show' => false,
),
'city' => array(
'label' => __( 'City', 'woocommerce' ),
'show' => false,
),
'postcode' => array(
'label' => __( 'Postcode / ZIP', 'woocommerce' ),
'show' => false,
),
'country' => array(
'label' => __( 'Country / Region', 'woocommerce' ),
'show' => false,
'type' => 'select',
'class' => 'js_field-country select short',
'options' => array( '' => __( 'Select a country / region…', 'woocommerce' ) ) + WC()->countries->get_shipping_countries(),
),
'state' => array(
'label' => __( 'State / County', 'woocommerce' ),
'class' => 'js_field-state select short',
'show' => false,
),
'phone' => array(
'label' => __( 'Phone', 'woocommerce' ),
),
)
);

WooCommerce Custom Shipping Method Plugin That Returns a Shipping Rate Based on Multiple Criteria

I am trying to create a WooCommerce plugin that returns a shipping cost for each product in a cart based on the following criteria:
The shipping destination zip code
The product's SKU
This what I have so far: it loads and activates but does not provide the shipping rate. I receive a message from the cart stating in effect that there are no shipping cost associated with the zone found.
/**
* Plugin Name: JxJ Shipping
* Description: Custom Shipping Method for WooCommerce
* Version: 1.0:
* License: GPL-3.0+
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
* Domain Path: /lang
* Text Domain: jxj
*
* WC requires at least: 3.5
* WC tested up to: 4.0
*
*/
if ( ! defined( 'WPINC' ) ) {
die;
}//if not defined
/**
* Check if WooCommerce is active
*/
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option(
'active_plugins' ) ) ) ) {
function jxj_shipping_method_init() {
if ( ! class_exists( 'WC_JxJ_Shipping_Method' ) ) {
class WC_JxJ_Shipping_Method extends WC_Shipping_Method {
/**
* Constructor for your shipping class
*
* #access public
* #return void
*/
public function __construct() {
$this->id = 'jxj_method';
$this->method_title = __( 'JxJ Shipping Method' );
$this->method_description = __( 'Custom Shipping Method for JuicedbyJ' );
$this->init();
$this->enabled = isset( $this->settings['enabled'] ) ? $this->settings['enabled'] : 'yes';
$this->title = isset( $this->settings['title'] ) ? $this->settings['title'] : __( 'jxj Shipping', 'jxj' );
}
/**
* Init your settings
*
* #access public
* #return void
*/
function init() {
$this->init_form_fields();
$this->init_settings();
add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
}
/**
* Define settings field for this shipping method
* #return void
*/
function init_form_fields() {
$this->form_fields = array(
'enabled' => array(
'title' => __( 'Enable', 'jxj' ),
'type' => 'checkbox',
'description' => __( 'Enable this shipping.', 'jxj' ),
'default' => 'yes',
'class'=> 'WC_JxJ_Shipping_Method'
),
'title' => array(
'title' => __( 'Title', 'jxj' ),
'type' => 'text',
'description' => __( 'Title to be display on site', 'jxj' ),
'default' => __( 'jxj Shipping', 'jxj' ),
'class'=> 'WC_JxJ_Shipping_Method'
),
'packaging' => array(
'title' => array(
'title'=>__( '6 Pack (120z)', 'jxj' ),
'type' => 'checkbox',
'description' => __( 'Juice sold in package of 6 (12oz plastic bottles)', 'jxj' ),
'default' => 'yes',
'class'=> 'WC_JxJ_Shipping_Method'
),
'title' => array(
'title' => __( '24 Case (120z)', 'jxj' ),
'type' => 'checkbox',
'description' => __( 'Juice sold in case of 24 (12oz plastic bottles)', 'jxj' ),
'default' => 'yes',
'class'=> 'WC_JxJ_Shipping_Method'
),
'title' => array(
'title' => __( '3 Pack (750ml)', 'jxj' ),
'type' => 'checkbox',
'description' => __( 'Juice sold in package of 3 (750ml glass bottles)', 'jxj' ),
'default' => 'yes',
'class'=> 'WC_JxJ_Shipping_Method'
),
'title' => array(
'title' => __( '12 Case (750ml)', 'jxj' ),
'type' => 'checkbox',
'description' => __( 'Juice sold in case of 12 (750ml glass bottles)', 'jxj' ),
'default' => 'yes',
'class'=> 'WC_JxJ_Shipping_Method'
),
),
'weight' => array(
'title' => __( 'Weight (oz)', 'jxj' ),
'type' => 'number',
'description' => __( 'Maximum allowed weight', 'jxj' ),
'default' => 350,
'class'=> 'WC_JxJ_Shipping_Method'
),
/**Available regions*/
'regions' => array(
array(
'title' => __( 'Bangor', 'jxj' ),
'type' => 'checkbox',
'description' => __( 'Region of Bangor, ME rates', 'jxj' ),
'default' => 'yes',
'class'=> 'WC_JxJ_Shipping_Method'
),
array(
'title' => __( 'Hartford', 'jxj' ),
'type' => 'checkbox',
'description' => __( 'Region of Hartford, CT rates', 'jxj' ),
'default' => 'yes',
'class'=> 'WC_JxJ_Shipping_Method'
),
array(
'title' => __( 'Burlington', 'jxj' ),
'type' => 'checkbox',
'description' => __( 'Region of Burlington, VT rates', 'jxj' ),
'default' => 'yes',
'class'=> 'WC_JxJ_Shipping_Method'
),
array(
'title' => __( 'Atlanta', 'jxj' ),
'type' => 'checkbox',
'description' => __( 'Region of Atlanta, GA rates', 'jxj' ),
'default' => 'yes',
'class'=> 'WC_JxJ_Shipping_Method'
),
array(
'title' => __( 'Miami', 'jxj' ),
'type' => 'checkbox',
'description' => __( 'Region of Miami, FL rates', 'jxj' ),
'default' => 'yes',
'class'=> 'WC_JxJ_Shipping_Method'
),
array(
'title' => __( 'Jackson', 'jxj' ),
'type' => 'checkbox',
'description' => __( 'Region of Jackson, MS rates', 'jxj' ),
'default' => 'yes',
'class'=> 'WC_JxJ_Shipping_Method'
),
array(
'title' => __( 'Louisville', 'jxj' ),
'type' => 'checkbox',
'description' => __( 'Region of Louisville, KY rates', 'jxj' ),
'default' => 'yes',
'class'=> 'WC_JxJ_Shipping_Method'
),
array(
'title' => __( 'Cleveland', 'jxj' ),
'type' => 'checkbox',
'description' => __( 'Region of Cleveland, OH rates', 'jxj' ),
'default' => 'yes',
'class'=> 'WC_JxJ_Shipping_Method'
),
array(
'title' => __( 'Des Moines', 'jxj' ),
'type' => 'checkbox',
'description' => __( 'Region of Des Moines, IA rates', 'jxj' ),
'default' => 'yes',
'class'=> 'WC_JxJ_Shipping_Method'
),
array(
'title' => __( 'Madison', 'jxj' ),
'type' => 'checkbox',
'description' => __( 'Region of Madison, WI rates', 'jxj' ),
'default' => 'yes',
'class'=> 'WC_JxJ_Shipping_Method'
),
array(
'title' => __( 'Minneapolis', 'jxj' ),
'type' => 'checkbox',
'description' => __( 'Region of Minneapolis, MN rates', 'jxj' ),
'default' => 'yes',
'class'=> 'WC_JxJ_Shipping_Method'
),
array(
'title' => __( 'Chicago', 'jxj' ),
'type' => 'checkbox',
'description' => __( 'Region of Chicago, IL rates', 'jxj' ),
'default' => 'yes',
'class'=> 'WC_JxJ_Shipping_Method'
),
array(
'title' => __( 'New Orleans', 'jxj' ),
'type' => 'checkbox',
'description' => __( 'Region of New Orleans, LA rates', 'jxj' ),
'default' => 'yes',
'class'=> 'WC_JxJ_Shipping_Method'
),
array(
'title' => __( 'Little Rock', 'jxj' ),
'type' => 'checkbox',
'description' => __( 'Region of Little Rock, AR rates', 'jxj' ),
'default' => 'yes',
'class'=> 'WC_JxJ_Shipping_Method'
),
),
);
}
/**
* calculate_shipping function.
*
* #access public
* #param mixed $package
* #return void
*/
public function calculate_shipping( $package ) {
$packKey = '';
$cost = 0;
$zone = "";
foreach ( $package['contents'] as $item_id => $values )
{
$_product = $values['data'];
$price = $_product->get_price();
$packaging = $_product->get_sku();
$strPack = strval($packaging);
$strSize = strlen($packaging);
if ( $strSize == 9 ) {
return $packKey = substr($strPack, $strSize-1, 1);
} else {
return $packKey = substr($strPack, $strSize-2, 2);
}
$weight = $_product->get_weight();
$str_weight = strval($weight);
$zipcode = $package [ "destination" ][ "postcode" ];
$str_zip = strval($zipcode);
$subtotal = WC()->cart->get_cart_totals();
$shipZones = array (
"Bangor"=>array('04401','04402'),
"Burlington"=>array('05401','05402'),
"Hartford"=>array('06101','06012'),
"Atlanta"=>array('30301','30302'),
"Miami"=>array('33114','33109'),
"Jackson"=>array('39056','39174'),
"Loiusville"=>array('40018','40023'),
"Cleveland"=>array('44101','44103'),
"Des Moines"=>array('50211','50047'),
"Madison"=>array('53558','53562'),
"Minneapolis"=>array('55111','55401'),
"Chicago"=>array('60106','60131'),
"New Orleans"=>array('70032','70043'),
"Little Rock"=>array('72002','72103'),
);
/**
/*array("zone"=>'pack'=>cost)
*/
$zonePrices = array (
"Bangor"=>array('3'=>18.37,'12'=>46.54,'6'=>14.64,'24'=>29.08),
"Burlington"=>array('3'=>11.54,'12'=>26.62,'6'=>10.35,'24'=>15.46),
"Hartford"=>array('3'=>12.32,'12'=>30.57,'6'=>11.10,'24'=>17.57),
"Atlanta"=>array('3'=>18.37,'12'=>46.54,'6'=>14.64,'24'=>29.08),
"Miami"=>array('3'=>20.64,'12'=>58.16,'6'=>16.64,'24'=>37.74),
"Jackson"=>array('3'=>20.62,'12'=>58.16,'6'=>16.64,'24'=>37.74),
"Louisville"=>array('3'=>18.37,'12'=>46.54,'6'=>14.64,'24'=>29.08),
"Cleveland"=>array('3'=>13.69,'12'=>36.92,'6'=>12.04,'24'=>20.70),
"Des Moines"=>array('3'=>18.37,'12'=>46.54,'6'=>14.64,'24'=>29.08),
"Madison"=>array('3'=>18.37,'12'=>45.54,'6'=>14.64,'24'=>29.08),
"Minneapolis"=>array('3'=>18.37,'12'=>46.54,'6'=>14.64,'24'=>29.08),
"Chicago"=>array('3'=>18.37,'12'=>46.54,'6'=>14.64,'24'=>29.08),
"New Orleans"=>array('3'=>20.62,'12'=>58.16,'6'=>16.64,'24'=>37.74),
"Little Rock"=>array('3'=>20.62,'12'=>58.16,'6'=>16.64,'24'=>37.74)
);
$searchArray1 = [];
$searchArray2 = [];
$shipRate = 0;
$region = '';
$regionRef = '';
foreach ( $shipZones as $key => $value ) {
$searchArray1 = $value;
$regionRef = $key;
//find zipcode in value array
foreach ( $searchArray1 as $key => $value ) {
if ( $zipcode == $value ) {
$region = $regionRef;
foreach ( $zonePrices as $key => $value ) {
$searchArray2 = $value;
if ( $region == $key ) {
foreach ( $searchArray2 as $key => $value) {
if ( $packKey == $key ) {
$shipRate = round($value,2);
return $shipRate = sprintf('%.2f',$value);
}
}
}
}
} else {
return $shipRate = $price * 0.2;
}
}
}
$rate = array(
'id' => $this->id,
'label' => $this->title,
'cost' => $shipRate,
'calc_tax' => 'per_item'
);
$this->add_rate( $rate );
}
}
}
}
}
add_action( 'woocommerce_shipping_init', 'jxj_shipping_method_init' );
function add_jxj_shipping_method( $methods ) {
$methods['jxj_shipping_method'] = 'WC_JxJ_Shipping_Method';
return $methods;
}
add_filter( 'woocommerce_shipping_methods', 'add_jxj_shipping_method' );
}

Custom divi builder child module not rendering

I am trying to create some custom Divi builder modules.
I followed the sparse documentation at their website, trying to create a module and a child module. Everything seems to be working fine, but the rendering.
It is just rendering the module shortcode as a string, and not the content.
Current code of the parent module is
class DEDE_Cards extends ET_Builder_Module {
public function init(){
$this->name = esc_html__('Custom Card', 'dede-designvox-divi-extension');
$this->plural = esc_html__('Custom Cards', 'dede-designvox-divi-extension');
$this->main_css_element = 'cards-wrapper';
$this->slug = 'dede_cards';
$this->vb_support = 'on';
$this->child_slug = 'dede_card_item';
}
public function get_fields(){
return array(
'title' => array(
'label' => esc_html__( 'Title', 'dede-designvox-divi-extension' ),
'type' => 'text',
'toggle_slug' => 'main_content',
'description' => esc_html__( 'Type the section title' ),
),
'card_title_position' => array(
'label' => esc_html__( 'Title Position', 'dede-designvox-divi-extension' ),
'type' => 'select',
'options' => array(
'overlay' => esc_html__( 'Overlay', 'et_builder' ),
'top' => esc_html__( 'Over image', 'et_builder' ),
'bottom' => esc_html__( 'Under image', 'et_builder' ),
),
'toggle_slug' => 'main_content',
'description' => esc_html__( 'Here you can choose the position of the card title', 'dede-designvox-divi-extension' ),
'default_on_front' => 'bottom',
),
);
}
function before_render() {
global $dede_card_item_number;
$dede_card_item_number = 1;
}
public function render($unprocessed_props, $content = null, $render_slug ){
global $dede_card_item_number;
return sprintf(
'<div>%1$s</div>',
$this->content
);
}
public function add_new_child_text() {
return esc_html__( 'Add New Card Item1', 'dede-designvox-divi-extension' );
}
}
new DEDE_Cards;
And then the child module
class DEDE_Card_Item extends ET_Builder_Module {
public function init(){
$this->name = esc_html__('Custom Card', 'dede-designvox-divi-extension');
$this->plural = esc_html__('Custom Cards', 'dede-designvox-divi-extension');
$this->type= 'child';
$this->child_title_var = 'title';
$this->no_render = true;
$this->slug = 'dede_card_item';
$this->vb_support = 'on';
}
public function get_fields(){
return array(
'title' => array(
'label' => esc_html__('Title', 'dede-designvox-divi-extension'),
'type' => 'text',
'description' => esc_html__('The title of this card', 'dede-designvox-divi-extension'),
'toggle_slug' => 'main_content',
),
'desc' => array(
'label' => esc_html__( 'Description', 'dede-designvox-divi-extension' ),
'type' => 'textarea',
'description' => esc_html__( 'The card description' ),
'toggle_slug' => 'main_content',
),
'src' => array(
'label' => esc_html__( 'Image', 'dede-designvox-divi-extension'),
'type' => 'upload',
'description' => esc_html__( 'Upload your desired image, or type in the URL to the image', 'dede-designvox-divi-extension'),
'upload_button_text' => esc_attr__('Upload an image', 'dede-designvox-divi-extension'),
'choose_text' => esc_attr__('Choose an Image', 'dede-designvox-divi-extension'),
'update_text' => esc_attr__('Set as Image', 'dede-designvox-divi-extension'),
'affects' => 'alt',
'toggle_slug' => 'main_content',
),
'alt' => array(
'label' => esc_html__( 'Image Alternative Text', 'dede-designvox-divi-extension' ),
'type' => 'text',
'depends_show_if' => 'on',
'depends_on' => array(
'src',
),
'description' => esc_html__( 'This defines the HTML ALT text. A short description of your image can be placed here.', 'dede-designvox-divi-extension' ),
'toggle_slug' => 'main_content',
),
'url' => array(
'label' => esc_html__( 'Link URL', 'dede-designvox-divi-extension' ),
'type' => 'text',
'description' => esc_html__( 'Enter the link that you would like this card to direct to.', 'dede-designvox-divi-extension' ),
'toggle_slug' => 'main_content',
),
);
}
}
new DEDE_Card_Item;
The shortcode is just being rendered as a string on the website, like:
[dede_card_item _builder_version=”3.14″ title=”#1 card title” desc=”#1 card desc” src=”http://localhost:8888/wp-content/uploads/2018/09/14079861_1202408189819777_4296465020285869305_n.jpg” use_background_color_gradient=”off” background_color_gradient_start=”#2b87da” background_color_gradient_end=”#29c4a9″ background_color_gradient_type=”linear” background_color_gradient_direction=”180deg” background_color_gradient_direction_radial=”center” background_color_gradient_start_position=”0%” background_color_gradient_end_position=”100%” background_color_gradient_overlays_image=”off” parallax=”off” parallax_method=”on” background_size=”cover” background_position=”center” background_repeat=”no-repeat” background_blend=”normal” allow_player_pause=”off” background_video_pause_outside_viewport=”on” text_shadow_style=”none” module_text_shadow_style=”none” box_shadow_style=”none” /][dede_card_item _builder_version=”3.14″ title=”#1 card title” desc=”#1 card desc” src=”http://localhost:8888/wp-content/uploads/2018/09/14079861_1202408189819777_4296465020285869305_n.jpg” use_background_color_gradient=”off” background_color_gradient_start=”#2b87da” background_color_gradient_end=”#29c4a9″ background_color_gradient_type=”linear” background_color_gradient_direction=”180deg” background_color_gradient_direction_radial=”center” background_color_gradient_start_position=”0%” background_color_gradient_end_position=”100%” background_color_gradient_overlays_image=”off” parallax=”off” parallax_method=”on” background_size=”cover” background_position=”center” background_repeat=”no-repeat” background_blend=”normal” allow_player_pause=”off” background_video_pause_outside_viewport=”on” text_shadow_style=”none” module_text_shadow_style=”none” box_shadow_style=”none” /][dede_card_item _builder_version=”3.14″ title=”#1 card title” desc=”#1 card desc” src=”http://localhost:8888/wp-content/uploads/2018/09/14079861_1202408189819777_4296465020285869305_n.jpg” use_background_color_gradient=”off” background_color_gradient_start=”#2b87da” background_color_gradient_end=”#29c4a9″ background_color_gradient_type=”linear” background_color_gradient_direction=”180deg” background_color_gradient_direction_radial=”center” background_color_gradient_start_position=”0%” background_color_gradient_end_position=”100%” background_color_gradient_overlays_image=”off” parallax=”off” parallax_method=”on” background_size=”cover” background_position=”center” background_repeat=”no-repeat” background_blend=”normal” allow_player_pause=”off” background_video_pause_outside_viewport=”on” text_shadow_style=”none” module_text_shadow_style=”none” box_shadow_style=”none” /][dede_card_item _builder_version=”3.14″ title=”#1 card title” desc=”#1 card desc” src=”http://localhost:8888/wp-content/uploads/2018/09/14079861_1202408189819777_4296465020285869305_n.jpg” use_background_color_gradient=”off” background_color_gradient_start=”#2b87da” background_color_gradient_end=”#29c4a9″ background_color_gradient_type=”linear” background_color_gradient_direction=”180deg” background_color_gradient_direction_radial=”center” background_color_gradient_start_position=”0%” background_color_gradient_end_position=”100%” background_color_gradient_overlays_image=”off” parallax=”off” parallax_method=”on” background_size=”cover” background_position=”center” background_repeat=”no-repeat” background_blend=”normal” allow_player_pause=”off” background_video_pause_outside_viewport=”on” text_shadow_style=”none” module_text_shadow_style=”none” box_shadow_style=”none” /]
You just have to leave out the line $this->no_render = true; and then implement the function render in the child Module.
I try to create the same module to add child items inside module. But I have another problem, when I click add child item I get the empty popup ((( like this https://prnt.sc/156jfev. I tried the same code as author
it's working for me you can try it,
Parents modules::
<?php
class GRDI_Barchart extends ET_Builder_Module {
public $slug = 'grdi_bar_chart';
public $vb_support = 'on';
protected $module_credits = array(
'module_uri' => '',
'author' => '',
'author_uri' => '',
);
public function init() {
$this->name = esc_html__( 'Bar Chart', 'grdi-graphina-divi' );
$this->plural = esc_html__( 'Bar Chart', 'et_builder' );
$this->slug = 'grdi_bar_chart';
$this->vb_support = 'on';
$this->child_slug = 'grdi_bar_chart_child';
$this->settings_modal_toggles = array(
'general' => array(
'toggles' => array(
'main_content' => esc_html__( 'Graphina Text', 'dsm-supreme-modules-for-divi' ),
),
),
'advanced' => array(
'toggles' => array(
'separator' => array(
'title' => esc_html__( 'Separator', 'dsm-supreme-modules-for-divi' ),
'priority' => 70,
),
'image' => array(
'title' => esc_html__( 'Image', 'dsm-supreme-modules-for-divi' ),
'priority' => 69,
),
),
),
);
}
public function get_fields() {
return array(
'title' => array(
'label' => esc_html__( 'Title', 'grdi-graphina-divi' ),
'type' => 'text',
'option_category' => 'basic_option',
'description' => esc_html__( 'Input your desired heading here.', 'simp-simple-extension' ),
'toggle_slug' => 'main_content',
),
'content' => array(
'label' => esc_html__( 'Description', 'grdi-graphina-divi' ),
'type' => 'tiny_mce',
'option_category' => 'basic_option',
'description' => esc_html__( 'Content entered here will appear inside the module.', 'grdi-graphina-divi' ),
'toggle_slug' => 'main_content',
),
);
}
public function get_advanced_fields_config() {
return array();
}
public function get_charts(){
echo "charts";
}
public function render( $attrs, $content = null, $render_slug ) {
wp_enqueue_script( 'graphina_apex_chart_script' );
wp_enqueue_style( 'graphina_apex_chart_style' );
wp_enqueue_style( 'graphina-public' );
echo "<pre/>";
$var = "<script>
var options = {
series: [{
name: 'Sales',
data: [4, 3, 10, 9, 29, 19, 22, 9, 12, 7, 19, 5, 13, 9, 17, 2, 7, 5]
}],
chart: {
type: 'bar',
height: 350
},
plotOptions: {
bar: {
borderRadius: 4,
horizontal: true,
}
},
dataLabels: {
enabled: false
},
xaxis: {
categories: ['South Korea', 'Canada', 'United Kingdom', 'Netherlands', 'Italy', 'France', 'Japan',
'United States', 'China', 'Germany'
],
}
};
var chart = new ApexCharts(document.querySelector('#charts'), options);
chart.render();
</script>" ;
$html = sprintf(
'<h1>%1$s</h1>
<p>%2$s</p>
<div id="charts"></div>',
$this->props['title'],
$this->props['content']
);
echo $html ;
echo $var ;
}
}
new GRDI_Barchart;
Child Modules::
<?php
class GRDI_BarchartChild extends ET_Builder_Module {
function init() {
$this->name = esc_html__( 'Bar Chart Items', 'et_builder' );
$this->plural = esc_html__( 'Bar Chart Items', 'et_builder' );
$this->slug = 'grdi_bar_chart_child';
$this->vb_support = 'on';
$this->type = 'child';
$this->child_title_var = 'title';
$this->no_render = true;
$this->settings_text = esc_html__( 'Bar Chart Settings', 'et_builder' );
$this->settings_modal_toggles = array(
'general' => array(
'toggles' => array(
'main_content' => et_builder_i18n( 'Text' ),
),
),
'advanced' => array(
'toggles' => array(
'icon' => esc_html__( 'Icon', 'et_builder' ),
'toggle_layout' => esc_html__( 'Toggle', 'et_builder' ),
'text' => array(
'title' => et_builder_i18n( 'Text' ),
'priority' => 49,
),
),
),
);
}
function get_fields() {
$fields = array(
'child_title' => array(
'label' => et_builder_i18n( 'Data Label' ),
'type' => 'text',
'option_category' => 'basic_option',
'description' => esc_html__( 'Data Label', 'et_builder' ),
'toggle_slug' => 'main_content',
'dynamic_content' => 'text',
'hover' => 'tabs',
'mobile_options' => true,
),
'child_content' => array(
'label' => et_builder_i18n( 'Data Value' ),
'type' => 'text',
'option_category' => 'basic_option',
'description' => esc_html__( 'Data Value', 'et_builder' ),
'toggle_slug' => 'main_content',
'dynamic_content' => 'text',
'hover' => 'tabs',
'mobile_options' => true,
),
);
return $fields;
}
function render($attrs, $content, $render_slug){
$output = sprintf(
'<h1>%1$s</h1>
<p>%2$s</p>',
$this->props['child_title'],
$this->props['child_content']
);
return $output;
}
}
if ( et_builder_should_load_all_module_data() ) {
new GRDI_BarchartChild();
}
I was having similar issue to Mikael, where the child's setting looked like:
Just in case anyone is having similar issue, i realised its because the child's module wasn't being loaded. Since i was using a custom extension, there was a file called loader.phpin my extension that was responsible for loading the module files and it was loading the parent module file but skipping the child because of the pesky if statement with preg_match.
$module_files = glob( __DIR__ . '/modules/**/*.php' );
foreach ( (array) $module_files as $module_file ) {
if ( $module_file && preg_match( "/\/modules\/\b([^\/]+)\/\\1\.php$/", $module_file ) ) {
require_once $module_file;
}
}
if you replace it with:
foreach ( (array) $module_files as $module_file ) {
// if ( $module_file && preg_match( "/\/modules\/\b([^\/]+)\/\\1\.php$/", $module_file ) ) {
require_once $module_file;
//}
}
then it worked for me :)
Really hope that saves someone the 3 hours of debugging i spent.

fetch all row values with same user id in php?

I am working on wordpress website and i just stuck over one sql query.
I need to get the values from database table which contain same user id.
here is my table:
And when i run select query in mysql it is fetching result correct:
But i dont know how to get this result in my php template.
this is my query i am using in php:
$current_user = wp_get_current_user();
$id=$current_user->ID;
$skill1 =mysql_query("SELECT Skills FROM wp_candidate_skills WHERE user_id='{$id}' ");
I cant use get->result because it is showing error that get->result must contain object. Something like this error is.
This is what i am using after Mysql_query:
$arr = mysql_fetch_array($skill1);
print_r($arr);
And the Output is:
Wordpress code:
public static function init_fields() {
if ( self::$fields )
return;
$current_user = wp_get_current_user();
$user = $current_user->user_login;
$useremail = $current_user->user_email;
$id=$current_user->ID;
$skill1 =mysql_query("SELECT ID,user_id,Skills FROM wp_candidate_skills WHERE user_id=$id");
$skill_note = mysql_fetch_array($skill1);
$result = mysql_query("SELECT * FROM wp_candidate_details WHERE user_id='{$id}' LIMIT 1");
$expe_note = mysql_fetch_assoc($result);
$job_title = $expe_note['job-cat1'];
$user_loc = $expe_note['location_one_id'];
$row_num = mysql_query("SELECT COUNT(Skills) FROM wp_candidate_skills WHERE user_id='{$id}' " );
$row_cnt = mysql_fetch_array($row_num);
$arr = mysql_fetch_array($skill1);
print_r($arr);
mysql_free_result($skill1);
$skill_area = $skill_note['Skills'];
self::$fields = apply_filters( 'submit_resume_form_fields', array(
'resume_fields' => array(
'candidate_name' => array(
'label' => __( 'Your name', 'wp-job-manager-resumes' ),
'type' => 'text',
'required' => true,
'placeholder' => __( 'Your full name', 'wp-job-manager-resumes' ),
'priority' => 1,
'value' => $user
),
'candidate_email' => array(
'label' => __( 'Your email', 'wp-job-manager-resumes' ),
'type' => 'text',
'required' => true,
'placeholder' => __( 'you#yourdomain.com', 'wp-job-manager-resumes' ),
'priority' => 2,
'value' => $useremail
),
'candidate_title' => array(
'label' => __( 'Professional title', 'wp-job-manager-resumes' ),
'type' => 'text',
'required' => true,
'placeholder' => __( 'e.g. "Web Developer"', 'wp-job-manager-resumes' ),
'priority' => 3,
'value' => $job_title
),
'candidate_location' => array(
'label' => __( 'Location', 'wp-job-manager-resumes' ),
'type' => 'text',
'required' => true,
'placeholder' => __( 'e.g. "London, UK", "New York", "Houston, TX"', 'wp-job-manager-resumes' ),
'priority' => 4,
'value' => $user_loc
),
'resume_skills' => array(
'label' => __( 'Skills', 'wp-job-manager-resumes' ),
'type' => 'text',
'required' => false,
'placeholder' => __( 'Comma separate a list of relevant skills', 'wp-job-manager-resumes' ),
'priority' => 9,
'value' => $skill_area
),
'links' => array(
'label' => __( 'URL(s)', 'wp-job-manager-resumes' ),
'type' => 'links',
'required' => false,
'placeholder' => '',
'description' => __( 'Optionally provide links to any of your websites or social network profiles.', 'wp-job-manager-resumes' ),
'priority' => 10,
'fields' => array(
'name' => array(
'label' => __( 'Name', 'wp-job-manager-resumes' ),
'type' => 'text',
'required' => true,
'placeholder' => '',
'priority' => 1
),
'url' => array(
'label' => __( 'URL', 'wp-job-manager-resumes' ),
'type' => 'text',
'required' => true,
'placeholder' => 'http://',
'priority' => 2
)
)
),
'candidate_education' => array(
'label' => __( 'Education', 'wp-job-manager-resumes' ),
'type' => 'education',
'required' => false,
'placeholder' => '',
'priority' => 11,
'fields' => array(
'location' => array(
'label' => __( 'School name', 'wp-job-manager-resumes' ),
'type' => 'text',
'required' => true,
'placeholder' => ''
),
'qualification' => array(
'label' => __( 'Qualification(s)', 'wp-job-manager-resumes' ),
'type' => 'text',
'required' => true,
'placeholder' => ''
),
'date' => array(
'label' => __( 'Start/end date', 'wp-job-manager-resumes' ),
'type' => 'text',
'required' => true,
'placeholder' => ''
),
'notes' => array(
'label' => __( 'Notes', 'wp-job-manager-resumes' ),
'type' => 'textarea',
'required' => false,
'placeholder' => ''
)
)
),
'candidate_experience' => array(
'label' => __( 'Experience', 'wp-job-manager-resumes' ),
'type' => 'experience',
'required' => false,
'placeholder' => '',
'priority' => 12,
'fields' => array(
'employer' => array(
'label' => __( 'Employer', 'wp-job-manager-resumes' ),
'type' => 'text',
'required' => true,
'placeholder' => ''
),
'job_title' => array(
'label' => __( 'Job Title', 'wp-job-manager-resumes' ),
'type' => 'text',
'required' => true,
'placeholder' => ''
),
'date' => array(
'label' => __( 'Start/end date', 'wp-job-manager-resumes' ),
'type' => 'text',
'required' => true,
'placeholder' => ''
),
'notes' => array(
'label' => __( 'Notes', 'wp-job-manager-resumes' ),
'type' => 'textarea',
'required' => false,
'placeholder' => ''
)
)
),
'resume_file' => array(
'label' => __( 'Resume file', 'wp-job-manager-resumes' ),
'type' => 'file',
'required' => false,
'description' => sprintf( __( 'Optionally upload your resume for employers to view. Max. file size: %s.', 'wp-job-manager-resumes' ), size_format( wp_max_upload_size() ) ),
'priority' => 13,
'placeholder' => ''
),
)
) );
if ( ! get_option( 'resume_manager_enable_resume_upload' ) )
unset( self::$fields['resume_fields']['resume_file'] );
if ( ! get_option( 'resume_manager_enable_categories' ) || wp_count_terms( 'resume_category' ) == 0 )
unset( self::$fields['resume_fields']['resume_category'] );
if ( ! get_option( 'resume_manager_enable_skills' ) )
unset( self::$fields['resume_fields']['resume_skills'] );
}
Help me out guys! Thanks
Try this code, `$skill1 = $wpdb->get_results("SELECT ID,user_id,Skills FROM wp_candidate_skills WHERE user_id='$id'");
$count = count($skill1);
$array = array();
foreach ($skill1 as $key => $value) {
$sk = $value->Skills;
$array[] = $sk;
}
$fixId =array();
$fixId[0] ='';
for($i=0;$i<=$count-1;$i++)
{
$temp = $array[$i];
$fixId[0] = $fixId[0].$temp.',';
}
$data = $fixId[0] ;
$words = array_filter(preg_split('/\s*,\s*/', $data));
$temp = implode(', ', $words);`
It will fetch data as an array with comma inserted between two values.
Use get_results like below:
<?php
global $wpdb; //important
$result = $wpdb->get_results("SELECT Skills FROM wp_candidate_skills WHERE user_id='$id' ");
foreach($result as $row)
{
echo $row->Skills."<br>";
}
Display mysql table data in a wp page
Use the code like below:
public static function init_fields() {
if ( self::$fields )
return;
global $wpdb;
$current_user = wp_get_current_user();
$user = $current_user->user_login;
$useremail = $current_user->user_email;
$id=$current_user->ID;
$result = $wpdb->get_results("SELECT ID,user_id,Skills FROM wp_candidate_skills WHERE user_id=$id");
print_r($result);
die();
}
Try this way
$current_user = wp_get_current_user();
$id=$current_user->ID;
$skill1 =mysql_query("SELECT Skills FROM wp_candidate_skills WHERE user_id='".$id."' ");
$arr = mysql_fetch_array($skill1);
print_r($arr);

Wordpress metabox not showing up

I need a Webinar in my wordpress site. So I'm registering an meta box for my custom post type. I can see my custom post in the wordpress admin panel, but inside the custom post meta box is missing. What is wrong here!
// Meta boxes
add_filter( 'rwmb_meta_boxes', 'navbig_webinars_register_meta_boxes' );
//Register meta boxes
function navbig_webinars_register_meta_boxes( $meta_boxes )
{
$prefix = 'navbig_webinars_';
$meta_boxes[] = array(
'id' => 'standard',
'title' => __( 'Webinar Data', 'rwmb' ),
'pages' => array( 'webinar' ),
'context' => 'normal',
'priority' => 'high',
'autosave' => true,
'fields' => array(
array(
'name' => __( 'Date of Webinar', 'rwmb' ),
'id' => "webinar_date",
'type' => 'date',
// jQuery date picker options. See here http://api.jqueryui.com/datepicker
'js_options' => array(
'dateFormat' => __( 'dd-MM-yy', 'rwmb' ),
'changeMonth' => true,
'changeYear' => true,
'showButtonPanel' => true,
),
),
array(
'name' => __( 'Location', 'rwmb' ),
'id' => "webinar-location",
'type' => 'text',
'std' => __( 'Default text value', 'rwmb' ),
),
array(
'name' => __( 'Time Of Webinar', 'rwmb' ),
'id' => 'webinar_time',
'type' => 'time',
'js_options' => array(
'stepMinute' => 5,
'showSecond' => true,
'stepSecond' => 10,
),
),
array(
'name' => __( 'Select Time Zone', 'rwmb' ),
'id' => "select-timezone",
'type' => 'select_advanced',
'options' => array(
'value1' => __( 'PST', 'rwmb' ),
'value2' => __( 'EST', 'rwmb' ),
),
'multiple' => false,
'placeholder' => __( 'Select an Coures Type', 'rwmb' ),
),
// URL
array(
'name' => __( 'Webinar URL', 'rwmb' ),
'id' => "webinar_url",
'type' => 'url',
'std' => 'http://google.com',
),
array(
'name' => __( 'Webinar Banner', 'rwmb' ),
'id' => "webinar_banner",
'type' => 'thickbox_image',
),
),
);
return $meta_boxes;
}
You can find more detail about meta boxes here.
Here is sample add_meta_box code:-
add_meta_box(
'some_meta_box_name'
,__( 'Some Meta Box Headline', 'plugin_textdomain' )
,'render_meta_box_content' //this is callback function.
,'post_type'
,'advanced'
,'high'
);

Categories