Essential Grid 中的 PHP 脚本存在语法错误,在 WooCommerce 中显示“已售完”徽章

问题描述 投票:0回答:1

我正在尝试将已售完的产品放入我的 WooCommerce 电子商店徽章中。

插件可用,但我使用 Essential Grid 插件来显示产品(因此经典插件不可用)。

有一个解决方案(在此描述),直接来自 Essential Grid 开发人员,但它(显然)在第 15 行包含一个语法错误,描述如下:

$html = ‘<p class=”stock in-stock”>In stock</p>’;

完整代码如下。

由于我是 PHP 的新手,我希望能修复该代码,以便我可以使用它。有人可以帮忙吗? ;)

add_shortcode( ‘stock_status’, ‘display_product_stock_status’ );

function display_product_stock_status( $atts) {
    $atts = shortcode_atts(
        array(‘id’  => get_the_ID() ),
        $atts, ‘stock_status’
    );
 
if( intval( $atts[‘id’] ) > 0 && function_exists( ‘wc_get_product’ ) ){
    $product = wc_get_product( $atts[‘id’] );
 
    $stock_status = $product->get_stock_status();
 
    if ( ‘instock’ == $stock_status) {        
$html = ‘<p class=”stock in-stock”>In stock</p>’;  
    } else {
$html = ‘<p class=”stock out-of-stock”>Out of stock</p>’;      
    }
}
return $html;
}

我试图找出如何修复语法错误,但由于我不懂 PHP 语法,所以无法解决它。

php syntax grid
1个回答
0
投票

他们的例子似乎是使用“印刷”引号。只需将它们替换为常规的直单引号和双引号即可:

add_shortcode( 'stock_status', 'display_product_stock_status' );

function display_product_stock_status( $atts) {
    $atts = shortcode_atts(
        array('id'  => get_the_ID() ),
        $atts, 'stock_status'
    );
 
    if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ){
        $product = wc_get_product( $atts['id'] );
    
        $stock_status = $product->get_stock_status();
    
        if ( 'instock' == $stock_status) {        
            $html = '<p class="stock in-stock">In stock</p>';  
        } else {
            $html = '<p class="stock out-of-stock">Out of stock</p>';      
        }
    }

    return $html;
}
© www.soinside.com 2019 - 2024. All rights reserved.