Ich versuche, das Hinzufügen bestimmter Produkte zum Warenkorb zu deaktivieren, bei denen das Kontrollkästchen “Auf Bestellung anrufen” (siehe Code unten) im Produkteditor aktiviert ist.
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' );
}
}
Die Produkte, bei denen das Kontrollkästchen aktiviert ist, sind tatsächlich nicht käuflich, was das gewünschte Ergebnis ist.
Das Problem, das ich habe, ist, wenn ich auf der Produktkatalogseite für käufliche Produkte (diejenigen, bei denen das Kontrollkästchen nicht aktiviert ist) auf „In den Warenkorb“ klicke, werde ich zur Produktseite weitergeleitet und eine Standard-WooCommerce-Nachricht „Sorry, this product not be gekauft.” erscheint. Was passieren sollte, ist, dass das Produkt automatisch zum Warenkorb hinzugefügt wird, wenn auf die Schaltfläche “In den Warenkorb” geklickt wird.
Auch von der Einzelproduktseite aus kann ich problemlos den käuflichen Warenkorb hinzufügen.
Ich bin mir nicht sicher, warum dies auf diese Weise geschieht. Irgendwelche Ideen?