在结帐的最后一步,我只会显示一个按钮,如果有2个或更多DIFFERENTE产品,所以我试图想办法计算,如果确实有2个或更多,我试过这样的事情:
if (sizeof(WC()->cart->get_cart()) != 1) {
echo '<a class="button emptycart" href="example-link">Sample-Text</a>';
}
显然没有运气。我在尝试知道产品ID时尝试过不同的情况,但即使我只有5个,也会有25个if语句来完成这个任务,例如:
if ( woocommerce_customer_bought_product( $email, $current_user->ID, '5898' )) && ( woocommerce_customer_bought_product( $email, $current_user->ID, '5936' )){
echo '<a style="float: right; position: relative;margin-top: -35px;" class="button emptycart" href="SAMPLE">BUY</a>';
}elseif ( woocommerce_customer_bought_product( $email, $current_user->ID, '5935' )) && ( woocommerce_customer_bought_product( $email, $current_user->ID, '5937' )){
echo '<a class="button emptycart" href="SAMPLE">BUY</a>';
}
等等......所以任何帮助都会受到赞赏。感谢。
答案 0 :(得分:0)
add_action(&#39; woocommerce_proceed_to_checkout&#39;,&#39; insert_empty_cart_button&#39;);
function insert_empty_cart_button() {
//array to store unique ids of the product
$unique_product_array=array();
// Cycle through each product in the cart
foreach( WC()->cart->cart_contents as $prod_in_cart ) {
// Get the Variation or Product ID
$prod_id = ( isset( $prod_in_cart['variation_id'] ) && $prod_in_cart['variation_id'] != 0 ) ? $prod_in_cart['variation_id'] : $prod_in_cart['product_id'];
// Check if the product already exist in array
if( in_array($prod_id,$unique_product_array) {
array_push($unique_product_array,$prod_id);
}
}
//check the number of unique products in the product array
if(count($unique_product_array)>1) {
echo '<a class="button emptycart" href="example-link">Sample-Text</a>';
}
}
}?>
此代码可能会帮助您实现目标。只需将其添加到您需要的适当挂钩即可。在这里,我已将其添加到&#39; woocommerce_proceed_to_checkout&#39;哪个也行。