Skip to content

Instantly share code, notes, and snippets.

@jameshwartlopez
Last active July 25, 2017 11:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jameshwartlopez/ad675a295b8e6f3062db4835f0613dc2 to your computer and use it in GitHub Desktop.
Save jameshwartlopez/ad675a295b8e6f3062db4835f0613dc2 to your computer and use it in GitHub Desktop.
View Price In Cart
<?php
/**
* Plugin Name: View Price In Cart
* Plugin URI: https://gist.github.com/jameshwartlopez/ad675a295b8e6f3062db4835f0613dc2
* Description: WooCommerce extension for making the product prices as a link to cart
* Author: Jameshwart Lopez
* Author URI: https://github.com/jameshwartlopez
* Version: 1.0
*/
class ViewPriceInCart
{
public $version = '1.0';
public function __construct()
{
$this->init();
}
public function init()
{
add_filter( 'woocommerce_get_price_html', array($this, 'view_price_in_cart') );
add_action( 'woocommerce_product_options_general_product_data', array($this, 'custom_general_fields') );
add_action( 'woocommerce_process_product_meta', array($this, 'custom_general_fields_save') );
}
public function view_price_in_cart($price)
{
global $woocommerce, $post;
if(!is_admin()){
$view_price_in_cart = get_post_meta($post->ID, '_view_price_in_cart', true);
if('yes' == $view_price_in_cart){
$cart_url = $woocommerce->cart->get_cart_url();
return '<a href="'.$cart_url.'">View price in cart</a>';
}
}
return $price;
}
public function custom_general_fields()
{
global $post;
$view_price_in_cart = get_post_meta($post->ID, '_view_price_in_cart', true);
?>
<div class="options_group">
<p class="form-field">
<label for="_view_price_in_cart">View Price in Cart?</label>
<input type="checkbox" <?php echo ('yes' == $view_price_in_cart) ? 'checked': ''; ?> class="checkbox" style="" name="_view_price_in_cart" id="_view_price_in_cart" value="yes">
<span class="description">Enable to show price as a link to cart page.</span>
</p>
</div>
<?php
}
public function custom_general_fields_save($post_id)
{
$view_price_in_cart = isset($_POST['_view_price_in_cart']) ? $_POST['_view_price_in_cart'] : '';
update_post_meta($post_id, '_view_price_in_cart', $view_price_in_cart);
}
}
$ViewPriceInCart = new ViewPriceInCart();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment