Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Woocommerce 在下拉列表中显示价格和库存状态_Woocommerce - Fatal编程技术网

Woocommerce 在下拉列表中显示价格和库存状态

Woocommerce 在下拉列表中显示价格和库存状态,woocommerce,Woocommerce,我希望在变体下拉列表中显示每个woocommerce产品变体的价格和库存状态,如下所示:价格-变体名称-库存状态 下面的代码在下拉列表中为我提供了价格和变体名称,但我不确定添加每个变体当前股票状态所需的额外代码 // Add Variation Price to Drop Down add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' ); function dis

我希望在变体下拉列表中显示每个woocommerce产品变体的价格和库存状态,如下所示:价格-变体名称-库存状态

下面的代码在下拉列表中为我提供了价格和变体名称,但我不确定添加每个变体当前股票状态所需的额外代码

// Add Variation Price to Drop Down

add_filter( 'woocommerce_variation_option_name', 
'display_price_in_variation_option_name' );

function display_price_in_variation_option_name( $term ) {
global $wpdb, $product;

if ( empty( $term ) ) return $term;
if ( empty( $product->id ) ) return $term;

$result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'" );

$term_slug = ( !empty( $result ) ) ? $result[0] : $term;

$query = "SELECT postmeta.post_id AS product_id
            FROM {$wpdb->prefix}postmeta AS postmeta
                LEFT JOIN {$wpdb->prefix}posts AS products ON ( products.ID = postmeta.post_id )
            WHERE postmeta.meta_key LIKE 'attribute_%'
                AND postmeta.meta_value = '$term_slug'
                AND products.post_parent = $product->id";

$variation_id = $wpdb->get_col( $query );

$parent = wp_get_post_parent_id( $variation_id[0] );

if ( $parent > 0 ) {
     $_product = new WC_Product_Variation( $variation_id[0] );
     return wp_kses( woocommerce_price( $_product->get_price() ), array() ). ' - ' . $term . '';
}
return $term;

}

提前感谢您的帮助。

我已经修改了您的函数,删除了自定义sql查询,并使用WooCommerce内置函数来实现目标

add_filter( 'woocommerce_variation_option_name','display_price_in_variation_option_name');

function display_price_in_variation_option_name( $term ) {
    global $product;

    if ( empty( $term ) ) {
        return $term;
    }
    if ( empty( $product->id ) ) {
        return $term;
    }

    $variation_id = $product->get_children();

    foreach ( $variation_id as $id ) {
        $_product       = new WC_Product_Variation( $id );
        $variation_data = $_product->get_variation_attributes();

        foreach ( $variation_data as $key => $data ) {

            if ( $data == $term ) {
                $html  = wp_kses( woocommerce_price( $_product->get_price() ), array() );
                $html .= ' - ' . $term;
                $html .= ( $_product->get_stock_quantity() ) ? ' - ' . $_product->get_stock_quantity() : '';
                return $html;
            }
        }
    }

    return $term;

}
注意:如果每个产品都有一个属性而不是一个属性组合,则此功能将正常工作

输出:


我通过删除自定义sql查询并使用WooCommerce内置函数来实现目标,从而修改了您的函数

add_filter( 'woocommerce_variation_option_name','display_price_in_variation_option_name');

function display_price_in_variation_option_name( $term ) {
    global $product;

    if ( empty( $term ) ) {
        return $term;
    }
    if ( empty( $product->id ) ) {
        return $term;
    }

    $variation_id = $product->get_children();

    foreach ( $variation_id as $id ) {
        $_product       = new WC_Product_Variation( $id );
        $variation_data = $_product->get_variation_attributes();

        foreach ( $variation_data as $key => $data ) {

            if ( $data == $term ) {
                $html  = wp_kses( woocommerce_price( $_product->get_price() ), array() );
                $html .= ' - ' . $term;
                $html .= ( $_product->get_stock_quantity() ) ? ' - ' . $_product->get_stock_quantity() : '';
                return $html;
            }
        }
    }

    return $term;

}
注意:如果每个产品都有一个属性而不是一个属性组合,则此功能将正常工作

输出:


如果其他人想在变体下拉列表中添加带有自定义措辞的股票状态,下面是我根据Kashalo提供的答案使用的代码

add_filter( 'woocommerce_variation_option_name','display_price_in_variation_option_name');

function display_price_in_variation_option_name( $term ) {
global $product;

if ( empty( $term ) ) {
    return $term;
}
if ( empty( $product->id ) ) {
    return $term;
}

$variation_id = $product->get_children();


foreach ( $variation_id as $id ) {
    $_product       = new WC_Product_Variation( $id );
    $variation_data = $_product->get_variation_attributes();
    $stock_status = $_product->get_stock_status();
    $stock_status = str_replace( array('instock','outofstock','onbackorder'), array('In Stock','Out of Stock','Please allow a few extra days for delivery'), $stock_status );

    foreach ( $variation_data as $key => $data ) {

        if ( $data == $term ) {
            $html  = wp_kses( woocommerce_price( $_product->get_price() ), array() );
            $html .= ' - ' . $term;
            $html .= ( $stock_status ) ? ' - ' . $stock_status : '';
            return $html;
        }
    }
}

return $term;

}

如果其他人想在变体下拉列表中添加带有自定义措辞的股票状态,下面是我根据Kashalo提供的答案使用的代码

add_filter( 'woocommerce_variation_option_name','display_price_in_variation_option_name');

function display_price_in_variation_option_name( $term ) {
global $product;

if ( empty( $term ) ) {
    return $term;
}
if ( empty( $product->id ) ) {
    return $term;
}

$variation_id = $product->get_children();


foreach ( $variation_id as $id ) {
    $_product       = new WC_Product_Variation( $id );
    $variation_data = $_product->get_variation_attributes();
    $stock_status = $_product->get_stock_status();
    $stock_status = str_replace( array('instock','outofstock','onbackorder'), array('In Stock','Out of Stock','Please allow a few extra days for delivery'), $stock_status );

    foreach ( $variation_data as $key => $data ) {

        if ( $data == $term ) {
            $html  = wp_kses( woocommerce_price( $_product->get_price() ), array() );
            $html .= ' - ' . $term;
            $html .= ( $stock_status ) ? ' - ' . $stock_status : '';
            return $html;
        }
    }
}

return $term;

}

你们有合并条款吗?像大小和颜色一样没有所有的变体只使用一个属性-大小。你有组合词吗?就像大小和颜色一样没有所有的变化都只使用一个属性-大小。非常好用!非常感谢你的帮助!我将get_stock_quantity换成get_stock_status,以使前端的客户更清楚地了解该商品是库存中还是缺货。非常有效!非常感谢你的帮助!我将get_stock_quantity换成get_stock_状态,以便让前端的客户更清楚地知道该商品是库存中还是缺货。