Ajax add to cart not updating cart content untill after refresh - wordpress

The ajax add to cart function of woocommerce doesn't seem to update the cart untill after I manually refresh the website.
This is the function for the cart widget I edited, and it seems to be working fine. It just doesn't refresh automatically.
class GeminiCart extends WP_Widget {
public function __construct() {
parent::__construct(
'gb_woocommerce_dropdown_cart',
esc_html__('Gemini Bracelet Woocommerce Dropdown Cart', 'depot'),
array( 'description' => esc_html__( 'Display a shop cart icon with a dropdown that shows products that are in the cart', 'depot' ), )
);
$this->setParams();
}
protected function setParams() {
$this->params = array(
array(
'type' => 'textfield',
'name' => 'woocommerce_dropdown_cart_margin',
'title' => esc_html__('Icon Margin', 'depot'),
'description' => esc_html__('Insert margin in format: top right bottom left (e.g. 10px 5px 10px 5px)', 'depot')
),
array(
'type' => 'dropdown',
'name' => 'woocommerce_enable_cart_info',
'title' => esc_html__('Enable Cart Info', 'depot'),
'options' => depot_mikado_get_yes_no_select_array(false),
'description' => esc_html__('Enabling this option will show cart info (products number and price) at the right side of dropdown cart icon', 'depot')
),
);
}
/**
* Generate widget form based on $params attribute
*
* #param array $instance
*
* #return null
*/
public function form($instance) {
if(is_array($this->params) && count($this->params)) {
foreach($this->params as $param_array) {
$param_name = $param_array['name'];
${$param_name} = isset($instance[$param_name]) ? esc_attr($instance[$param_name]) : '';
}
foreach($this->params as $param) {
switch($param['type']) {
case 'textfield':
?>
<p>
<label for="<?php echo esc_attr($this->get_field_id($param['name'])); ?>"><?php echo
esc_html($param['title']); ?>:</label>
<input class="widefat" id="<?php echo esc_attr($this->get_field_id($param['name'])); ?>" name="<?php echo esc_attr($this->get_field_name($param['name'])); ?>" type="text" value="<?php echo esc_attr(${$param['name']}); ?>"/>
<?php if(!empty($param['description'])) : ?>
<span class="mkd-field-description"><?php echo esc_html($param['description']); ?></span>
<?php endif; ?>
</p>
<?php
break;
case 'dropdown':
?>
<p>
<label for="<?php echo esc_attr($this->get_field_id($param['name'])); ?>"><?php echo
esc_html($param['title']); ?>:</label>
<?php if(isset($param['options']) && is_array($param['options']) && count($param['options'])) { ?>
<select class="widefat" name="<?php echo esc_attr($this->get_field_name($param['name'])); ?>" id="<?php echo esc_attr($this->get_field_id($param['name'])); ?>">
<?php foreach($param['options'] as $param_option_key => $param_option_val) {
$option_selected = '';
if(${$param['name']} == $param_option_key) {
$option_selected = 'selected';
}
?>
<option <?php echo esc_attr($option_selected); ?> value="<?php echo esc_attr($param_option_key); ?>"><?php echo esc_attr($param_option_val); ?></option>
<?php } ?>
</select>
<?php } ?>
<?php if(!empty($param['description'])) : ?>
<span class="mkd-field-description"><?php echo esc_html($param['description']); ?></span>
<?php endif; ?>
</p>
<?php
break;
}
}
} else { ?>
<p><?php esc_html_e('There are no options for this widget.', 'depot'); ?></p>
<?php }
}
/**
* #param array $new_instance
* #param array $old_instance
*
* #return array
*/
public function update($new_instance, $old_instance) {
$instance = array();
foreach($this->params as $param) {
$param_name = $param['name'];
$instance[$param_name] = sanitize_text_field($new_instance[$param_name]);
}
return $instance;
}
public function widget( $args, $instance ) {
extract( $args );
global $woocommerce;
$icon_styles = array();
if ($instance['woocommerce_dropdown_cart_margin'] !== '') {
$icon_styles[] = 'padding: ' . $instance['woocommerce_dropdown_cart_margin'];
}
$icon_class = 'mkd-cart-info-is-disabled';
if (!empty($instance['woocommerce_enable_cart_info']) && $instance['woocommerce_enable_cart_info'] === 'yes') {
$icon_class = 'mkd-cart-info-is-active';
}
$cart_description = depot_mikado_options()->getOptionValue('mkd_woo_dropdown_cart_description');
?>
<div class="mkd-shopping-cart-holder <?php echo esc_html($icon_class); ?>" <?php depot_mikado_inline_style($icon_styles) ?>>
<div class="mkd-shopping-cart-inner">
<?php $cart_is_empty = sizeof( $woocommerce->cart->get_cart() ) <= 0; ?>
<a itemprop="url" class="mkd-header-cart" href="<?php echo esc_url($woocommerce->cart->get_cart_url()); ?>">
<span class="mkd-cart-icon-text"><?php esc_html_e('CART', 'depot'); ?></span>
<span class="mkd-cart-info">
<span class="mkd-cart-info-total">( <?php echo wp_kses($woocommerce->cart->get_cart_contents_count()); ?> )</span>
</span>
</a>
<?php if ( !$cart_is_empty ) : ?>
<div class="mkd-shopping-cart-dropdown">
<ul>
<?php foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $cart_item ) :
$_product = $cart_item['data'];
// Only display if allowed
if ( ! $_product->exists() || $cart_item['quantity'] == 0 ) {
continue;
}
// Get price
if ( version_compare( WOOCOMMERCE_VERSION, '3.0' ) >= 0 ) {
$product_price = get_option( 'woocommerce_tax_display_cart' ) == 'excl' ? wc_get_price_excluding_tax( $_product ) : wc_get_price_including_tax( $_product );
} else {
$product_price = get_option( 'woocommerce_tax_display_cart' ) == 'excl' ? $_product->get_price_excluding_tax() : $_product->get_price_including_tax();
}
?>
<li>
<div class="mkd-item-image-holder">
<a itemprop="url" href="<?php echo esc_url(get_permalink( $cart_item['product_id'] )); ?>">
<?php echo wp_kses($_product->get_image(), array(
'img' => array(
'src' => true,
'width' => true,
'height' => true,
'class' => true,
'alt' => true,
'title' => true,
'id' => true
)
)); ?>
</a>
</div>
<div class="mkd-item-info-holder">
<h5 itemprop="name" class="mkd-product-title"><a itemprop="url" href="<?php echo esc_url(get_permalink( $cart_item['product_id'] )); ?>"><?php echo apply_filters('depot_mikado_woo_widget_cart_product_title', $_product->get_title(), $_product ); ?></a></h5>
<span class="mkd-quantity"><?php echo esc_html($cart_item['quantity']); ?></span>
<?php echo apply_filters( 'depot_mikado_woo_cart_item_price_html', wc_price( $product_price ), $cart_item, $cart_item_key ); ?>
<?php echo apply_filters( 'depot_mikado_woo_cart_item_remove_link', sprintf('<span class="icon-arrows-remove"></span>', esc_url( $woocommerce->cart->get_remove_url( $cart_item_key ) ), esc_html__('Remove this item', 'depot') ), $cart_item_key ); ?>
</div>
</li>
<?php endforeach; ?>
<div class="mkd-cart-bottom">
<div class="mkd-subtotal-holder clearfix">
<span class="mkd-total"><?php esc_html_e( 'TOTAL:', 'depot' ); ?></span>
<span class="mkd-total-amount">
<?php echo wp_kses($woocommerce->cart->get_cart_subtotal(), array(
'span' => array(
'class' => true,
'id' => true
)
)); ?>
</span>
</div>
<?php if(!empty($cart_description)) { ?>
<div class="mkd-cart-description">
<div class="mkd-cart-description-inner">
<span><?php echo esc_html($cart_description); ?></span>
</div>
</div>
<?php } ?>
<div class="mkd-btn-holder clearfix">
<a itemprop="url" href="<?php echo esc_url($woocommerce->cart->get_cart_url()); ?>" class="mkd-view-cart" data-title="<?php esc_html_e('VIEW CART','depot'); ?>"><span><?php esc_html_e('VIEW CART','depot'); ?></span></a>
</div>
<div class="mkd-btn-holder clearfix">
<a itemprop="url" href="<?php echo esc_url($woocommerce->cart->get_checkout_url()); ?>" class="mkd-view-cart" data-title="<?php esc_html_e('CHECKOUT','depot'); ?>"><span><?php esc_html_e('CHECKOUT','depot'); ?></span></a>
</div>
</div>
</ul>
</div>
<?php else : ?>
<div class="mkd-shopping-cart-dropdown">
<ul>
<li class="mkd-empty-cart"><?php esc_html_e( 'No products in the cart.', 'depot' ); ?></li>
</ul>
</div>
<?php endif; ?>
</div>
</div>
<?php
}
}
I already implemented this function to make it update; but it doesn't change anything:
add_filter( 'woocommerce_add_to_cart_fragments', 'iconic_cart_count_fragments', 10, 1 );
function iconic_cart_count_fragments( $fragments ) {
$fragments['mkd-header-cart'] = '<span class="mkd-cart-icon-text"><?php esc_html_e(\'CART\', \'depot\'); ?></span>
<span class="mkd-cart-info">
<span class="mkd-cart-info-total">( <?php echo wp_kses($woocommerce->cart->get_cart_contents_count()); ?> )</span>
</span>';
return $fragments;
}
In wordpress itself I enabled the AJAX add to cart.
The add to cart button also has the class add_to_cart_button

