【问题标题】:Add a field to coupon settings and display the value on Woocommerce admin order list在优惠券设置中添加一个字段并在 Woocommerce 管理订单列表中显示该值
【发布时间】:2020-11-20 12:56:52
【问题描述】:

我很难让它发挥作用。 我脑子里有个想法:

  1. 能够通过常规优惠券选项卡中的额外字段将单个用户分配给优惠券代码,该字段列出未分配的用户优惠券代码

    我不想使用第三方扩展、自定义字段等。我希望我能够通过元数据来做到这一点,但我失败了。不知道如何正确完成。

  2. 在订单页面上添加两个额外的列,并显示优惠券代码和分配给它的用户。

    在 phpstorm 中阅读文档和 xDebugging 一段时间后,我也未能完成。

     function order_sellers_and_coupons_columns_values($column)
     {
        global $post, $the_order;
    
        if ($column == 'order_coupon_code') {
            $coupons = $the_order->get_used_coupons(); // not sure how to get coupon object by the coupon code
            echo (count($coupons)) ? $coupons[0] : '';
        }
     }
    
     // even though I see the order objects have an items and coupon lines property, 
     // which is an object, i can't get access to it
     $the_order->items["coupon_lines"]
    

我不是要求现成的解决方案,而是向我展示如何完成它。

提前感谢您的任何帮助。

【问题讨论】:

    标签: php wordpress woocommerce orders coupon


    【解决方案1】:

    在 WooCommerce 管理单优惠券页面中,我们为卖家(经销商)添加了一个额外字段

    // Add a custom field to Admin coupon settings pages
    add_action( 'woocommerce_coupon_options', 'add_coupon_text_field', 10 );
    function add_coupon_text_field() {
        woocommerce_wp_text_input( array(
            'id'                => 'seller_id',
            'label'             => __( 'Assing a seller (dealer)', 'woocommerce' ),
            'placeholder'       => '',
            'description'       => __( 'Assign a seller / dealer to a coupon', 'woocommerce' ),
            'desc_tip'    => true,
    
        ) );
    }
    
    // Save the custom field value from Admin coupon settings pages
    add_action( 'woocommerce_coupon_options_save', 'save_coupon_text_field', 10, 2 );
    function save_coupon_text_field( $post_id, $coupon ) {
        if( isset( $_POST['seller_id'] ) ) {
            $coupon->update_meta_data( 'seller_id', sanitize_text_field( $_POST['seller_id'] ) );
            $coupon->save();
        }
    }
    

    然后使用以下内容,您将添加到管理员订单列表中的优惠券代码(当它在订单中使用时)以及卖家/经销商名称:

    // Adding a new column to admin orders list
    add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column' );
    function custom_shop_order_column($columns)
    {
        $reordered_columns = array();
    
        // Inserting columns to a specific location
        foreach( $columns as $key => $column){
            $reordered_columns[$key] = $column;
            if( $key ==  'order_status' ){
                // Inserting after "Status" column
                $reordered_columns['coupons'] = __( 'Coupon','theme_domain');
            }
        }
        return $reordered_columns;
    }
    
    // Adding used coupon codes
    add_action( 'manage_shop_order_posts_custom_column' , 'custom_orders_list_column_content', 10, 2 );
    function custom_orders_list_column_content( $column, $post_id )
    {
        global $the_order;
    
        if ( $column == 'coupons' ) {
            $coupons = (array) $the_order->get_used_coupons();
            $dealers = [];
    
            foreach( $coupons as $coupon_code ) {
                $coupon    = new WC_Coupon( $coupon_code );
                $dealers[] = $coupon->get_meta('seller_id');
            }
    
            if( count($coupons) > 0 )
                echo implode( ', ', $coupons );
    
            if( count($dealers) > 0 )
                echo '<br><small>(' . implode( ', ', $dealers ) . ')</small>';
        }
    }
    

    所有代码都在您的活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。


    在管理员优惠券单页上:

    在管理员编辑订单列表中:

    【讨论】:

    • 再次感谢 Loic :) 不确定我是否说得够清楚。我不想将优惠券限制给用户(商店客户),而是将优惠券分配给我的卖家/经销商,所以每次我们的一些客户使用优惠券代码时,我都会知道这是我的一位经销商的客户。我也不想为一个订单使用超过一张优惠券。只是想将它们用作标识符。
    • 洛伊克,你太棒了!刚刚测试了你的代码,它就像一个魅力。我有很多东西要学 - 非常感谢您的技能和帮助。
    • 两者都做了,还有什么我可以为你做的吗? :)
    • 有没有办法仅在选择特定优惠券类型时才显示该字段?例如,只有选择了固定推车。 span>
    猜你喜欢
    • 2019-04-02
    • 2021-04-07
    • 2021-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-28
    • 1970-01-01
    相关资源
    最近更新 更多