• Resolved sbser123

    (@sbser123)


    i have create a custom post type called voucher in that i have to add to cart that post this is how i am working on it but the problem is that when i add to cart the the post then it will send me a error
    ?Sorry, this product cannot be purchased. how can i solve this

    function create_voucher_post_type() {
    $labels = array(
    ‘name’ => x(‘Vouchers’, ‘post type general name’, ‘textdomain’), ‘singular_name’ => _x(‘Voucher’, ‘post type singular name’, ‘textdomain’), ‘menu_name’ => _x(‘Vouchers’, ‘admin menu’, ‘textdomain’), ‘name_admin_bar’ => _x(‘Voucher’, ‘add new on admin bar’, ‘textdomain’), ‘add_new’ => _x(‘Add New’, ‘voucher’, ‘textdomain’), ‘add_new_item’ => (‘Add New Voucher’, ‘textdomain’), ‘new_item’ => (‘New Voucher’, ‘textdomain’), ‘edit_item’ => (‘Edit Voucher’, ‘textdomain’), ‘view_item’ => (‘View Voucher’, ‘textdomain’), ‘all_items’ => (‘All Vouchers’, ‘textdomain’), ‘search_items’ => (‘Search Vouchers’, ‘textdomain’), ‘parent_item_colon’ => (‘Parent Vouchers:’, ‘textdomain’), ‘not_found’ => (‘No vouchers found.’, ‘textdomain’), ‘not_found_in_trash’ => _(‘No vouchers found in Trash.’, ‘textdomain’)
    );

    $args = array(
        'labels'             => $labels,
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array('slug' => 'voucher'),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => null,
        'menu_icon'          => 'dashicons-tickets-alt',
        'supports'           => array('title', 'thumbnail', 'comments', 'excerpt', 'editor', 'custom-fields', 'page-attributes', 'post-formats'),
        'taxonomies'         => array('voucher_category', 'product_tag'),
    );
    
    register_post_type('voucher', $args);

    }
    add_action(‘init’, ‘create_voucher_post_type’);

    // function add_voucher_meta_boxes() {
    // add_meta_box(‘woocommerce-product-data’, __(‘Product Data’, ‘woocommerce’), ‘WC_Meta_Box_Product_Data::output’, ‘voucher’, ‘normal’, ‘high’);
    // }
    // add_action(‘add_meta_boxes’, ‘add_voucher_meta_boxes’);

    function enqueue_voucher_admin_scripts($hook) {
    global $post;

    if ('post.php' === $hook || 'post-new.php' === $hook) {
        if ($post && $post->post_type === 'voucher') {
            wp_enqueue_script('woocommerce-admin-custom', '/wp-content/plugins/woocommerce/assets/js/admin/woocommerce_admin.min.js', array('jquery', 'wc-admin-product-meta-boxes'), '8.8.3', true);
            wp_enqueue_script('wc-admin-meta-boxes');
            wp_enqueue_script('wc-admin-product-meta-boxes');
            wp_enqueue_script('iris');
            wp_enqueue_script('jquery-ui-datepicker');
            wp_enqueue_script('jquery-ui-sortable');
            wp_enqueue_script('jquery-ui-autocomplete');
            wp_enqueue_script('wc-enhanced-select');
            wp_enqueue_style('woocommerce_admin_styles');
            wp_enqueue_style('woocommerce_admin');
            wp_enqueue_style('woocommerce_admin_print_styles');
        }
    }

    }
    add_action(‘admin_enqueue_scripts’, ‘enqueue_voucher_admin_scripts’);

    // Add new product type
    add_filter(‘product_type_selector’, ‘add_voucher_product_type’);

    function add_voucher_product_type($types) {
    $types[‘voucher’] = __(‘Voucher’);
    return $types;
    }

    // Register new product type class
    add_action(‘init’, ‘register_voucher_product_class’);

    function register_voucher_product_class() {
    if (class_exists(‘WC_Product’)) {
    class WC_Product_Voucher extends WC_Product {
    public function __construct($product) {
    $this->product_type = ‘voucher’;
    parent::__construct($product);
    }
    }
    }
    }

    // Load custom class when product type is ‘voucher’
    add_filter(‘woocommerce_product_class’, ‘woocommerce_product_voucher_class’, 10, 2);

    function woocommerce_product_voucher_class($classname, $product_type) {
    if ($product_type == ‘voucher’) {
    $classname = ‘WC_Product_Voucher’;
    }
    return $classname;
    }
    and this is for my add to cart the

    function custom_add_to_cart() {
    if (isset($_POST[‘add-to-cart’])) {
    $post_id = intval($_POST[‘add-to-cart’]);

        if (get_post_type($post_id) == 'voucher') {
            $quantity = 1;
    
            // Get the voucher price
            $voucher_price = get_post_meta($post_id, '_voucher_price', true);
    
            if ($voucher_price) {
                $product = wc_get_product($post_id);
                // Create a product object
                $product->set_name(get_the_title($post_id));
                $product->set_price($voucher_price);
                $product->set_regular_price($voucher_price);
                $product->set_stock_status('instock');
                $product->set_catalog_visibility('visible');
                $product->save(); // Save the product to ensure all changes are applied
    
                // Clear any previous notices
                wc_clear_notices();
    
                // Add the product to cart
                $added_to_cart = WC()->cart->add_to_cart($post_id, $quantity);
    
                if (!$added_to_cart) {
                    // Capture error messages from WooCommerce
                    $notices = wc_get_notices('error');
                    $error_message = "Error: Product could not be added to cart.";
    
                    if (!empty($notices)) {
                        $error_message .= ' Details: ' . implode(' ', wp_list_pluck($notices, 'notice'));
                    }
    
                    die($error_message);
                } else {
                    // Display success message
                    wc_add_notice(__('Product added to cart successfully!', 'woocommerce'), 'success');
                    wp_safe_redirect(wc_get_cart_url());
                    exit;
                }
            } else {
                // Display the error message if voucher price is not set
                wc_add_notice(__('Error: Voucher price not set.', 'woocommerce'), 'error');
                wp_safe_redirect(wc_get_cart_url());
                exit;
            }
        }
    }

    }
    add_action(‘template_redirect’, ‘custom_add_to_cart’);

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Sorry, this product cannot be purchased in custom post type’ is closed to new replies.