The code that reloads those fragments can be found here: \plugins\woocommerce\assets\js\frontend\cart-fragments.js
To refresh WooCommerce widget you have to copy the code below
var $fragment_refresh = {
url: wc_cart_fragments_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'get_refreshed_fragments' ),
type: 'POST',
success: function( data ) {
if ( data && data.fragments ) {
$.each( data.fragments, function( key, value ) {
$( key ).replaceWith( value );
});
if ( $supports_html5_storage ) {
sessionStorage.setItem( wc_cart_fragments_params.fragment_name, JSON.stringify( data.fragments ) );
set_cart_hash( data.cart_hash );
if ( data.cart_hash ) {
set_cart_creation_timestamp();
}
}
$( document.body ).trigger( 'wc_fragments_refreshed' );
}
}
};
and to trigger the event you do this: $.ajax( $fragment_refresh ); OR jQuery.ajax( $fragment_refresh );

Related

Search Result shows White Blank Page wordpress

My site link is: https://staging.buellairhorns.com/
When I try the search function, the search result page shows nothing but a white blank page. Can anyone help me figure out what's wrong?
This is my: search.php
<?php
$container_class = apply_filters( 'neve_container_class_filter', 'container', 'blog-archive' );
get_header();
$wrapper_classes = [ 'posts-wrapper' ];
if ( ! neve_is_new_skin() ) {
$wrapper_classes[] = 'row';
}
?>
<div class="<?php echo esc_attr( $container_class ); ?> archive-container">
<div class="row">
<?php do_action( 'neve_do_sidebar', 'blog-archive', 'left' ); ?>
<div class="nv-index-posts search col">
<?php
do_action( 'neve_page_header', 'search' );
if ( have_posts() ) {
/* Start the Loop. */
echo '<div class="' . esc_attr( join( ' ', $wrapper_classes ) ) . '">';
while ( have_posts() ) {
the_post();
get_template_part( 'template-parts/content' );
}
echo '</div>';
if ( ! is_singular() ) {
do_action( 'neve_do_pagination', 'blog-archive' );
}
} else {
get_template_part( 'template-parts/content', 'none' );
}
?>
<div class="w-100"></div>
</div>
<?php do_action( 'neve_do_sidebar', 'blog-archive', 'right' ); ?>
</div>
</div>
<?php
get_footer();
This is my searchform.php
<?php
$form_classes = [ 'search-form' ];
$placeholder = array_key_exists( 'placeholder', $args ) ? $args['placeholder'] : __( 'Search for...', 'neve' );
if ( array_key_exists( 'additional_form_classes', $args ) && is_array( $args['additional_form_classes'] ) ) {
$form_classes = array_merge( $form_classes, $args['additional_form_classes'] );
}
$value = array_key_exists( 'value', $args ) ? $args['value'] : '';
$placeholder = apply_filters( 'nv_search_placeholder', $placeholder );
$aria_label = __( 'Search', 'neve' );
$home_url = home_url( '/' );
if ( function_exists( 'PLL' ) ) {
$pll_data = PLL();
if ( property_exists( $pll_data, 'links' ) && method_exists( $pll_data->links, 'get_home_url' ) ) {
$home_url = $pll_data->links->get_home_url( null, true );
}
}
?>
<form role="search"
method="get"
class="<?php echo esc_attr( implode( ' ', $form_classes ) ); ?>"
action="<?php echo esc_url( $home_url ); ?>">
<label>
<span class="screen-reader-text"><?php echo esc_html__( 'Search for...', 'neve' ); ?></span>
</label>
<input type="search"
class="search-field"
aria-label="<?php echo esc_attr__( 'Search', 'neve' ); ?>"
placeholder="<?php echo esc_attr( $placeholder ); ?>"
value="<?php echo esc_attr( $value ); ?>"
name="s"/>
<button type="submit"
class="search-submit"
aria-label="<?php echo esc_attr( $aria_label ); ?>">
<span class="nv-search-icon-wrap">
<?php neve_search_icon( false, true ); ?>
</span>
</button>
<?php
if ( array_key_exists( 'post_type', $args ) ) {
echo '<input type="hidden" name="post_type" value="' . esc_attr( $args['post_type'] ) . '"/>';
}
?>
</form>
I'am using neve theme. I created a custom search result page using Elementor Pro but still not working.

