Skip to content

Instantly share code, notes, and snippets.

@vaakash
Last active July 25, 2020 19:28
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 vaakash/f4891252f698a62bb0dc21d040853e7e to your computer and use it in GitHub Desktop.
Save vaakash/f4891252f698a62bb0dc21d040853e7e to your computer and use it in GitHub Desktop.
Support to add woocommerce related parameters in shortcoder WordPress plugin
<?php
function shortcoder_woocommerce_params( $params ){
$params['wc_info'] = array(
'name' => __( 'WooCommerce information', 'shortcoder' ),
'icon' => 'cart',
'params' => array(
'sku' => __( 'Product SKU', 'shortcoder' ),
'price' => __( 'Product price', 'shortcoder' ),
'regular_price' => __( 'Product regular price', 'shortcoder' ),
'sale_price' => __( 'Product sale price', 'shortcoder' ),
'short_description' => __( 'Product short description', 'shortcoder' ),
'product_image' => __( 'Product image', 'shortcoder' ),
'product_categories' => __( 'Product categories', 'shortcoder' ),
'product_tags' => __( 'Product tags', 'shortcoder' ),
'product_brands' => __( 'Product brands', 'shortcoder' ),
)
);
return $params;
}
function shortcoder_woocommerce_metadata( $metadata ){
global $post;
$product = wc_get_product( $post->ID );
if( !$product ){
return $metadata;
}
$brands = get_the_term_list($post->ID, 'product_brand');
if( !$brands || is_wp_error( $brands) ){
$brands = '';
}
$metadata[ 'sku' ] = $product->get_sku();
$metadata[ 'price' ] = $product->get_price();
$metadata[ 'regular_price' ] = $product->get_regular_price();
$metadata[ 'sale_price' ] = $product->get_sale_price();
$metadata[ 'short_description' ] = $product->get_short_description();
$metadata[ 'product_image' ] = $product->get_image();
$metadata[ 'product_categories' ] = wc_get_product_category_list($post->ID);
$metadata[ 'product_tags' ] = wc_get_product_tag_list($post->ID);
$metadata[ 'product_brands' ] = $brands;
return $metadata;
}
function shortcoder_woocomerce_init(){
if ( ! function_exists( 'is_woocommerce_activated' ) ) {
add_filter( 'sc_mod_wp_params', 'shortcoder_woocommerce_params' );
add_filter( 'sc_mod_metadata', 'shortcoder_woocommerce_metadata' );
}
}
add_action( 'init', 'shortcoder_woocomerce_init' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment