添加一个复选框作为禁用支付网关的 WooCommerce 管理产品选项
Posted
技术标签:
【中文标题】添加一个复选框作为禁用支付网关的 WooCommerce 管理产品选项【英文标题】:Add a checkbox as WooCommerce admin product option that disables payment gateways 【发布时间】:2021-11-30 15:07:50 【问题描述】:我正在尝试根据复选框中的值取消设置 COD 支付网关基于自定义产品类型,添加为 WooCommerce 管理产品选项。
但似乎代码对产品类型没有任何作用:doarcard。
如果我将其设置为简单,那么它将起作用:
//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;
有什么建议吗?
【问题讨论】:
【参考方案1】:你应该像这样在 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'
【讨论】:
【参考方案2】:自定义产品类型与您的问题无关。复选框的值是一个布尔值,值为yes
或no
在此基础上,您可以取消设置 $payment_gateways
所以使用:
// 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 );
【讨论】:
以上是关于添加一个复选框作为禁用支付网关的 WooCommerce 管理产品选项的主要内容,如果未能解决你的问题,请参考以下文章
根据 WooCommerce 购物车中的商品数量和产品类别禁用支付网关