Display child terms if there are posts on custom taxonomy template

I have a piece of code that shows child terms of a custom taxonomy of the current parent term in the taxonomy-term.php template file. I want to display html only when there are child terms present. Now the heading 2 is displayed even when there are no child terms. Here is the code:
<?php
$term_children = get_terms('onderwerp', array( 'parent' => get_queried_object_id(),));
if ( ! is_wp_error( $terms ) || ($term_children->count != 0) ) { ?>
<section>
<h2>Ik heb een vraag over:</h2>
<div class="onderwerp-links">
<?php
foreach ( $term_children as $child ) { ?>
<?php $img = get_term_meta( $term->term_id, 'ndftm_img', true ); ?>
<a href="<?php echo esc_url( get_term_link( $child ) ) ?>" style="background-image: url(<?php echo $img; ?>)">
<span><?php echo $child->name; ?></span>
</a><?php
}
?>
</div>
</section>
<?php
}
?>
I fixed it by adding
'hide_empty' => true
in the get terms array
and making the if statement like this:
if ( ! empty($term_children) )
Here's the code:
<?php
$term_children = get_terms('onderwerp', array( 'parent' => get_queried_object_id(), 'hide_empty' => true ));
if ( ! empty($term_children) ) { ?>
<section>
<h2>Ik heb een vraag over:</h2>
<div class="onderwerp-links">
<?php
foreach ( $term_children as $child ) { ?>
<?php $img = get_term_meta( $term->term_id, 'ndftm_img', true ); ?>
<a href="<?php echo esc_url( get_term_link( $child ) ) ?>" style="background-image: url(<?php echo $img; ?>)">
<span><?php echo $child->name; ?></span>
</a><?php
}
?>
</div>
</section>
<?php
}
?>

