此功能允许我在购物车按钮之前的单个产品页面上显示演示Url。但我想用一个短代码在其他地方显示它。请帮我怎么做-
代码如下:
/**
* Show a Live Demo button on a single product page
*/
function isa_before_add_to_cart_form() {
global $product;
$url = get_post_meta( $product->get_id(), \'_isa_wc_product_demo_url\', true );
if ( $url ) {
echo \'<p><a href="\' . esc_url( $url ) . \'" id="livedemo" class="button primary is-shade box-shadow-3 box-shadow-5-hover" target="_blank" rel="noopener">\' .
__( \'Live Demo\', \'textdomain\') .
\'</a><p>\';
}
}
add_action(\'woocommerce_before_add_to_cart_form\',\'isa_before_add_to_cart_form\');
/**
* Display the Demo URL field in the Product Data metabox
*/
function isa_wc_product_add_demo_url_field() {
echo \'<div class="options_group">\';
woocommerce_wp_text_input(
array(
\'id\' => \'_isa_wc_product_demo_url\',
\'label\' => __( \'Demo URL\', \'textdomain\' ),
\'placeholder\' => \'\',
\'desc_tip\' => \'true\',
\'description\' => __( \'Set a URL that will be displayed on the product page to link to a demo of this product. Full URL starting with "https://"\', \'textdomain\' )
)
);
echo \'</div>\';
}
add_action( \'woocommerce_product_options_general_product_data\', \'isa_wc_product_add_demo_url_field\' );
/**
* Save the Demo URL field value when the produc is saved
*/
function isa_wc_product_save_demo_url_value( $post_id ) {
$val = trim( get_post_meta( $post_id, \'_isa_wc_product_demo_url\', true ) );
$new = sanitize_text_field( $_POST[\'_isa_wc_product_demo_url\'] );
if ( $val != $new ) {
update_post_meta( $post_id, \'_isa_wc_product_demo_url\', $new );
}
}
add_action( \'woocommerce_process_product_meta\', \'isa_wc_product_save_demo_url_value\' );
最合适的回答,由SO网友:Hardeep Asrani 整理而成
您可以使用add_shortcode 函数来执行此操作。你只需要通过isa_before_add_to_cart_form
用作以下快捷码:
add_shortcode( \'isa_show_link\', \'isa_before_add_to_cart_form );
现在您可以使用快捷码
[isa_show_link]
将其显示在任何位置。这很简单。