How to Add Products to Custom Post Type

How to Add Products to Custom Post Type

Getting your Trinity Audio player ready...

How to Add Products to Custom Post Type:
Add a custom product type like bookable_product to the cart in WooCommerce, follow these steps:

Photo credit: Vecteezy

Step 1: Register the custom product type: If you’ve created a new product type, you need to ensure it’s properly registered with WooCommerce. Use the following code to register your custom product type in the theme’s functions.php file or in a custom plugin:

add_action('init', 'register_bookable_product_type');

function register_bookable_product_type() {
    class WC_Product_Bookable_Product extends WC_Product {
        public function __construct($product) {
            $this->product_type = 'bookable_product';
            parent::__construct($product);
        }
    }
}

add_filter('product_type_selector', 'add_bookable_product_type');

function add_bookable_product_type($types) {
    $types['bookable_product'] = __('Bookable Product', 'textdomain');
    return $types;
}

Step 2: Ensure the add_to_cart functionality is working: WooCommerce uses AJAX to handle the “Add to Cart” button. You need to ensure that the custom product type can be added to the cart like other products.

In the same functions.php file or plugin, add the following code to ensure your product can be added to the cart:

add_filter('woocommerce_add_to_cart_validation', 'custom_bookable_product_add_to_cart', 10, 3);

function custom_bookable_product_add_to_cart($passed, $product_id, $quantity) {
    $product = wc_get_product($product_id);
    if ($product->get_type() == 'bookable_product') {
        // Custom validation for bookable products if needed
    }
    return $passed;
}

add_action('woocommerce_add_cart_item_data', 'custom_bookable_product_add_cart_item_data', 10, 2);

function custom_bookable_product_add_cart_item_data($cart_item_data, $product_id) {
    $product = wc_get_product($product_id);
    if ($product->get_type() == 'bookable_product') {
        // Custom cart item data for bookable product
        $cart_item_data['bookable'] = true;  // Example custom data
    }
    return $cart_item_data;
}

add_filter('woocommerce_get_item_data', 'custom_bookable_product_get_item_data', 10, 2);

function custom_bookable_product_get_item_data($item_data, $cart_item) {
    if (isset($cart_item['bookable'])) {
        $item_data[] = array(
            'key' => __('Booking', 'textdomain'),
            'value' => __('Bookable Product', 'textdomain'),
        );
    }
    return $item_data;
}

add_action('woocommerce_add_to_cart', 'custom_bookable_product_add_to_cart', 10, 6);

function custom_bookable_product_add_to_cart($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) {
    $product = wc_get_product($product_id);
    if ($product->get_type() == 'bookable_product') {
        // Custom functionality when adding to cart
    }
}

Handle product display and button: You will also need to ensure that the product type is displayed properly with an “Add to Cart” button on the single product page. You can override the WooCommerce template files if necessary (like single-product/add-to-cart/bookable_product.php).

Here is an example of how you can handle the “Add to Cart” button for the bookable product in your custom template file:

<?php
// Display the "Add to Cart" button for bookable products
do_action('woocommerce_before_add_to_cart_button'); 
?>

<button type="submit" class="single_add_to_cart_button button alt">
    <?php echo esc_html($product->single_add_to_cart_text()); ?>
</button>

<?php do_action('woocommerce_after_add_to_cart_button'); ?>

These steps should allow your custom bookable_product type to be added to the cart successfully. If you need further customization for the booking functionality (e.g., booking dates), additional fields and validation logic will be required. Let me know if you need assistance with that part!

For further assistance we can be contacted :

Similar Posts