Change header template from Divi theme

I have this template(header.php). Now i'm working on a site, and i want to to some changes in this template. According to this template i get 2 menu locations, but i want to change the order of them.
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>" />
<?php
elegant_description();
elegant_keywords();
elegant_canonical();
/**
* Fires in the head, before {#see wp_head()} is called. This action can be used to
* insert elements into the beginning of the head before any styles or scripts.
*
* #since 1.0
*/
do_action( 'et_head_meta' );
$template_directory_uri = get_template_directory_uri();
?>
<link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" />
<script type="text/javascript">
document.documentElement.className = 'js';
</script>
<?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>
<?php
$product_tour_enabled = et_builder_is_product_tour_enabled();
$page_container_style = $product_tour_enabled ? ' style="padding-top: 0px;"' : ''; ?>
<div id="page-container"<?php echo et_core_intentionally_unescaped( $page_container_style, 'fixed_string' ); ?>>
<?php
if ( $product_tour_enabled || is_page_template( 'page-template-blank.php' ) ) {
return;
}
$et_secondary_nav_items = et_divi_get_top_nav_items();
$et_phone_number = $et_secondary_nav_items->phone_number;
$et_email = $et_secondary_nav_items->email;
$et_contact_info_defined = $et_secondary_nav_items->contact_info_defined;
$show_header_social_icons = $et_secondary_nav_items->show_header_social_icons;
$et_secondary_nav = $et_secondary_nav_items->secondary_nav;
$et_top_info_defined = $et_secondary_nav_items->top_info_defined;
$et_slide_header = 'slide' === et_get_option( 'header_style', 'left' ) || 'fullscreen' === et_get_option( 'header_style', 'left' ) ? true : false;
?>
<?php if ( $et_top_info_defined && ! $et_slide_header || is_customize_preview() ) : ?>
<?php ob_start(); ?>
<div id="top-header"<?php echo $et_top_info_defined ? '' : 'style="display: none;"'; ?>>
<div class="container clearfix">
<?php if ( $et_contact_info_defined ) : ?>
<div id="et-info">
<?php if ( '' !== ( $et_phone_number = et_get_option( 'phone_number' ) ) ) : ?>
<span id="et-info-phone"><?php echo et_core_esc_previously( et_sanitize_html_input_text( $et_phone_number ) ); ?></span>
<?php endif; ?>
<?php if ( '' !== ( $et_email = et_get_option( 'header_email' ) ) ) : ?>
<span id="et-info-email"><?php echo esc_html( $et_email ); ?></span>
<?php endif; ?>
<?php
if ( true === $show_header_social_icons ) {
get_template_part( 'includes/social_icons', 'header' );
} ?>
</div> <!-- #et-info -->
<?php endif; // true === $et_contact_info_defined ?>
<div id="et-secondary-menu">
<?php
if ( ! $et_contact_info_defined && true === $show_header_social_icons ) {
get_template_part( 'includes/social_icons', 'header' );
} else if ( $et_contact_info_defined && true === $show_header_social_icons ) {
ob_start();
get_template_part( 'includes/social_icons', 'header' );
$duplicate_social_icons = ob_get_contents();
ob_end_clean();
printf(
'<div class="et_duplicate_social_icons">
%1$s
</div>',
et_core_esc_previously( $duplicate_social_icons )
);
}
if ( '' !== $et_secondary_nav ) {
echo et_core_esc_wp( $et_secondary_nav );
}
et_show_cart_total();
?>
</div> <!-- #et-secondary-menu -->
</div> <!-- .container -->
</div> <!-- #top-header -->
<?php
$top_header = ob_get_clean();
/**
* Filters the HTML output for the top header.
*
* #since 3.10
*
* #param string $top_header
*/
echo et_core_intentionally_unescaped( apply_filters( 'et_html_top_header', $top_header ), 'html' );
?>
<?php endif; // true ==== $et_top_info_defined ?>
<?php if ( $et_slide_header || is_customize_preview() ) : ?>
<?php ob_start(); ?>
<div class="et_slide_in_menu_container">
<?php if ( 'fullscreen' === et_get_option( 'header_style', 'left' ) || is_customize_preview() ) { ?>
<span class="mobile_menu_bar et_toggle_fullscreen_menu"></span>
<?php } ?>
<?php
if ( $et_contact_info_defined || true === $show_header_social_icons || false !== et_get_option( 'show_search_icon', true ) || class_exists( 'woocommerce' ) || is_customize_preview() ) { ?>
<div class="et_slide_menu_top">
<?php if ( 'fullscreen' === et_get_option( 'header_style', 'left' ) ) { ?>
<div class="et_pb_top_menu_inner">
<?php } ?>
<?php }
if ( true === $show_header_social_icons ) {
get_template_part( 'includes/social_icons', 'header' );
}
et_show_cart_total();
?>
<?php if ( false !== et_get_option( 'show_search_icon', true ) || is_customize_preview() ) : ?>
<?php if ( 'fullscreen' !== et_get_option( 'header_style', 'left' ) ) { ?>
<div class="clear"></div>
<?php } ?>
<form role="search" method="get" class="et-search-form" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<?php
printf( '<input type="search" class="et-search-field" placeholder="%1$s" value="%2$s" name="s" title="%3$s" />',
esc_attr__( 'Search …', 'Divi' ),
get_search_query(),
esc_attr__( 'Search for:', 'Divi' )
);
?>
<button type="submit" id="searchsubmit_header"></button>
</form>
<?php endif; // true === et_get_option( 'show_search_icon', false ) ?>
<?php if ( $et_contact_info_defined ) : ?>
<div id="et-info">
<?php if ( '' !== ( $et_phone_number = et_get_option( 'phone_number' ) ) ) : ?>
<span id="et-info-phone"><?php echo et_core_esc_previously( et_sanitize_html_input_text( $et_phone_number ) ); ?></span>
<?php endif; ?>
<?php if ( '' !== ( $et_email = et_get_option( 'header_email' ) ) ) : ?>
<span id="et-info-email"><?php echo esc_html( $et_email ); ?></span>
<?php endif; ?>
</div> <!-- #et-info -->
<?php endif; // true === $et_contact_info_defined ?>
<?php if ( $et_contact_info_defined || true === $show_header_social_icons || false !== et_get_option( 'show_search_icon', true ) || class_exists( 'woocommerce' ) || is_customize_preview() ) { ?>
<?php if ( 'fullscreen' === et_get_option( 'header_style', 'left' ) ) { ?>
</div> <!-- .et_pb_top_menu_inner -->
<?php } ?>
</div> <!-- .et_slide_menu_top -->
<?php } ?>
<div class="et_pb_fullscreen_nav_container">
<?php
$slide_nav = '';
$slide_menu_class = 'et_mobile_menu';
$slide_nav = wp_nav_menu( array( 'theme_location' => 'primary-menu', 'container' => '', 'fallback_cb' => '', 'echo' => false, 'items_wrap' => '%3$s' ) );
$slide_nav .= wp_nav_menu( array( 'theme_location' => 'secondary-menu', 'container' => '', 'fallback_cb' => '', 'echo' => false, 'items_wrap' => '%3$s' ) );
?>
<ul id="mobile_menu_slide" class="<?php echo esc_attr( $slide_menu_class ); ?>">
<?php
if ( '' === $slide_nav ) :
?>
<?php if ( 'on' === et_get_option( 'divi_home_link' ) ) { ?>
<li <?php if ( is_home() ) echo( 'class="current_page_item"' ); ?>><?php esc_html_e( 'Home', 'Divi' ); ?></li>
<?php }; ?>
<?php show_page_menu( $slide_menu_class, false, false ); ?>
<?php show_categories_menu( $slide_menu_class, false ); ?>
<?php
else :
echo et_core_esc_wp( $slide_nav ) ;
endif;
?>
</ul>
</div>
</div>
<?php
$slide_header = ob_get_clean();
/**
* Filters the HTML output for the slide header.
*
* #since 3.10
*
* #param string $top_header
*/
echo et_core_intentionally_unescaped( apply_filters( 'et_html_slide_header', $slide_header ), 'html' );
?>
<?php endif; // true ==== $et_slide_header ?>
<?php ob_start(); ?>
<header id="main-header" data-height-onload="<?php echo esc_attr( et_get_option( 'menu_height', '66' ) ); ?>">
<div class="container clearfix et_menu_container">
<?php
$logo = ( $user_logo = et_get_option( 'divi_logo' ) ) && ! empty( $user_logo )
? $user_logo
: $template_directory_uri . '/images/logo.png';
ob_start();
?>
<div class="logo_container">
<span class="logo_helper"></span>
<a href="<?php echo esc_url( home_url( '/' ) ); ?>">
<img src="<?php echo esc_attr( $logo ); ?>" alt="<?php echo esc_attr( get_bloginfo( 'name' ) ); ?>" id="logo" data-height-percentage="<?php echo esc_attr( et_get_option( 'logo_height', '54' ) ); ?>" />
</a>
</div>
<?php
$logo_container = ob_get_clean();
/**
* Filters the HTML output for the logo container.
*
* #since 3.10
*
* #param string $logo_container
*/
echo et_core_intentionally_unescaped( apply_filters( 'et_html_logo_container', $logo_container ), 'html' );
?>
<div id="et-top-navigation" data-height="<?php echo esc_attr( et_get_option( 'menu_height', '66' ) ); ?>" data-fixed-height="<?php echo esc_attr( et_get_option( 'minimized_menu_height', '40' ) ); ?>">
<?php if ( ! $et_slide_header || is_customize_preview() ) : ?>
<nav id="top-menu-nav">
<?php
$menuClass = 'nav';
if ( 'on' === et_get_option( 'divi_disable_toptier' ) ) $menuClass .= ' et_disable_top_tier';
$primaryNav = '';
$primaryNav = wp_nav_menu( array( 'theme_location' => 'primary-menu', 'container' => '', 'fallback_cb' => '', 'menu_class' => $menuClass, 'menu_id' => 'top-menu', 'echo' => false ) );
if ( empty( $primaryNav ) ) :
?>
<ul id="top-menu" class="<?php echo esc_attr( $menuClass ); ?>">
<?php if ( 'on' === et_get_option( 'divi_home_link' ) ) { ?>
<li <?php if ( is_home() ) echo( 'class="current_page_item"' ); ?>><?php esc_html_e( 'Home', 'Divi' ); ?></li>
<?php }; ?>
<?php show_page_menu( $menuClass, false, false ); ?>
<?php show_categories_menu( $menuClass, false ); ?>
</ul>
<?php
else :
echo et_core_esc_wp( $primaryNav );
endif;
?>
</nav>
<?php endif; ?>
<?php
if ( ! $et_top_info_defined && ( ! $et_slide_header || is_customize_preview() ) ) {
et_show_cart_total( array(
'no_text' => true,
) );
}
?>
<?php if ( $et_slide_header || is_customize_preview() ) : ?>
<span class="mobile_menu_bar et_pb_header_toggle et_toggle_<?php echo esc_attr( et_get_option( 'header_style', 'left' ) ); ?>_menu"></span>
<?php endif; ?>
<?php if ( ( false !== et_get_option( 'show_search_icon', true ) && ! $et_slide_header ) || is_customize_preview() ) : ?>
<div id="et_top_search">
<span id="et_search_icon"></span>
</div>
<?php endif; // true === et_get_option( 'show_search_icon', false ) ?>
<?php
/**
* Fires at the end of the 'et-top-navigation' element, just before its closing tag.
*
* #since 1.0
*/
do_action( 'et_header_top' );
?>
</div> <!-- #et-top-navigation -->
</div> <!-- .container -->
<div class="et_search_outer">
<div class="container et_search_form_container">
<form role="search" method="get" class="et-search-form" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<?php
printf( '<input type="search" class="et-search-field" placeholder="%1$s" value="%2$s" name="s" title="%3$s" />',
esc_attr__( 'Search …', 'Divi' ),
get_search_query(),
esc_attr__( 'Search for:', 'Divi' )
);
?>
</form>
<span class="et_close_search_field"></span>
</div>
</div>
</header> <!-- #main-header -->
<?php
$main_header = ob_get_clean();
/**
* Filters the HTML output for the main header.
*
* #since 3.10
*
* #param string $main_header
*/
echo et_core_intentionally_unescaped( apply_filters( 'et_html_main_header', $main_header ), 'html' );
?>
<div id="et-main-area">
<?php
/**
* Fires after the header, before the main content is output.
*
* #since 3.10
*/
do_action( 'et_before_main_content' );
In practice the secondary menu appears first, before primary menu. How to change the ordering for displaying:
FIRST: Primary menu;
SECOND: Secondary menu;
Who knows how could i change the order, for displaying the same order that i described above?
First of all, it is not advisable to change the order of the menu in the DIVI theme. Just rearranging the code will collapse the header.
So it is better to register a new Navigation Menu and then use it where you wanted.
Before making any changes to the theme files, make sure you are using Divi Child Theme. If you don't have one, You can download it here (Divi Child Theme Download Link)
Then you can copy your header.php file from Divi theme folder and paste it into the Child theme folder.
To register a custom menu, use the below code in the Functions.php file.
function wpb_custom_new_menu() {
register_nav_menu('my-custom-menu',__( 'My Custom Menu' ));
}
add_action( 'init', 'wpb_custom_new_menu' );
Now go to Appearance -> Menus and create a New Menu and assign it as "My Custom Menu".
To add this menu to the header, Add the below code in header.php where you want to display the menu.
'my-custom-menu',
'container_class' => 'custom-menu-class' ) );
?>
Then you can use the .custom_menu_class CSS class to style the menu.

