在我之前的问题之后,我有一个问题:Add non-existing order metadata to extend WooCommerce admin orders search

I have two questions:

First:如果我希望只有当客户购买了ID为120和260的产品中的两个时才保存此MD5值,如何才能做到这一点?(即,当付款成功且购买完成时,如果这两个ID为120和260的产品中的一个在客户的(成功)订单中,则应该存储此MD5值)

  • 此问题的另一个请求是,您是否可以判断以前是否为该客户保存了此MD5值,是否会保存新的值?因为MD5代码是基于邮件创建的,对于拥有特定邮件的客户,肯定会得到重复的金额.

Second:在订购了两个特定的产品后,如何执行以下代码或执行一段PHP代码,例如,ID为120和260?

$pro_message = 'Hi dear'
echo $pro_message. 'custom php code';

推荐答案

要使"this md5 value to be saved only when"个特定产品在购物车(或订单项目)中,我们需要一个可重用的条件函数来判断项目:

// Conditional function: Check if specific products are in cart or order items
function has_items_for_email_md5( $object_items ) {
    $found_120 = $found_260 = false; // Initializing
    
    foreach ( $object_items as $item ) {
        $product_id   = isset($item['product_id'])   ? $item['product_id']   : $item->get_product_id();
        $variation_id = isset($item['variation_id']) ? $item['variation_id'] : $item->get_variation_id();

        if ( in_array('120', [$product_id , $variation_id]) ) $found_120 = true;
        if ( in_array('260', [$product_id , $variation_id]) ) $found_260 = true;
    }
    return ( $found_120 && $found_260 );
}

现在,我们可以对前面的代码进行一些小的更改,它将使用这个条件函数:

// When order is created after checkout
add_action( 'woocommerce_checkout_create_order', 'save_billing_email_md5', 20, 2 );
function save_billing_email_md5( $order, $data ) {
    if ( ! has_items_for_email_md5( WC()->cart->get_cart() ) ) return;

    if( $billing_email = $order->get_billing_email() ) {
        $order->add_meta_data('_billing_email_md5', md5($billing_email));
    }
}

// When an order is manually created/updated in admin
add_action( 'woocommerce_process_shop_order_meta', 'admin_save_billing_email_md5' );
function admin_save_billing_email_md5( $order_id ) {
    $order = wc_get_order( $order_id );

    if ( ! has_items_for_email_md5( $order->get_items() ) ) return;

    if( $billing_email = $order->get_billing_email() ) {
        update_post_meta($order_id, '_billing_email_md5', md5($billing_email));
    }
}

最后一个函数保持不变:

add_filter( 'woocommerce_shop_order_search_fields', 'extending_admin_orders_search_field', 10, 1 );
function extending_admin_orders_search_field( $meta_keys ){
    $meta_keys[] = '_billing_email';
    $meta_keys[] = '_billing_phone';
    $meta_keys[] = '_payment_method_title';
    $meta_keys[] = '_billing_email_md5'; // <=== HERE

    return $meta_keys;
}

应该能行得通.


然后,为了在购买这两个特定产品(ID 120和260)后执行一些代码,我们将使用条件函数,如下所示(来自订单ID):

$order = wc_get_order( $order_id ); // Get WC_Order object (if needed)

if ( has_items_for_email_md5( $order->get_items() ) ) {
    $pro_message = __('Hi dear');

    echo $pro_message . __('custom php code');
}

Php相关问答推荐

产品ACF值在WooCommerce我的账户订单的附加列中为空

PHP Perl正则表达式—URL前面没有等号和可能的单引号或双引号

我如何知道BaseController中当前运行的是哪个控制器?

从含有特殊字符的ANSI文本文件中读取数据是在移值,为什么?

如何限制WordPress自定义分类术语页面中的术语数量

MULTI UPDATE Laravel为什么只能更新我的第一行

Laravel Nova不使用数组键作为筛选器中的选项值

添加并显示单价字段到WooCommerce产品变化

使注册字段UserMeta值对于WooCommerce中的每个用户是唯一的

将下拉 Select 的值从WooCommerce后端保存并显示到前端

根据WooCommerce Cart小计阈值自动应用优惠券

Select 本地提货时隐藏WooCommerce显示的发货成本

Shopware 6插件:由于删除了配置密钥,更新后配置值不正确

Wordpress,配置一周第一天选项

在 PHP MySQL 中自动生成账单编号

PHP 解析 XML 以获取多个深度的所有字符串值

无法在 Laravel 中实例化抽象类 ProductAbstract

Firefox 115 会话行为:为什么页面无法找到要加载的现有会话 ID?

同时执行odbc_execute、odbc_fetch_row和odbc_result

如何限制for循环php中的条目数