I am developing an extension for woocommerce WordPress plugin.
I would like to display the currency symbol outside of the loop in a custom function
I have the following:
function my_function( ) {
global $woocommerce;
echo get_woocommerce_currency_symbol();
}
I am not sure why but this doesn't output the symbol? Am I missing something?
Your code should work, which means the issue might be in the database. You can check these 2 functions:get_woocommerce_currency() and get_woocommerce_currency_symbol()
from the WooCommerce docs that shows that you are using the functions correct.
What is left is for you to start some troubleshooting steps to see what causes the error:
What is get_option('woocommerce_currency') returning? If nothing, then you have no currency set and that is why you get nothing from get_woocommerce_currency_symbol();
What happens if you add a currency as a parameter to get_woocommerce_currency_symbol? it gets displayed? something like echo get_woocommerce_currency_symbol("USD");
You should add to your script some error handling lines, to inform the user that he needs to have the currency set before using your extension.
use this to show price with currency
$product->get_price_html();
OR use
echo get_woocommerce_currency_symbol();
to show the currency only
You should always price under wc_price function, it will automatically add currency symbol along with the default span of woo commerce
For ex..
$product_price = 60;
echo wc_price($product_price);
get_option('woocommerce_currency')
will help you lot.
function get_woocommerce_currency_symbol( $currency = '' ) {
if ( ! $currency ) {
$currency = get_woocommerce_currency();
}
$symbols = apply_filters( 'woocommerce_currency_symbols', array(
'AED' => 'د.إ',
'AFN' => '؋',
'ALL' => 'L',
'AMD' => 'AMD',
'ANG' => 'ƒ',
'AOA' => 'Kz',
'ARS' => '$',
'AUD' => '$',
'AWG' => 'ƒ',
'AZN' => 'AZN',
'BAM' => 'KM',
'BBD' => '$',
'BDT' => '৳ ',
'BGN' => 'лв.',
'BHD' => '.د.ب',
'BIF' => 'Fr',
'BMD' => '$',
'BND' => '$',
'BOB' => 'Bs.',
'BRL' => 'R$',
'BSD' => '$',
'BTC' => '฿',
'BTN' => 'Nu.',
'BWP' => 'P',
'BYR' => 'Br',
'BZD' => '$',
'CAD' => '$',
'CDF' => 'Fr',
'CHF' => 'CHF',
'CLP' => '$',
'CNY' => '¥',
'COP' => '$',
'CRC' => '₡',
'CUC' => '$',
'CUP' => '$',
'CVE' => '$',
'CZK' => 'Kč',
'DJF' => 'Fr',
'DKK' => 'DKK',
'DOP' => 'RD$',
'DZD' => 'د.ج',
'EGP' => 'EGP',
'ERN' => 'Nfk',
'ETB' => 'Br',
'EUR' => '€',
'FJD' => '$',
'FKP' => '£',
'GBP' => '£',
'GEL' => 'ლ',
'GGP' => '£',
'GHS' => '₵',
'GIP' => '£',
'GMD' => 'D',
'GNF' => 'Fr',
'GTQ' => 'Q',
'GYD' => '$',
'HKD' => '$',
'HNL' => 'L',
'HRK' => 'Kn',
'HTG' => 'G',
'HUF' => 'Ft',
'IDR' => 'Rp',
'ILS' => '₪',
'IMP' => '£',
'INR' => '₹',
'IQD' => 'ع.د',
'IRR' => '﷼',
'IRT' => 'تومان',
'ISK' => 'kr.',
'JEP' => '£',
'JMD' => '$',
'JOD' => 'د.ا',
'JPY' => '¥',
'KES' => 'KSh',
'KGS' => 'сом',
'KHR' => '៛',
'KMF' => 'Fr',
'KPW' => '₩',
'KRW' => '₩',
'KWD' => 'د.ك',
'KYD' => '$',
'KZT' => 'KZT',
'LAK' => '₭',
'LBP' => 'ل.ل',
'LKR' => 'රු',
'LRD' => '$',
'LSL' => 'L',
'LYD' => 'ل.د',
'MAD' => 'د.م.',
'MDL' => 'MDL',
'MGA' => 'Ar',
'MKD' => 'ден',
'MMK' => 'Ks',
'MNT' => '₮',
'MOP' => 'P',
'MRO' => 'UM',
'MUR' => '₨',
'MVR' => '.ރ',
'MWK' => 'MK',
'MXN' => '$',
'MYR' => 'RM',
'MZN' => 'MT',
'NAD' => '$',
'NGN' => '₦',
'NIO' => 'C$',
'NOK' => 'kr',
'NPR' => '₨',
'NZD' => '$',
'OMR' => 'ر.ع.',
'PAB' => 'B/.',
'PEN' => 'S/.',
'PGK' => 'K',
'PHP' => '₱',
'PKR' => '₨',
'PLN' => 'zł',
'PRB' => 'р.',
'PYG' => '₲',
'QAR' => 'ر.ق',
'RMB' => '¥',
'RON' => 'lei',
'RSD' => 'дин.',
'RUB' => '₽',
'RWF' => 'Fr',
'SAR' => 'ر.س',
'SBD' => '$',
'SCR' => '₨',
'SDG' => 'ج.س.',
'SEK' => 'kr',
'SGD' => '$',
'SHP' => '£',
'SLL' => 'Le',
'SOS' => 'Sh',
'SRD' => '$',
'SSP' => '£',
'STD' => 'Db',
'SYP' => 'ل.س',
'SZL' => 'L',
'THB' => '฿',
'TJS' => 'ЅМ',
'TMT' => 'm',
'TND' => 'د.ت',
'TOP' => 'T$',
'TRY' => '₺',
'TTD' => '$',
'TWD' => 'NT$',
'TZS' => 'Sh',
'UAH' => '₴',
'UGX' => 'UGX',
'USD' => '$',
'UYU' => '$',
'UZS' => 'UZS',
'VEF' => 'Bs F',
'VND' => '₫',
'VUV' => 'Vt',
'WST' => 'T',
'XAF' => 'Fr',
'XCD' => '$',
'XOF' => 'Fr',
'XPF' => 'Fr',
'YER' => '﷼',
'ZAR' => 'R',
'ZMW' => 'ZK',
) );
$currency_symbol = isset( $symbols[ $currency ] ) ? $symbols[ $currency ] : '';
return apply_filters( 'woocommerce_currency_symbol', $currency_symbol, $currency );
}
[1]: http://woocommerce.wp-a2z.org/oik_api/get_woocommerce_currency_symbol/
You want to show the WooCommerce curency symbol in your Custom WordPress Theme or Plugin but don’t know how? Worry no more. Here is all you need to know. For Custom WooCommerce WordPress Plugin:
function show_currency_symbol( ) {
global $woocommerce;
echo get_woocommerce_currency_symbol();
}
For Custom WooCommerce WordPress Theme:
<?php global $woocommerce; echo get_woocommerce_currency_symbol(); ?>
This short code uses the woocommerce_price function; it will automatically add currency symbol along with the default span of woo commerce.
/* Custom price */
$price = 100;
echo woocommerce_price($price);
You can change the symbol in you ftp files by going to WP-Content > Plugins > Woocommerce > Includes > WC.Core.Functions.php it starts on line 662 for me.
Related
Could anyone guide me in how to add an ACF field in a function please. Everything I do returns nothing.
add_action('woocommerce_after_main_content', 'custom_seo_block', 10);
function custom_seo_block() {
return "<?php the_field('seo_content'); ?>";
}
Thanks in advance
add_action('woocommerce_after_main_content', 'custom_seo_block', 10);
function custom_seo_block() {
$value = get_field( "seo_content", $post_id );
return $value;
}
Get ACF field value by post id and than return it.
It is still not returning the data. Sorry if this is a silly question. Obviously doing something wrong here.
add_action('woocommerce_after_main_content', 'custom_seo_block', 10);
function custom_seo_block() {
if( function_exists('acf_add_local_field_group') ):
acf_add_local_field_group(array(
'key' => 'group_5c52deb4b5fe0',
'title' => 'SEO content',
'fields' => array(
array(
'key' => 'field_5c52dec5d99a4',
'label' => 'SEO content',
'name' => 'seo_content',
'type' => 'wysiwyg',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'tabs' => 'all',
'toolbar' => 'full',
'media_upload' => 1,
'delay' => 0,
),
),
'location' => array(
array(
array(
'param' => 'taxonomy',
'operator' => '==',
'value' => 'all',
),
),
),
'menu_order' => 5,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => 1,
'description' => '',
));
endif;
}
add_action('woocommerce_after_main_content', 'custom_seo_block', 10);
function custom_seo_block() {
global $post;
the_field('seo_content',$post->ID);
}
I have a own option-page in the backend-sidebar with this repeatable fields in it. (not a metabox) I cant figure out, how to display the data from the group field in the frontend.
function ww_register_theme_options() {
$ww_prefix = '_ww_';
$ww_contacts = new_cmb2_box( array(
'id' => $ww_prefix . 'ww_option_plugin',
'title' => esc_html__( 'Ansprechpartner', 'ww-contact' ),
'icon_url' => '/wp-content/plugins/ww-contact/assets/images/icons/ww-icon-white.png',
'object_types' => array( 'options-page' ),
'option_key' => 'ww_options',
) );
$ww_group_field = $ww_contacts->add_field(array(
'id' => $ww_prefix . 'contact_repeat_group',
'type' => 'group',
'description' => __('Ansprechparter Liste', 'ww-contact'),
'repeatable' => true,
'options' => array(
'group_title' => 'Ansprechpartner {#}',
'add_button' => __('neuer Ansprechpartner', 'ww-contact'),
'remove_button' => __('Entfernen', 'ww-contact'),
'sortable' => true,
),
));
$ww_contacts->add_group_field($ww_group_field, array(
'name' => 'Vorname',
'id' => $ww_prefix . 'forname',
'type' => 'text',
));
$ww_contacts->add_group_field($ww_group_field, array(
'name' => 'Vorname',
'id' => $ww_prefix . 'surname',
'type' => 'text',
));
}
Remove the prefix from your add_group_field ids they aren't needed. Then loop through like so...
$entries = get_post_meta( $parent, 'ww_contact_repeat_group', true );
if($entries){
foreach ( (array) $entries as $key => $entry ) {
if ( isset( $entry['forname'] ) ) {
$forname = $entry['forname'];
}
}
}
I put this code into my functions.php
include_once('advanced-custom-fields/acf.php');
include_once('acf-options-page/acf-options-page.php');
include_once('acf-repeater/acf-repeater.php');
define( 'ACF_LITE', true );
if(function_exists("register_field_group"))
{
register_field_group(array (
'id' => 'acf_options',
'title' => 'Options',
'fields' => array (
array (
'key' => 'field_525d1b6d49043',
'label' => 'lable',
'name' => 'homepage',
'type' => 'repeater',
'instructions' => 'Select which categories you want to display on the homepage.',
'sub_fields' => array (
array (
'key' => 'field_525d1b8a49044',
'label' => 'Category',
'name' => 'firsttext',
'type' => 'text',
'column_width' => '',
'field_type' => 'text',
'return_format' => 'id',
),
array (
'key' => 'field_525d2473de72c',
'label' => 'Number of Posts',
'name' => 'number-of-posts',
'type' => 'text',
'column_width' => '',
'default_value' => 4,
'placeholder' => '',
'prepend' => '',
'append' => '',
'min' => 2,
'max' => '',
'step' => 2,
),
),
'row_min' => 1,
'row_limit' => 1,
'layout' => 'row',
'button_label' => 'Add a category',
),
),
'location' => array (
array (
array (
'param' => 'options_page',
'operator' => '==',
'value' => 'acf-options',
'order_no' => 0,
'group_no' => 0,
),
),
),
'options' => array (
'position' => 'normal',
'layout' => 'no_box',
'hide_on_screen' => array (
),
),
'menu_order' => 0,
));
}
Then I enter values into fields in admin console
How I cat get this value from page file?
I try get_field('acf_options) and I try get_field('homepage), but this return null
Im not sure why you would want to put this in your function.php file instead of just do this this through the plugin.
But if you want to retrieve those field values, you should do this: get_field( 'homepage', 'option' );
instead of:
get_field( 'homepage' ) or get_field( 'acf_options' );
My best advice would be to look on the ACF website for information. These docs help out a lot!
I want to be use some sliders in my wordpress theme. I want to select them in my theme option. I am using the code below.
<?php
$slider_select = get_option_tree( 'slider_select', '', 'true' );
?>
<?php get_template_part('$slider_select'); ?>
But, it is not working. I want the get_template_part code worked. Any suggestion?
replace
get_template_part('$slider_select');
with
get_template_part($slider_select);
You can use this method for create a slider in option tree.
create settings array like this.
array(
'id' => 'my_slider',
'label' => 'Images',
'desc' => '',
'std' => '',
'type' => 'list-item',
'section' => 'general',
'class' => '',
'choices' => array(),
'settings' => array(
array(
'id' => 'slider_image',
'label' => 'Image',
'desc' => '',
'std' => '',
'type' => 'upload',
'class' => '',
'choices' => array()
),
array(
'id' => 'slider_link',
'label' => 'Link to Post',
'desc' => 'Enter the posts url.',
'std' => '',
'type' => 'text',
'class' => '',
'choices' => array()
),
array(
'id' => 'slider_description',
'label' => 'Description',
'desc' => 'This text is used to add fancy captions in the slider.',
'std' => '',
'type' => 'textarea',
'class' => '',
'choices' => array()
)
)
)
Add into page using loop
$my_slider = ot_get_option( 'my_slider' );
foreach($my_slider as $slider){
echo '<img src="'.$slider["slider_image"].'">';
echo $slider["slider_link"];
echo $slider["slider_description"];
}
I want to create a module in which, when I install that module, it should create custom table defined in homepage_blocks_schema(), create content-type defined in homepage_blocks_install() and in that content type, create custom fields same as my schema.
Here is function,
function homepage_blocks_schema() {
$schema['homepage_blocks'] = array(
'fields' => array(
'hid' => array(
'type' => 'int',
'length' => 11,
'unsigned' => TRUE,
'not null' => TRUE,
),
'title' => array(
'type' => 'varchar',
'length' => 255,
),
'image' => array(
'type' => 'varchar',
'length' => 255,
),
),
'primary key' => array('hid'),
);
return $schema;
}
homepage_blocks_install() {
$homepage_blocks = array(
'type' => 'homepage_blocks',
'name' => $t('Homepage Blocks'),
//'base' => 'node_content',
'base' => 'homepage_blocks',
'custom' => true,
'modified' => true,
'locked' => false,
'title_label' => $t('Homepage Blocks')
);
$content_type = node_type_set_defaults($homepage_blocks);
//node_add_body_field($content_type, 'Body'); // add the body field to the content type
node_type_save($content_type); // create the content type
variable_set('node_options_homepage_blocks', array('status'));
variable_set('comment_homepage_blocks', 'COMMENT_NODE_HIDDEN'); // hide comments for this node.
variable_set('node_submitted_homepage_blocks', false); // Hide date and author information
//drupal_install_schema('homepage_blocks');
foreach (_homepage_blocks_installed_fields() as $field) { // Create all the fields we are adding to our content type.
//krumo(field_create_field($field));
field_create_field($field);
}
foreach (_homepage_blocks_installed_instances() as $instance) { // Create all the instances for our fields.
$instance['entity_type'] = 'node';
$instance['bundle'] = 'homepage_blocks';
field_create_instance($instance);
}
}
function _homepage_blocks_installed_fields() {
$t = get_t();
$fields = array(
'hmblock_title' => array(
'field_name' => 'hmblock_title',
'label' => $t('Title'),
//'cardinality' => 1,
'type' => 'text',
'settings' => array(
'max_length' => 255,
),
),
'hmblock_image' => array(
'field_name' => 'hmblock_image',
'label' => $t('Image'),
//'cardinality' => 1,
'type' => 'image',
'settings' => array(
'default_image' => 0,
'uri_scheme' => 'public',
),
),
);
//$fields = drupal_get_schema('homepage_blocks');
return $fields;
}
function _homepage_blocks_installed_instances() {
$t = get_t();
$instances = array(
'hmblock_title' => array(
'field_name' => 'hmblock_title',
'label' => $t('Title'),
'cardinality' => 1,
'widget' => array(
'type' => 'text_textfield',
'settings' => array('size' => 255),
),
),
'hmblock_image' => array(
'field_name' => 'hmblock_image',
'label' => $t('Image'),
'cardinality' => 1,
'type' => 'image',
'settings' => array(
'alt_field' => 1,
'file_directory' => 'image',
'file_extensions' => 'png gif jpg jpeg',
'max_filesize' => '50mb',
'max_resolution' => '',
'min_resolution' => '',
'title_field' => 1,
'user_register_form' => FALSE,
),
'widget' => array(
'settings' => array(
'preview_image_style' => 'thumbnail',
'progress_indicator' => 'throbber',
),
)
),
);
return $instances;
}
This code creates table and content type but not its fields..
Can anyone help ?
There is an easier way
you can create your custom type using drupal core and fields module and then export it into a module using Features module.
You can find a full guide here : Bundling site settings using Features