From 5a991051232ac1c6fd76e2dcb00c3d86c00d2256 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Mon, 7 Nov 2011 15:09:46 +0000 Subject: [PATCH] woocommerce_product_add_to_cart shortocode --- assets/js/admin/editor_plugin.js | 6 ++-- shortcodes/shortcodes-init.php | 61 ++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/assets/js/admin/editor_plugin.js b/assets/js/admin/editor_plugin.js index 35a0e22edf6..1e69875aaa8 100644 --- a/assets/js/admin/editor_plugin.js +++ b/assets/js/admin/editor_plugin.js @@ -19,8 +19,10 @@ }); var a=this;d.onRenderMenu.add(function(c,b){ - - a.addImmediate(b,"Product by ID", '[product id=""]'); + + + a.addImmediate(b,"Product price/cart button", '[add_to_cart id="" sku=""]'); + a.addImmediate(b,"Product by SKU/ID", '[product id="" sku=""]'); a.addImmediate(b,"Products by SKU/ID", '[product ids="" skus=""]'); a.addImmediate(b,"Products by category slug", '[product_category category="" per_page="12" columns="4" orderby="date" order="desc"]'); diff --git a/shortcodes/shortcodes-init.php b/shortcodes/shortcodes-init.php index 7e5d9ba93b2..80ce88408f4 100644 --- a/shortcodes/shortcodes-init.php +++ b/shortcodes/shortcodes-init.php @@ -232,6 +232,66 @@ function woocommerce_product($atts){ return ob_get_clean(); } + +/** + * Display a single prodcut price + cart button + **/ +function woocommerce_product_add_to_cart($atts){ + if (empty($atts)) return; + + $args = array( + 'post_type' => 'product', + 'posts_per_page' => 1, + 'post_status' => 'publish', + 'meta_query' => array( + array( + 'key' => 'visibility', + 'value' => array('catalog', 'visible'), + 'compare' => 'IN' + ) + ) + ); + + if(isset($atts['sku'])){ + $args['meta_query'][] = array( + 'key' => 'sku', + 'value' => $atts['sku'], + 'compare' => '=' + ); + } + + if(isset($atts['id'])){ + $args['p'] = $atts['id']; + } + + if (!$atts['style']) $atts['style'] = 'border:4px solid #ccc; padding: 12px;'; + + query_posts($args); + + ob_start(); + + if (have_posts()) : while (have_posts()) : the_post(); + + $_product = &new woocommerce_product( $post->ID ); + + if (!$_product->is_visible()) continue; + + ?> +

+ + get_price_html(); ?> + + + +