Need to add excerpt in recent post WP widget

I'm using a premium theme on my wp website but I'm trying to add a recent post widget on home page sidebar, this widget just shows posts title and date but no excerpt. I am a beginner don't know how to customize this widget, I just think these are the codes of the recent post widget.
<?php
/*-----------------------------------------------------------------------------------
Plugin Name: Custom Blog Widget
Plugin URI: http://www.premiumpixels.com
Description: A widget that allows the display of blog posts.
Version: 1.0
Author: Orman Clark
Author URI: http://www.premiumpixels.com
-----------------------------------------------------------------------------------*/
// Add function to widgets_init that'll load our widget.
add_action( 'widgets_init', 'tz_blog_widgets' );
// Register widget.
function tz_blog_widgets() {
register_widget( 'TZ_Blog_Widget' );
}
// Widget class.
class tz_blog_widget extends WP_Widget {
/*-----------------------------------------------------------------------------------*/
/* Widget Setup
/*-----------------------------------------------------------------------------------*/
function __construct() {
/* Widget settings. */
$widget_ops = array( 'classname' => 'tz_blog_widget', 'description' => __('A widget that displays your latest posts with a short excerpt.', 'Creativo') );
/* Widget control settings. */
$control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'tz_blog_widget' );
/* Create the widget. */
parent::__construct( 'tz_blog_widget', __('Custom Recent Posts Widget', 'Creativo'), $widget_ops, $control_ops );
//$this->WP_Widget( 'tz_blog_widget', __('Custom Recent Posts Widget', 'Creativo'), $widget_ops, $control_ops );
}
/*-----------------------------------------------------------------------------------*/
/* Display Widget
/*-----------------------------------------------------------------------------------*/
function widget( $args, $instance ) {
extract( $args );
$title = apply_filters('widget_title', $instance['title'] );
/* Our variables from the widget settings. */
$number = $instance['number'];
/* Before widget (defined by themes). */
echo $before_widget;
/* Display Widget */
?>
<?php /* Display the widget title if one was input (before and after defined by themes). */
if ( $title )
echo $before_title . $title . $after_title;
?>
<?php
$query = new WP_Query();
$query->query( array(
'posts_per_page' => $number,
'ignore_sticky_posts' => 1
));
?>
<?php if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?>
<div class="latest-posts">
<?php
if ( (function_exists('has_post_thumbnail')) && (has_post_thumbnail()) ) { ?>
<div class="latest-posts-thumb"><?php the_post_thumbnail('related-img'); ?></div>
<h2><?php the_title(); ?></h2>
<span><?php the_time( get_option('date_format') ); ?></span>
<div class="clr"></div>
<?php
}
else{?>
<div class="latest-posts-thumb"><img src="<?php bloginfo( 'template_url' ); ?>/images/no-image.jpg" /></div>
<h2><?php the_title(); ?></h2>
<span><?php the_time( get_option('date_format') ); ?></span>
<div class="clr"></div>
<?php
}
?>
</div>
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>
<?php
/* After widget (defined by themes). */
echo $after_widget;
}
/*-----------------------------------------------------------------------------------*/
/* Update Widget
/*-----------------------------------------------------------------------------------*/
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
/* Strip tags to remove HTML (important for text inputs). */
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['number'] = strip_tags( $new_instance['number'] );
/* No need to strip tags for.. */
return $instance;
}
/*-----------------------------------------------------------------------------------*/
/* Widget Settings
/*-----------------------------------------------------------------------------------*/
function form( $instance ) {
/* Set up some default widget settings. */
$defaults = array(
'title' => __('Take a look behind the scenes.','Creativo'),
'number' => 4
);
$instance = wp_parse_args( (array) $instance, $defaults ); ?>
<!-- Widget Title: Text Input -->
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'Creativo') ?></label>
<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" />
</p>
<!-- Widget Title: Text Input -->
<p>
<label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e('Amount to show:', 'Creativo') ?></label>
<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" value="<?php echo $instance['number']; ?>" />
</p>
<?php
}
}
?>
//change this
if ((function_exists('has_post_thumbnail')) && (has_post_thumbnail())) { ?>
<div class="latest-posts-thumb"><?php the_post_thumbnail('related-img'); ?></div>
<h2><?php the_title(); ?></h2>
<span><?php the_time(get_option('date_format')); ?></span>
<div class="clr"></div>
<?php
}
//to
if ((function_exists('has_post_thumbnail')) && (has_post_thumbnail())) { ?>
<div class="latest-posts-thumb"><?php the_post_thumbnail('related-img'); ?></div>
<h2><?php the_title(); ?></h2>
<p class="post-excerpt"><?php the_excerpt(); ?></p>
<span><?php the_time(get_option('date_format')); ?></span>
<div class="clr"></div>
<?php
}
the_excerpt() is used to get post excerpt

