WooCommerce:将 3 个自定义短代码合二为一 [英] WooCommerce: Merge 3 custom shortcodes in one

查看:96
本文介绍了WooCommerce:将 3 个自定义短代码合二为一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个独立的 woocommerce 片段,每个片段都有一个功能.我正在尝试将它们组合在一个脚本中,但似乎无法返回多个值.

I have three separate woocommerce snippets where each one have one function. I'm trying to combine them together in one script but can't seems to return more than one value.

function display_woocommerce_order_count2( $atts, $content = null ) {
$args = shortcode_atts( array(
    'status' => 'completed',
), $atts );
$statuses    = array_map( 'trim', explode( ',', $args['status'] ) );
$order_count = 0;
foreach ( $statuses as $status ) {
    // if we didn't get a wc- prefix, add one
    if ( 0 !== strpos( $status, 'wc-' ) ) {
        $status = 'wc-' . $status;
    }
    $order_count += wp_count_posts( 'shop_order' )->$status;
}
ob_start();
return '<span style="color:#fff;text-align:center;font-size:12px">Deals:' . 
$order_count;
$user->total;
return ob_get_clean();
}
add_shortcode( 'wc_order_count3', 'display_woocommerce_order_count2' );

<小时>

function get_instock_products_count(){
global $wpdb;

// The SQL query
$result = $wpdb->get_col( "
    SELECT COUNT(p.ID)
    FROM {$wpdb->prefix}posts as p
    INNER JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
    WHERE p.post_type LIKE '%product%'
    AND p.post_status LIKE 'publish'
    AND pm.meta_key LIKE '_stock_status'
    AND pm.meta_value LIKE 'instock'
" );

return '<span style="color:#fff;text-align:center;font-size:12px">Proposals 
Left: ' . reset($result);
}
add_shortcode('fp7', 'get_instock_products_count');

<小时>

function new_proposals2(){
global $wpdb;

// 24 hours ago
$is_24h_ago = date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s")." -1day"));

// The SQL query
$result = $wpdb->get_col( "
    SELECT COUNT(p.ID)
    FROM {$wpdb->prefix}posts as p
    WHERE p.post_type LIKE '%product%'
    AND p.post_status LIKE 'publish'
    AND p.post_date > '$is_24h_ago'
" );

return '<span style="color:#fff;text-align:center;font-size:12px">New 
Proposals: ' . reset($result);
}
add_shortcode( 'new_proposals', 'new_proposals2' );

我尝试组合所有脚本并将函数一个接一个放置,并尝试在 3 个函数的末尾返回这 3 个值.但只返回第一个.

i tried combining all scripts and put function after one another and tried to return those 3 values at the end of the 3 functions. But only the first one is returned.

或者在每个函数结束时返回值,没有运气.

or return value at the end of each function, no luck.

我的目标是有类似的东西:

My goal is to have something similar to:

提案:已采纳(x) |新(x) |左(x)

Proposals: Taken(x) | New(x) | Left(x)

推荐答案

将 3 个短代码合并为一个非常简单,可以通过以下方式完成:

To merge that 3 shortcodes in one is very easy and can be done this way:

function order_multi_count( $atts, $content = null ) {
    global $wpdb;

    $args = shortcode_atts( array(
        'status' => 'completed',
    ), $atts );

    ## ---- ---- ---- ---- ---- ---- TAKEN ---- ---- ---- ---- ---- ---- ---- ##

    $statuses    = array_map( 'trim', explode( ',', $args['status'] ) );
    $taken = 0;

    foreach ( $statuses as $status ) {
        // if we didn't get a wc- prefix, add one
        if ( 0 !== strpos( $status, 'wc-' ) ) {
            $status = 'wc-' . $status;
        }
        $taken += wp_count_posts( 'shop_order' )->$status;
    }

    ## ---- ---- ---- ---- ---- ----  LEFT ---- ---- ---- ---- ---- ---- ---- ##

    // The SQL query 
    $result = $wpdb->get_col( "
        SELECT COUNT(p.ID)
        FROM {$wpdb->prefix}posts as p
        INNER JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
        WHERE p.post_type LIKE '%product%'
        AND p.post_status LIKE 'publish'
        AND pm.meta_key LIKE '_stock_status'
        AND pm.meta_value LIKE 'instock'
    " );

    $left = reset($result);

    ## ---- ---- ---- ---- ---- ----  NEW  ---- ---- ---- ---- ---- ---- ---- ##

    // 24 hours ago
    $is_24h_ago = date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s")." -1day"));

    // The SQL query
    $result2 = $wpdb->get_col( "
        SELECT COUNT(p.ID)
        FROM {$wpdb->prefix}posts as p
        WHERE p.post_type LIKE '%product%'
        AND p.post_status LIKE 'publish'
        AND p.post_date > '$is_24h_ago'
    " );

    $new = reset($result2);

    ## ---- ---- ---- ---- ---- RETURNING VALUE ---- ---- ---- ---- ---- ---- ##

    $style = 'style="color:#fff;text-align:center;font-size:12px"';
    return "<span $style><strong>Proposals:</strong> Taken ($taken) | New ($new) | Left ($left)</span>"; 
}
add_shortcode( 'order_multi_count', 'order_multi_count' );

这应该有效

用法: [order_multi_count][order_multi_count status="processing"]

这篇关于WooCommerce:将 3 个自定义短代码合二为一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