1 回答
TA贡献1806条经验 获得超5个赞
您可以创建要显示“继续购物”链接的类别数组,以及购物车中当前所有类别的数组。然后您可以使用array_intersect()来查看两个数组之间是否存在匹配。如果是,请显示链接:
add_action( 'woocommerce_before_cart_table', 'woo_add_continue_shopping_button_to_cart_products' );
function woo_add_continue_shopping_button_to_cart_products() {
// Define categories which should show the keep shopping link
$keep_shopping = array( 'Music', 'Clothing' );
// Check categories in the cart
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$terms = get_the_terms( $cart_item['product_id'], 'product_cat' );
if ( !empty($terms) ) {
foreach ($terms as $key => $term) {
$order_cats[$term->term_id] = $term->name;
}
}
}
// Get array of category matches
$cat_matches = array_intersect( $keep_shopping, $order_cats );
if ( count( $cat_matches ) > 0 ) {
// 1 or more matches, show keep shopping link
printf( '<div class="woocommerce-message"><a href="%s" class="button">Continue Shopping →</a> Do you want more products?</div>', get_permalink( wc_get_page_id( 'shop' ) ) );
}
}
- 1 回答
- 0 关注
- 79 浏览
添加回答
举报