【问题标题】:hide wordpress menu on item purchase在购买商品时隐藏 wordpress 菜单
【发布时间】:2017-05-22 01:40:39
【问题描述】:

我正在尝试建立一个 wordpress 网站,我想在其中显示/隐藏项目购买时的菜单项。通过 WooCommerce 插件购买商品。

例如如果我购买了一个项目,与产品相关的链接应该作为菜单项出现在菜单中。如果有人可以提示我该怎么做。没关系如果我必须编码或编辑代码,我会的。

【问题讨论】:

  • 你想做什么?某种购物车链接/弹出窗口显示购物车中已有的商品?
  • 没有一个简单的逻辑,在产品购买时显示或隐藏导航菜单项如果(购买产品)在导航菜单中显示项目(项目可以是任何页面)否则隐藏菜单页面项目。

标签: php wordpress woocommerce menuitem


【解决方案1】:

由于您的问题不是很清楚,我想您想获取客户(用户 ID)购买的所有商品并将它们显示为一种列表或菜单。

您将在下面找到 2 个功能。
1) 第一个将获取当前客户的所有购买产品 ID(带有可选参数,$user_id)。
2)第二个将显示带有标题和链接的产品的菜单(或列表)......

这是这段代码(在你的活动子主题或主题的function.php文件中)

function get_customer_products( $user_id = null ){

    if( empty($user_id) && is_user_logged_in() )
        $user_id = get_current_user_id();

    if( ! empty($user_id) && ! is_admin() ){
        $customer_orders = get_posts( array(
            'meta_key' => '_customer_user',
            'meta_value' => $user_id,
            'post_type'   => 'shop_order',
            'numberposts' => -1,
            'post_status' => 'wc-completed', // 'completed' order status
        ) );

        $product_ids = array();

        foreach($customer_orders as $customer_order){
            $_order = wc_get_order( $customer_order->ID );
            foreach($_order->get_items() as $item){
                // Avoiding duplicates
                if(!in_array($item['product_id'], $product_ids))
                    $product_ids[] = $item['product_id'];
            }
        }
        return $product_ids;
    }
}

function display_customer_product_list(){
    // Getting current customer bought products IDs
    $product_ids = get_customer_products();
    if(!empty($product_ids)){
        $output_html = '<div class="custom-product"><ul class="custom-menu">';
        foreach( $product_ids as $product_id ){
            $product = new WC_Product($product_id);
            $output_html .= '<li><a href="'.$product->get_permalink().'">'.$product->get_title().'</a></li>';
        }
        $output_html .= '</ul></div>';

        echo $output_html;
    }
}

用法

然后你可以在任何地方使用,在你的主题 php 模板/文件中,这样:

display_customer_product_list();

这将输出如下内容:

<div class="custom-product">
    <ul class="custom-menu">
    <li><a href="http://www.example.com/product/slug1/">Product Title 1</a></li>
    <li><a href="http://www.example.com/product/slug2/">Product Title 2</a></li>
    <li><a href="http://www.example.com/product/slug3/">Product Title 3</a></li>
    </ul>
</div>

使用该材料,您将能够实现您正在查看的内容,重新排列第二个功能,或者仅在活动主题的 header.php 模板中使用第一个功能......

作为隐藏某些现有菜单的条件,您可以使用以下内容:

if(count(get_customer_products()) > 0){
    // Displaying customer bought product items
} else {
    // Displaying normal menu items
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-05
    • 2017-06-03
    • 2011-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-09
    • 2015-08-14
    相关资源
    最近更新 更多