I create woocmmerce widget that will create loop by product category. Product loops result should as much as same woocommerce default content-product.php file. And everything is fine within loop ( attributes, link etc ) except css class
. I can't figure out actually where i messed up, but i guess the main problem is within $defaults
variable or in woocommerce_loop_add_to_cart_args
filter. Please someone help me.
here is my code -
function widget($args, $instance) {
$title = apply_filters( 'widget_title', $instance['title'] );
echo $args['before_widget'];
if ( !empty( $title )) {
echo $args['before_title'] . $title . $args['after_title'];
}
$defaults = array(
'cat' => '',
'posts' => '-1',
'orderby' => 'name',
'order' => 'ASC',
'thumbs' => '',
'hidden_p' => '',
'oos_p' => '',
);
if (empty($instance['posts'])) {
$instance['posts'] = $defaults['posts'];
}
if (empty($instance['orderby'])) {
$instance['orderby'] = $defaults['orderby'];
}
if (empty($instance['order'])) {
$instance['order'] = $defaults['order'];
}
?>
<ul class="products productsbycat_list productsbycat_<?php echo $instance['cat']; ?> column-4">
<?php
$arggs = array(
'post_type' => 'product',
'posts_per_page' => $instance['posts'],
'product_cat' => $instance['cat'],
'orderby' => $instance['orderby'],
'order' => $instance['order'],
);
$loop = new WP_Query($arggs);
$show_hidden = ($instance['hidden_p'] == '1') ? true : false;
$show_oos = ($instance['oos_p'] == '1') ? true : false;
while ($loop->have_posts()):
$loop->the_post();
global $product;
$show_hidden_product = true;
$show_oos_product = true;
if ( $show_hidden ) {
if ( ! $product->is_visible()) {
$show_hidden_product = true;
}
} else {
if ( ! $product->is_visible()) {
$show_hidden_product = false;
}
}
if ( $show_oos ) {
if ( ! $product->managing_stock() && ! $product->is_in_stock()) {
$show_oos_product = true;
}
} else {
if ( ! $product->managing_stock() && ! $product->is_in_stock()) {
$show_oos_product = false;
}
}
$output = '';
if ($show_hidden_product && $show_oos_product) {
$output .= '<li class="' . esc_attr( implode( ' ', wc_get_product_class( '', $product ) ) ) . '"><div class="global_product_wrapper">';
$output .= '<a href="'.get_permalink($loop->post->ID).'" class="woocommerce-LoopProduct-link woocommerce-loop-product__link" title="'.esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID).'">';
$image_size = apply_filters( 'single_product_archive_thumbnail_size', 'woocommerce_thumbnail' );
$output .= $product ? $product->get_image( $image_size ) : '';
$output .= '</a>';
$output .= '<div class="title-and-price">';
$output .= '<a href="'.get_permalink($loop->post->ID).'" class="woocommerce-LoopProduct-link woocommerce-loop-product__link" title="'.esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID).'">';
$output .= '<h2 class="' . esc_attr( apply_filters( 'woocommerce_product_loop_title_classes', 'woocommerce-loop-product__title' ) ) . '">' . get_the_title() . '</h2>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
$output .= '</a>';
if ( $price_html = $product->get_price_html() ) :
$output .= '<span class="price">' . $price_html . '</span>';
endif;
$output .= '</div>';
$output .= '<div class="custom_add_to_cart_wrapper">';
if ( $product ) {
$defaults = array(
'quantity' => 1,
'class' => implode(
' ',
array_filter(
array(
'button',
'product_type_' . $product->get_type(),
$product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
$product->supports( 'ajax_add_to_cart' ) && $product->is_purchasable() && $product->is_in_stock() ? 'ajax_add_to_cart' : '',
)
)
),
'attributes' => array(
'data-product_id' => $product->get_id(),
'data-product_sku' => $product->get_sku(),
'aria-label' => $product->add_to_cart_description(),
'rel' => 'nofollow',
),
);
$args = apply_filters( 'woocommerce_loop_add_to_cart_args', wp_parse_args( $args, $defaults ), $product );
if ( isset( $args['attributes']['aria-label'] ) ) {
$args['attributes']['aria-label'] = wp_strip_all_tags( $args['attributes']['aria-label'] );
}
$output .= apply_filters(
'woocommerce_loop_add_to_cart_link', // WPCS: XSS ok.
sprintf(
'<a href="%s" data-quantity="%s" class="%s" %s>%s</a>',
esc_url( $product->add_to_cart_url() ),
esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 ),
esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '',
esc_html( $product->add_to_cart_text() )
),
$product,
$args
);
}
$output .= '<div>';
$output .= '</div></li>';
}
echo $output;
endwhile;
wp_reset_query();
?>
</ul>
<?php
echo $args['after_widget'];
}
My themes shop page add to cart class -
But in my widget's sidebar show -
Please help me.
I create woocmmerce widget that will create loop by product category. Product loops result should as much as same woocommerce default content-product.php file. And everything is fine within loop ( attributes, link etc ) except css class
. I can't figure out actually where i messed up, but i guess the main problem is within $defaults
variable or in woocommerce_loop_add_to_cart_args
filter. Please someone help me.
here is my code -
function widget($args, $instance) {
$title = apply_filters( 'widget_title', $instance['title'] );
echo $args['before_widget'];
if ( !empty( $title )) {
echo $args['before_title'] . $title . $args['after_title'];
}
$defaults = array(
'cat' => '',
'posts' => '-1',
'orderby' => 'name',
'order' => 'ASC',
'thumbs' => '',
'hidden_p' => '',
'oos_p' => '',
);
if (empty($instance['posts'])) {
$instance['posts'] = $defaults['posts'];
}
if (empty($instance['orderby'])) {
$instance['orderby'] = $defaults['orderby'];
}
if (empty($instance['order'])) {
$instance['order'] = $defaults['order'];
}
?>
<ul class="products productsbycat_list productsbycat_<?php echo $instance['cat']; ?> column-4">
<?php
$arggs = array(
'post_type' => 'product',
'posts_per_page' => $instance['posts'],
'product_cat' => $instance['cat'],
'orderby' => $instance['orderby'],
'order' => $instance['order'],
);
$loop = new WP_Query($arggs);
$show_hidden = ($instance['hidden_p'] == '1') ? true : false;
$show_oos = ($instance['oos_p'] == '1') ? true : false;
while ($loop->have_posts()):
$loop->the_post();
global $product;
$show_hidden_product = true;
$show_oos_product = true;
if ( $show_hidden ) {
if ( ! $product->is_visible()) {
$show_hidden_product = true;
}
} else {
if ( ! $product->is_visible()) {
$show_hidden_product = false;
}
}
if ( $show_oos ) {
if ( ! $product->managing_stock() && ! $product->is_in_stock()) {
$show_oos_product = true;
}
} else {
if ( ! $product->managing_stock() && ! $product->is_in_stock()) {
$show_oos_product = false;
}
}
$output = '';
if ($show_hidden_product && $show_oos_product) {
$output .= '<li class="' . esc_attr( implode( ' ', wc_get_product_class( '', $product ) ) ) . '"><div class="global_product_wrapper">';
$output .= '<a href="'.get_permalink($loop->post->ID).'" class="woocommerce-LoopProduct-link woocommerce-loop-product__link" title="'.esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID).'">';
$image_size = apply_filters( 'single_product_archive_thumbnail_size', 'woocommerce_thumbnail' );
$output .= $product ? $product->get_image( $image_size ) : '';
$output .= '</a>';
$output .= '<div class="title-and-price">';
$output .= '<a href="'.get_permalink($loop->post->ID).'" class="woocommerce-LoopProduct-link woocommerce-loop-product__link" title="'.esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID).'">';
$output .= '<h2 class="' . esc_attr( apply_filters( 'woocommerce_product_loop_title_classes', 'woocommerce-loop-product__title' ) ) . '">' . get_the_title() . '</h2>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
$output .= '</a>';
if ( $price_html = $product->get_price_html() ) :
$output .= '<span class="price">' . $price_html . '</span>';
endif;
$output .= '</div>';
$output .= '<div class="custom_add_to_cart_wrapper">';
if ( $product ) {
$defaults = array(
'quantity' => 1,
'class' => implode(
' ',
array_filter(
array(
'button',
'product_type_' . $product->get_type(),
$product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
$product->supports( 'ajax_add_to_cart' ) && $product->is_purchasable() && $product->is_in_stock() ? 'ajax_add_to_cart' : '',
)
)
),
'attributes' => array(
'data-product_id' => $product->get_id(),
'data-product_sku' => $product->get_sku(),
'aria-label' => $product->add_to_cart_description(),
'rel' => 'nofollow',
),
);
$args = apply_filters( 'woocommerce_loop_add_to_cart_args', wp_parse_args( $args, $defaults ), $product );
if ( isset( $args['attributes']['aria-label'] ) ) {
$args['attributes']['aria-label'] = wp_strip_all_tags( $args['attributes']['aria-label'] );
}
$output .= apply_filters(
'woocommerce_loop_add_to_cart_link', // WPCS: XSS ok.
sprintf(
'<a href="%s" data-quantity="%s" class="%s" %s>%s</a>',
esc_url( $product->add_to_cart_url() ),
esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 ),
esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '',
esc_html( $product->add_to_cart_text() )
),
$product,
$args
);
}
$output .= '<div>';
$output .= '</div></li>';
}
echo $output;
endwhile;
wp_reset_query();
?>
</ul>
<?php
echo $args['after_widget'];
}
My themes shop page add to cart class -
But in my widget's sidebar show -
Please help me.
Share Improve this question edited Oct 9, 2020 at 13:48 Zahid Hossain asked Oct 9, 2020 at 7:05 Zahid HossainZahid Hossain 71 silver badge4 bronze badges 1- Please someone help me. – Zahid Hossain Commented Oct 9, 2020 at 13:49
1 Answer
Reset to default 0At last i solved it myself. Hope someone will get benefit form this answer. I merge $default
variables all array (quantity
, class
, attributes
) directly into link. I changed whole block of if ( $product ) {}
.
if ( $product ) {
$output .= sprintf( '<a href="%s" data-quantity="1" class="%s" %s>%s</a>',
esc_url( $product->add_to_cart_url() ),
esc_attr(
implode(
' ',
array_filter(
array(
'button', 'product_type_' . $product->get_type(),
$product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
$product->supports( 'ajax_add_to_cart' ) ? 'ajax_add_to_cart' : '',
)
)
)
),
wc_implode_html_attributes(
array(
'data-product_id' => $product->get_id(),
'data-product_sku' => $product->get_sku(),
'aria-label' => $product->add_to_cart_description(),
'rel' => 'nofollow',
)
),
esc_html( $product->add_to_cart_text() )
);
}