[英]Add a checkbox as WooCommerce admin product option that disables payment gateways
I'm trying to unset COD payment gateway
based on a custom product type
based on the value from the checkbox, added as WooCommerce admin product option.我正在尝试
根据
复选框中的值根据
自定义产品类型
取消设置 COD 支付网关,添加为 WooCommerce 管理产品选项。
But it seems the code doesn't do anything with product type: doarcard.但似乎代码对产品类型没有做任何事情:doarcard。
If I set it to simple then it will work:如果我将其设置为简单,那么它将起作用:
//new product type
add_filter("product_type_options", function ($product_type_options) {
$product_type_options['doarcard'] = array(
'id' => '_doarcard',
'wrapper_class' => 'show_if_simple show_if_variable',
'label' => __( 'Doar Card', 'woodmart' ),
'description' => __( 'Activare doar plata cu card sau transfer bancar', 'woodmart' ),
'default' => 'no');
return $product_type_options;
});
add_action("save_post_product", function ($post_ID, $product, $update) {
update_post_meta(
$product->ID
, "_doarcard"
, isset($_POST["_doarcard"]) ? "yes" : "no");
}, 10, 3);
//disable cod for doarcard
add_filter('woocommerce_available_payment_gateways', 'conditional_payment_gateways', 10, 1);
function conditional_payment_gateways( $available_gateways ) {
if( is_admin() )
return $available_gateways;
$prod_doarcard = false;
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = wc_get_product($cart_item['product_id']);
// Get the product types in cart (example)
if($product->is_type('doarcard')) $prod_doarcard = true;
}
if($prod_doarcard)
unset($available_gateways['cod']); // unset 'cod'
return $available_gateways;
}
Any advice?有什么建议吗?
A custom product type has nothing to do with your question.自定义产品类型与您的问题无关。 The value of the checkbox is a boolean with value yes
or no
复选框的值是一个布尔值,值为yes
或no
and based on that you can then unset the $payment_gateways
并基于此您可以取消设置$payment_gateways
So use:所以使用:
// Add a checkbox as WooCommerce admin product option
function filter_product_type_options( $product_type_options ) {
$product_type_options['doarcard'] = array(
'id' => '_doarcard',
'wrapper_class' => 'show_if_simple show_if_variable',
'label' => __( 'Doar Card', 'woocommerce' ),
'description' => __( 'Activare doar plata cu card sau transfer bancar', 'woocommerce' ),
'default' => 'no',
);
return $product_type_options;
}
add_filter( 'product_type_options', 'filter_product_type_options', 10, 1 );
// Save checkbox
function action_woocommerce_admin_process_product_object( $product ) {
$product->update_meta_data( '_doarcard', isset( $_POST['_doarcard'] ) ? 'yes' : 'no' );
}
add_action( 'woocommerce_admin_process_product_object', 'action_woocommerce_admin_process_product_object', 10, 1 );
// Payment gateways
function filter_woocommerce_available_payment_gateways( $payment_gateways ) {
// Not on admin
if ( is_admin() ) return $payment_gateways;
// Initialize
$prod_doarcard = false;
// WC Cart
if ( WC()->cart ) {
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
// Get meta
$doarcard = $cart_item['data']->get_meta( '_doarcard', true );
// Equal to yes = checked
if ( $doarcard == 'yes' ) {
$prod_doarcard = true;
// Product present with the right condition, so break the loop
break;
}
}
// True
if ( $prod_doarcard ) {
// Cod
if ( isset( $payment_gateways['cod'] ) ) {
unset( $payment_gateways['cod'] );
}
}
}
return $payment_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'filter_woocommerce_available_payment_gateways', 10, 1 );
you should use unset inside foreach loop like this:你应该像这样在 foreach 循环中使用 unset :
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = wc_get_product($cart_item['product_id']);
// Get the product types in cart (example)
if($product->is_type('doarcard')){
unset($available_gateways['cod']); // unset 'cod'
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.