在 WooCommerce 中隐藏基于产品类型的付款方式
Hide payment method based on product type in WooCommerce
在 WoCommerce 中,我想禁用特定的支付方式并在 WooCommerce 中显示订阅产品的特定支付方式(反之亦然)。
This 是我们发现的最接近的东西,但不符合我的预期。
是的,有些插件可以做到这一点,但我们希望在不使用其他插件的情况下实现这一点,也不会使我们的样式表比现在更糟糕。
请问有什么帮助吗?
这是一个在 woocommerce_available_payment_gateways
过滤器挂钩中使用自定义挂钩函数的示例,我可以在其中根据购物车项目(产品类型)禁用支付网关:
add_filter('woocommerce_available_payment_gateways', 'conditional_payment_gateways', 10, 1);
function conditional_payment_gateways( $available_gateways ) {
// Not in backend (admin)
if( is_admin() )
return $available_gateways;
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$prod_variable = $prod_simple = $prod_subscription = false;
// Get the WC_Product object
$product = wc_get_product($cart_item['product_id']);
// Get the product types in cart (example)
if($product->is_type('simple')) $prod_simple = true;
if($product->is_type('variable')) $prod_variable = true;
if($product->is_type('subscription')) $prod_subscription = true;
}
// Remove Cash on delivery (cod) payment gateway for simple products
if($prod_simple)
unset($available_gateways['cod']); // unset 'cod'
// Remove Paypal (paypal) payment gateway for variable products
if($prod_variable)
unset($available_gateways['paypal']); // unset 'paypal'
// Remove Bank wire (Bacs) payment gateway for subscription products
if($prod_subscription)
unset($available_gateways['bacs']); // unset 'bacs'
return $available_gateways;
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
所有代码都在 Woocommerce 3+ 上测试并且有效。
This is just an example to show you how things can work. You will have to adapt it
这段代码对我非常有用,但其中有一个错误我必须修复:行
$prod_variable = $prod_simple = $prod_subscription = false;
必须放在 FOREACH 之外(之前),否则每次执行新项目时都会重置标志。我的情况是,只要订阅产品在购物车中,我就需要取消设置特定的付款方式。实际上,此代码仅在只有一个订阅产品时才有效。如果我将另一个不同的项目放入购物车,标志将再次变为 false 并且付款方式将加载。将线放在 FOREACH 之外将解决此问题。
在 WoCommerce 中,我想禁用特定的支付方式并在 WooCommerce 中显示订阅产品的特定支付方式(反之亦然)。
This 是我们发现的最接近的东西,但不符合我的预期。
是的,有些插件可以做到这一点,但我们希望在不使用其他插件的情况下实现这一点,也不会使我们的样式表比现在更糟糕。
请问有什么帮助吗?
这是一个在 woocommerce_available_payment_gateways
过滤器挂钩中使用自定义挂钩函数的示例,我可以在其中根据购物车项目(产品类型)禁用支付网关:
add_filter('woocommerce_available_payment_gateways', 'conditional_payment_gateways', 10, 1);
function conditional_payment_gateways( $available_gateways ) {
// Not in backend (admin)
if( is_admin() )
return $available_gateways;
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$prod_variable = $prod_simple = $prod_subscription = false;
// Get the WC_Product object
$product = wc_get_product($cart_item['product_id']);
// Get the product types in cart (example)
if($product->is_type('simple')) $prod_simple = true;
if($product->is_type('variable')) $prod_variable = true;
if($product->is_type('subscription')) $prod_subscription = true;
}
// Remove Cash on delivery (cod) payment gateway for simple products
if($prod_simple)
unset($available_gateways['cod']); // unset 'cod'
// Remove Paypal (paypal) payment gateway for variable products
if($prod_variable)
unset($available_gateways['paypal']); // unset 'paypal'
// Remove Bank wire (Bacs) payment gateway for subscription products
if($prod_subscription)
unset($available_gateways['bacs']); // unset 'bacs'
return $available_gateways;
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
所有代码都在 Woocommerce 3+ 上测试并且有效。
This is just an example to show you how things can work. You will have to adapt it
这段代码对我非常有用,但其中有一个错误我必须修复:行
$prod_variable = $prod_simple = $prod_subscription = false;
必须放在 FOREACH 之外(之前),否则每次执行新项目时都会重置标志。我的情况是,只要订阅产品在购物车中,我就需要取消设置特定的付款方式。实际上,此代码仅在只有一个订阅产品时才有效。如果我将另一个不同的项目放入购物车,标志将再次变为 false 并且付款方式将加载。将线放在 FOREACH 之外将解决此问题。