php 用于向woocommerce产品属性添加自定义复选框以隐藏购物车按钮的功能

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 用于向woocommerce产品属性添加自定义复选框以隐藏购物车按钮的功能相关的知识,希望对你有一定的参考价值。

//add custom checkbox for removal of add to cart button on product pages
   add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
   function woo_add_custom_general_fields() {

   	global $woocommerce, $post;

   	echo '<div class="options_group">';

   	woocommerce_wp_checkbox( 
   		array( 
   			'id'            => '_custom_product', 
   			'wrapper_class' => '', 
   			'label'         => __('No Add to Cart Button', 'woocommerce' ), 
   			'description'   => __( 'Product can not be added to cart for purchase online', 'woocommerce' ) 
   		)
   	);	

   	echo '</div>';
   }

// Save Fields
   add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );

   function woo_add_custom_general_fields_save( $post_id ){

	// Checkbox

   	$woocommerce_custom_product_checkbox = isset( $_POST['_custom_product'] ) ? 'yes' : 'no';
   	update_post_meta( $post_id, '_custom_product', $woocommerce_custom_product_checkbox );	

   }
   add_filter('woocommerce_is_purchasable', 'mmi_custom_is_purchasable', 10, 2);

   function mmi_custom_is_purchasable( $is_purchasable, $object ) {

    // get the product id first
   	$product_id = $object->get_id();

	// get the product meta data
   	$is_custom = get_post_meta($product_id, '_custom_product', true);

   	if ($is_custom == "yes"){
   		return false;
   	}
   	else {
   		return true;
   	}

   }

   //remove add to cart and add contact form for non-purchasable products
   add_action( 'woocommerce_single_product_summary', 'mmi_custom_product_cta', 10);

   function mmi_custom_product_cta()
   {
   	global $product;
    // get the product id first
   	$product_id = $product->get_id();

	// get the product meta data
   	$is_custom = get_post_meta($product_id, '_custom_product', true);

	// Show the Form if product is Custom
   	if ($is_custom == "yes"){
   		get_template_part( 'template-parts/content', 'noaddtocartform' );
   	}
   }

以上是关于php 用于向woocommerce产品属性添加自定义复选框以隐藏购物车按钮的功能的主要内容,如果未能解决你的问题,请参考以下文章

向产品属性添加新术语并将其设置在 Woocommerce 中的产品中

php WooCommerce:向产品卡添加详细信息

php 通过函数文件向woocommerce产品添加acf字段

php 通过函数文件向woocommerce产品添加acf字段

将自定义列产品可见性添加到 Woocommerce 3 中的管理产品列表

从所选变体向 WooCommerce 变量产品标题添加一些属性值