创建一个内部带有php的简短代码

时间:2020-08-11 作者:Wayne Hesler-Mondore

我正在尝试获取以下代码:

public function add_request_quote_btn() {
    global $product;
    $product_id = $product->get_id();
    $wc_quote_show_button = get_option(\'wc_quote_show_button\');
    $enable_btn = \'no\';
    if($wc_quote_show_button == \'all\' || $wc_quote_show_button == \'\')
        $enable_btn = \'yes\';
    elseif($wc_quote_show_button == \'out_stock\' && !$product->is_in_stock())
        $enable_btn = \'yes\';
    elseif($wc_quote_show_button == \'guest_users\' && !is_user_logged_in())
        $enable_btn = \'yes\';
    elseif($wc_quote_show_button == \'logged_in_users\' && is_user_logged_in())
        $enable_btn = \'yes\';
    elseif($wc_quote_show_button == \'specific_products\'){
        $wc_quote_products = get_option(\'wc_quote_products\');
        if(!empty($wc_quote_products) && in_array($product_id, $wc_quote_products))
            $enable_btn = \'yes\';
    }
        
    
    if($enable_btn == \'yes\')
    {
        
        $wc_quote_button_text = get_option(\'wc_quote_button_text\');
        if($wc_quote_button_text == \'\')
            $wc_quote_button_text = __(\'Request Quote\', \'woocommerce\');
        
        echo "<button class="open-wc-quote-form single_add_to_cart_button button alt" data-product="\'.$product_id.\'">\'.$wc_quote_button_text.\'</button>";
    }
输入一个短代码,这样我就可以用这个按钮了,我需要它。我尝试的每件事都会引发错误或417个预期失败。

有人能帮帮我吗?

谢斯韦恩

1 个回复
SO网友:Nate Allen

我剥离了许多短代码中不需要的代码:

function sx372821_request_quote_button( $atts = array() ) {
    global $product;

    $atts = shortcode_atts( array(
        \'button_text\'   => get_option( \'wc_quote_button_text\' ),
    ), $atts, \'request_quote_button\' );

    $product_id = $product->get_id();

    $wc_quote_button_text = esc_html( $atts[\'button_text\'] );

    if ( empty( $wc_quote_button_text ) ) {
        $wc_quote_button_text = esc_html__( \'Request Quote\', \'woocommerce\' );
    }

    return "<button class=\\"open-wc-quote-form single_add_to_cart_button button alt\\" data-product=\\"{$product_id}\\">{$wc_quote_button_text}</button>";
}

add_shortcode( \'request_quote_button\', \'sx372821_request_quote_button\' );
现在您可以执行以下操作:

[request_quote_button]

如果要替代默认设置,请指定按钮文本:

[request_quote_button button_text="Get a Quote"]

相关推荐

What does this shortcode do?

function my_shortcode($atts, $content = null){ extract(shortcode_atts(array(\"type\" => \"warrning\"), $atts)); return \".$content.\"; } add_shortcode(\"warrning_dialog\", \"my_shortcode\"); 我知道它会创建短代码[warning\\u dialo