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..
<form action="" method="post">
<input name="add-to-cart" type="hidden" value="<?php echo $post->ID ?>" />
<input name="quantity" type="number" value="1" min="1" />
<input name="submit" type="submit" value="Add to cart" />
</form>
you need to have this to get "Add to Cart" button. The name of the inputs are required to be as is.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…