我正在尝试更改结帐页面中的下单按钮文本的条件是,如果和只有当有一个产品在购物车中的“捐赠”类别。否则,希望将文本从“下订单”更改为“提交订单”。为此,我应用了以下代码
add_filter('woocommerce_order_button_text', 'subscriptions_custom_checkout_submit_button_text' );
function subscriptions_custom_checkout_submit_button_text( $order_button_text ) {
// set our flag to be false until we find a product in that category
$cat_check = false;
// check each cart item for our category
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
// replace 'donations' with your category's slug
if ( has_term( 'donations', 'product_cat', $product->id ) && !has_term( 'dvds', 'product_cat', $product->id ) ){
$cat_check = true;
// break because we only need one "true" to matter here
break;
}
}
// if a product in the cart is in our category, do something
if ( $cat_check ) {
$order_button_text = __( 'Submit Donation', 'woocommerce-subscriptions' );
} else {
// You can change it here for other products types in cart
# $order_button_text = __( 'Something here', 'woocommerce-subscriptions' );
$order_button_text = __( 'Submit Order', 'woocommerce-subscriptions' );
}
return $order_button_text;
}它起作用了。但还有一个不同的问题..如果有来自其他类别和捐赠类别的产品。
我认为在if循环中需要应用AND条件。但是我不明白如何在if循环中应用和条件。
发布于 2018-11-29 15:33:30
这对你来说应该是可行的:
add_filter('woocommerce_order_button_text', 'subscriptions_custom_checkout_submit_button_text' );
function subscriptions_custom_checkout_submit_button_text( $order_button_text ) {
$donation = true;
// check each cart item for our category
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
if ( !has_term( 'donations', 'product_cat', $product->id ) ){
$donation = false;
break;
}
}
return $donation ? 'Submit Donation' : 'Submit Order';
}你可以在这里查看关于更改"Place Order“按钮文本的教程,https://rudrastyh.com/woocommerce/place-order-button-text.html这里有一个关于更改某个产品的按钮文本的示例。
https://stackoverflow.com/questions/49746854
复制相似问题