最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

shortcode - Creating a short code with php inside

programmeradmin1浏览0评论

I am trying to get this code:

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>";
    }

into a shortcode so i can put this button wjere i need it. Everything i try throws errors or 417 failed expectation.

Could someone help me out?

Thanks Wayne

I am trying to get this code:

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>";
    }

into a shortcode so i can put this button wjere i need it. Everything i try throws errors or 417 failed expectation.

Could someone help me out?

Thanks Wayne

Share Improve this question asked Aug 11, 2020 at 19:07 Wayne Hesler-MondoreWayne Hesler-Mondore 111 bronze badge 1
  • You need to add the actual code you're using to setup the shortcode and the actual text of the error to your question – mozboz Commented Aug 11, 2020 at 19:40
Add a comment  | 

1 Answer 1

Reset to default -1

I stripped out a lot of the code that wouldn't be needed in a shortcode:

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' );

Now you can do:

[request_quote_button]

or specify the button text if you want to override the default:

[request_quote_button button_text="Get a Quote"]

发布评论

评论列表(0)

  1. 暂无评论