按产品类别在购物车中显示个性化消息,鼓励您继续购物

使用以下代码,我可以在购物车中显示一条消息,鼓励用户继续购买更多产品,但该消息对于所有产品类别永久显示,如何使其适用于一个或多个特定类别?谢谢你能帮助我。


    add_action( 'woocommerce_before_cart_table', 'woo_add_continue_shopping_button_to_cart_products' );


function woo_add_continue_shopping_button_to_cart_products() {


 echo '<div class="woocommerce-message">';

 echo ' <a href="https://midomain.com/" class="button">Continue Shopping →</a> Do you want more products?';

 echo '</div>';

}


慕码人2483693
浏览 80回答 1
1回答

忽然笑

您可以创建要显示“继续购物”链接的类别数组,以及购物车中当前所有类别的数组。然后您可以使用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() {&nbsp; &nbsp; // Define categories which should show the keep shopping link&nbsp; &nbsp; $keep_shopping = array( 'Music', 'Clothing' );&nbsp; &nbsp; // Check categories in the cart&nbsp; &nbsp; foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {&nbsp; &nbsp; &nbsp; &nbsp; $terms = get_the_terms( $cart_item['product_id'], 'product_cat' );&nbsp; &nbsp; &nbsp; &nbsp; if ( !empty($terms) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($terms as $key => $term) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $order_cats[$term->term_id] = $term->name;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // Get array of category matches&nbsp; &nbsp; $cat_matches = array_intersect( $keep_shopping, $order_cats );&nbsp; &nbsp; if ( count( $cat_matches ) > 0 ) {&nbsp; &nbsp; &nbsp; &nbsp; // 1 or more matches, show keep shopping link&nbsp; &nbsp; &nbsp; &nbsp; 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' ) ) );&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP