Php 禁用特定电子商务产品的“添加到购物车”按钮
我正在尝试禁用向购物车添加某些产品,这些产品在产品编辑器上勾选了“调用订单”复选框(请参见下面的代码)Php 禁用特定电子商务产品的“添加到购物车”按钮,php,wordpress,woocommerce,product,custom-fields,Php,Wordpress,Woocommerce,Product,Custom Fields,我正在尝试禁用向购物车添加某些产品,这些产品在产品编辑器上勾选了“调用订单”复选框(请参见下面的代码) add_action( 'woocommerce_product_options_general_product_data', 'custom_general_product_data_custom_fields' ); /** * Add `Call to Order` field in the Product data's General tab. */ function custom
add_action( 'woocommerce_product_options_general_product_data', 'custom_general_product_data_custom_fields' );
/**
* Add `Call to Order` field in the Product data's General tab.
*/
function custom_general_product_data_custom_fields() {
// Checkbox.
woocommerce_wp_checkbox(
array(
'id' => '_not_ready_to_sell',
'wrapper_class' => 'show_if_simple',
'label' => __( 'Call to Order', 'woocommerce' ),
'description' => __( '', 'woocommerce' )
)
);
}
add_action( 'woocommerce_process_product_meta', 'custom_save_general_proddata_custom_fields' );
/**
* Save the data values from the custom fields.
* @param int $post_id ID of the current product.
*/
function custom_save_general_proddata_custom_fields( $post_id ) {
// Checkbox.
$woocommerce_checkbox = isset( $_POST['_not_ready_to_sell'] ) ? 'yes' : 'no';
update_post_meta( $post_id, '_not_ready_to_sell', $woocommerce_checkbox );
}
add_filter( 'woocommerce_is_purchasable', 'custom_woocommerce_set_purchasable', 10, 2);
/**
* Mark "Not ready to sell" products as not purchasable.
*/
function custom_woocommerce_set_purchasable() {
$not_ready_to_sell = get_post_meta( get_the_ID(), '_not_ready_to_sell' , true);
return ( 'yes' == $not_ready_to_sell ? false : true );
}
add_filter( 'woocommerce_product_add_to_cart_text', 'custom_product_add_to_cart_text' );
/**
* Change "Read More" button text for non-purchasable products.
*/
function custom_product_add_to_cart_text() {
$not_ready_to_sell = get_post_meta( get_the_ID(), '_not_ready_to_sell', true );
if ( 'yes' === $not_ready_to_sell ) {
return __( 'Call to Order', 'woocommerce' );
} else {
return __( 'Add to Cart', 'woocommerce' );
}
}
勾选复选框的产品实际上是不可购买的,这是期望的结果
我遇到的问题是,当我在产品目录页面上单击可购买产品(未勾选复选框的产品)的“添加到购物车”时,我将被重定向到产品页面,并显示默认的WooCommerce消息“抱歉,此产品无法购买”。应该发生的是,当单击“添加到购物车”按钮时,产品将自动添加到购物车
同样从单一产品页面,我可以毫无问题地添加可购买的购物车
我不知道为什么会这样。有什么想法吗 我已经测试了您的代码,它可以正常工作……我没有您描述的有问题的行为……因此其他东西正在制造麻烦: 您首先需要进行数据库备份…然后应尝试:
现在,你的代码有点过时了 以下是重新访问和增强的代码版本(适用于Woocommerce 3+):
代码位于活动子主题(或活动主题)的function.php文件中。它可以工作。我添加了你的代码,而不是我的,它可以工作!!我发现了上面的代码片段,所以对于WooCommerce 3.0+来说可能有点过时。谢谢你的帮助!真的很感激!
// Add a custom field in the Product data's General tab (for simple products).
add_action( 'woocommerce_product_options_general_product_data', 'add_general_product_data_custom_field' );
function add_general_product_data_custom_field() {
woocommerce_wp_checkbox( array( // Checkbox.
'id' => '_not_ready_to_sell',
'label' => __( 'Call to Order', 'woocommerce' ),
'wrapper_class' => 'show_if_simple',
) );
}
// Save custom field value
add_action( 'woocommerce_admin_process_product_object', 'save_general_product_data_custom_field', 10, 1 );
function save_general_product_data_custom_field( $product ) {
$product->update_meta_data( '_not_ready_to_sell', isset( $_POST['_not_ready_to_sell'] ) ? 'yes' : 'no' );
}
// Make not purchasable, products with '_not_ready_to_sell' meta data set to "yes" (for simple products)
add_filter( 'woocommerce_is_purchasable', 'filter_woocommerce_set_purchasable', 10, 2);
function filter_woocommerce_set_purchasable( $purchasable, $product ) {
return 'yes' === $product->get_meta( '_not_ready_to_sell' ) && $product->is_type('simple') ? false : $purchasable;
}
// Change button text to "Call to Order" for simple products not purchasable.
add_filter( 'woocommerce_product_add_to_cart_text', 'filter_product_add_to_cart_text', 10, 2 );
function filter_product_add_to_cart_text( $button_text, $product ) {
if ( 'yes' === $product->get_meta( '_not_ready_to_sell' ) && $product->is_type('simple') ) {
$button_text = __( 'Call to Order', 'woocommerce' );
}
return $button_text;
}