Adding custom post type / post to Woocommerce

前端 未结 4 1800
既然无缘
既然无缘 2020-12-15 11:08

I have a personal theme \"A\" and I want that it can also work without Woocommerce. When Woocommerce \"WC\" plugin is added I would integra

4条回答
  •  孤街浪徒
    2020-12-15 11:29

    I have created a simple tutorial as to adding it here would be too long.

    To achieved your goal, your post type must have a price. That said, the custom field for price should have a meta key _price.

    if you already have a meta key which is not _price, you can add a filter to woocommerce_get_price shown below.

    add_filter('woocommerce_get_price','reigel_woocommerce_get_price',20,2);
    function reigel_woocommerce_get_price($price,$post){
        if ($post->post->post_type === 'post') // change this to your post type
            $price = get_post_meta($post->id, "price", true); // assuming your price meta key is price
        return $price;
    }
    

    With this, you can now add any post in your post type to the cart. Do so like http://localhost/wordpress/cart/?add-to-cart=1 where 1 is the ID of your post in your post type.

    sample result image after visiting that link:

    Now, you have to setup a form like below..

    you need to have this to get "Add to Cart" button. The name of the inputs are required to be as is.

提交回复
热议问题