How to add custom woocommerce admin field type

i has already add one custom field type to woocommerce admin fields, and has worked 100%.
but this code is in the woocommerce plugin (woocommerce/includes/admin/class-wc-admin-settings.php).
My problem now is how to exclude or hook or filter my custom woocommerce admin field type from woocoommerce plugin to my plugin. therefore, my custom field type still exists and worked when update woocommerce.
this is class-wc-admin-settings.php has customized
<?php
...
class WC_Admin_Settings {
...
public static function output_fields( $options ) {
...
switch ( $value['type'] ) {
case 'productcategory' :
$option_value = (array) self::get_option( $value['id'] );
?><tr valign="top">
<th scope="row" class="titledesc">
<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label>
<?php echo $tooltip_html; ?>
</th>
<td class="forminp forminp-<?php echo sanitize_title( $value['type'] ) ?>">
<fieldset>
<ul class="" style="margin:0; padding:0;">
<?php
$args = array(
'orderby' => 'name',
'hide_empty'=> 0,
'taxonomy' => 'product_cat'
);
$all_categories = get_categories( $args );
$index = 0;
$count = count($all_categories);
$numItemsPerRow = ceil($count / 2);
$numItemsOffsetFix = $count % 2 == 1;
echo '<div class="columns" style="width:auto; display:inline-block; height:auto; float:left; padding:0; margin:0 25px 0 0;">';
foreach ($all_categories as $key => $val) {
if ($index > 0 and $index % $numItemsPerRow == 0) {
echo '</div><div class="columns">';
if ($numItemsOffsetFix) {
$numItemsPerRow--;
$numItemsOffsetFix = false;
}
}
//foreach ( $value['options'] as $key => $val ) {
?>
<li style="">
<label><input type="checkbox"
name="<?php echo esc_attr( $value['id'] ); ?>[]"
id="<?php echo esc_attr( $val->term_id )?>"
value="<?php echo esc_attr( $val->term_id )?>"
<?php
if ( in_array( $val->term_id,$option_value ) ) {
echo ' checked="checked"';
}
?>
/> <?php echo $val->name; ?>
</label>
</li>
<?php
$index++;
}
?>
</ul>
</fieldset>
<fieldset>
<?php echo $description; ?>
</fieldset>
</td>
</tr><?php
break;
...
} //end switch
...
} //end output_fields function
public static function save_fields( $options ) {
...
switch ( $option['type'] ) {
...
case 'productcategory':
$value = array_filter( array_map( 'wc_clean', (array) $raw_value ) );
break;
...
}
...
} //end save_fields function
...
} //end class
?>
thanks for advise.
There's a do_action as the default case in the switch statement. Meaning, that if nothing has matched yet in the core code it will see if anything is attached to the action hook. Similarly, you can sanitize via the woocommerce_admin_settings_sanitize_option_$option_name filter
Therefore, here's my best guess:
// handle output of new settings type
add_action( 'woocommerce_admin_field_productcategory', 'output_productcategory_fields' );
function output_productcategory_fields( $value ) {
$option_value = (array) WC_Admin_Settings::get_option( $value['id'] );
?><tr valign="top">
<th scope="row" class="titledesc">
<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label>
<?php echo $tooltip_html; ?>
</th>
<td class="forminp forminp-<?php echo sanitize_title( $value['type'] ) ?>">
<fieldset>
<ul class="" style="margin:0; padding:0;">
<?php
$args = array(
'orderby' => 'name',
'hide_empty'=> 0,
'taxonomy' => 'product_cat'
);
$all_categories = get_categories( $args );
$index = 0;
$count = count($all_categories);
$numItemsPerRow = ceil($count / 2);
$numItemsOffsetFix = $count % 2 == 1;
echo '<div class="columns" style="width:auto; display:inline-block; height:auto; float:left; padding:0; margin:0 25px 0 0;">';
foreach ($all_categories as $key => $val) {
if ($index > 0 and $index % $numItemsPerRow == 0) {
echo '</div><div class="columns">';
if ($numItemsOffsetFix) {
$numItemsPerRow--;
$numItemsOffsetFix = false;
}
}
//foreach ( $value['options'] as $key => $val ) {
?>
<li style="">
<label><input type="checkbox"
name="<?php echo esc_attr( $value['id'] ); ?>[]"
id="<?php echo esc_attr( $val->term_id )?>"
value="<?php echo esc_attr( $val->term_id )?>"
<?php
if ( in_array( $val->term_id,$option_value ) ) {
echo ' checked="checked"';
}
?>
/> <?php echo $val->name; ?>
</label>
</li>
<?php
$index++;
}
?>
</ul>
</fieldset>
<fieldset>
<?php echo $description; ?>
</fieldset>
</td>
</tr><?php
}
// sanitize data for new settings type
add_filter( 'woocommerce_admin_settings_sanitize_option_productcategory', 'sanitize_productcategory_option', 10, 3 );
function sanitize_productcategory_option( $value, $option, $raw_value ){
$value = array_filter( array_map( 'wc_clean', (array) $raw_value ) );
return $value;
}

Resources