我创建了Woocmerce小部件,它将按产品类别创建循环。产品循环结果应与woocommerce默认内容产品相同。php文件。循环(属性、链接等)中的一切都很好,除了css class
. 我不知道我到底把事情搞砸了,but i guess the main problem is within $defaults
variable or in woocommerce_loop_add_to_cart_args
filter. 请有人帮帮我。
这是我的密码-
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\'];
}
我的主题商店页面添加到购物车类-
但在我的小部件的侧边栏显示中-
请帮帮我。
最合适的回答,由SO网友:Zahid Hossain 整理而成
最后我自己解决了。希望有人能从这个答案中受益。我合并$default
变量所有数组(quantity
, class
, attributes
) 直接进入链接。我改变了整个街区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() )
);
}