【问题标题】:Attaching additional hidden information to Woocommerce order将其他隐藏信息附加到 Woocommerce 订单
【发布时间】:2018-04-16 08:18:27
【问题描述】:

所以我在这里遇到了 Woocommerce 的问题。 我不知道这个问题的主题是否是这里的解决方案,但让我解释一下我想要实现的目标。

我创建了这个函数,它将某个类别内购物车中产品的所有总价格汇总到一个小计中。根据该小计,用户会收到一条消息,说明用户获得了多少欧元:

## Category based sum ##
function cat_cart_sum($cat_id) {
    if ( ! is_page( 'winkelmand' ) ) {
        return;
    }
    $cat_count = 0; 
    // Iterating through each cart item
    foreach(WC()->cart->get_cart() as $cart_item)  
        if( has_term( $cat_id, 'product_cat', $cart_item['product_id'])) {
            $regularprice = $cart_item['data']->get_price();
            $price = $regularprice * $cart_item['quantity'];
            $cat_count += $price;
        }
    if ($cat_count >= 40 && $cat_count <= 100) {
        wc_add_notice('U krijgt bij het afhalen €10 gratis vuurwerk!', 'notice');
        } elseif ($cat_count > 100 && $cat_count <= 200) {
            wc_add_notice('U krijgt bij het afhalen €25 gratis vuurwerk!', 'notice');
        } elseif ($cat_count > 200 && $cat_count <= 300) {
            wc_add_notice('U krijgt bij het afhalen €50 gratis vuurwerk!', 'notice');
        } elseif ($cat_count > 300 && $cat_count <= 400) {
            wc_add_notice('U krijgt bij het afhalen €75 gratis vuurwerk!', 'notice');
        } elseif ($cat_count >= 400) {
            wc_add_notice('U krijgt bij het afhalen €100 gratis vuurwerk!', 'notice');
        }
    return $cat_count;
}

用户现在知道他们得到了多少。但仅限于他们的购物车。我希望能够从任何地方通过电子邮件、发票或其他方式调用此金额。 我迷失在这个问题上。有人有解决办法吗?

亲切的问候

【问题讨论】:

    标签: php wordpress woocommerce cart orders


    【解决方案1】:

    WC()-&gt;cart 对象不适用于订单和电子邮件通知……因此,一旦客户结帐,您就无法使用它。

    对于if ( ! is_page( 'winkelmand' ) ) return;,此代码仅适用于页面'winkelmand'... 订购。

    您将在下面找到带有 3 个参数的重新排列函数:

    • $cat_id(类别 ID)
    • $arg(可选:订单 ID 或 WC_Order 对象)
    • $is_email(可选:(布尔值)仅在电子邮件通知中设置为“true”)

    这个函数有2个循环:

    • 如果未定义 $arg,购物车商品循环将起作用...
    • 如果定义了 $arg(通过订单 ID 或订单对象),则订单商品循环将起作用。

    这是你的函数的代码:

    function cat_sum( $cat_id, $arg = null, $is_email = false ) {
        // Will work everywhere except on page 'winkelmand'
        if ( is_page( 'winkelmand' ) ) return;
    
        $total_count = 0;
        $type = gettype($arg);
    
        // 1. WC_Cart
        if( $type == 'NULL' && gettype(WC()->cart) == 'object' && ! WC()->cart->is_empty() ){
            // Iterating through each cart item
            foreach(WC()->cart->get_cart() as $cart_item)
                if( has_term( $cat_id, 'product_cat', $cart_item['product_id'] ) )
                    $total_count += $cart_item['data']->get_price() * $cart_item['quantity'];
            $is_order = false; // Not an order
        }
        // Order ID is set in $arg
        elseif( $type == 'integer' || $type == 'string' ){
            // get an instance of the WC_Order object
            $order = wc_get_order($arg);
            $is_order = true; // An order
        }
        // WC_Order Object is set in $arg
        elseif( $type == 'object' ){
            $order = $arg;
            $is_order = true; // An order
        }
        else return;
    
        // 2. WC_Order
        if( $is_order )
            foreach($order->get_items() as $item )
                if( has_term( $cat_id, 'product_cat', $item->get_product_id() ) )
                    $total_count += $item->get_product()->get_price() * $item->get_quantity();
    
        // Display notice
        if ( $total_count >= 40 && $total_count <= 100 )
            $message = __( 'U krijgt bij het afhalen €10 gratis vuurwerk!' );
        elseif ( $total_count > 100 && $total_count <= 200 )
            $message = __( 'U krijgt bij het afhalen €25 gratis vuurwerk!' );
        elseif ( $total_count > 200 && $total_count <= 300 )
            $message = __( 'U krijgt bij het afhalen €50 gratis vuurwerk!' );
        elseif ( $total_count > 300 && $total_count <= 400 )
            $message = __( 'U krijgt bij het afhalen €75 gratis vuurwerk!' );
        elseif ( $total_count >= 400 )
            $message = __( 'U krijgt bij het afhalen €100 gratis vuurwerk!' );
        else return;
    
        if( ! $is_email ){
            wc_add_notice( $message, 'notice' );
            return $total_count;
        } else {
            return array(
                'total' => $total_count,
                'notice' => $message
            );
        }
    }
    

    代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。


    用法:

    1) 当 WC_Cart 对象可用时(在客户结帐之前)。您将像使用它一样使用它(例如使用“服装”类别)

    $cat_count = cat_sum( 'clothing' );
    echo '<p>'.$cat_count.'</p>';
    

    2) 当 WC_Cart 对象不可用时(客户结帐后)。您需要在收到的订单页面(谢谢)中设置订单 ID 或 WC_Order 对象,如本例所示:

    add_action( 'woocommerce_thankyou', 'cat_count_in_thankyou', 10, 1 );
    function cat_count_in_thankyou( $order_id ) {
        echo '<p>'.cat_sum( 'clothing', $order_id ).'</p>';
    }
    

    代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

    3) 在电子邮件通知中 (WC_Cart 对象不可用且不显示通知) 您需要设置订单 ID 或 WC_Order 对象。对于这种情况,我们使用设置为true 的第三个参数。该函数将返回一个包含类别计数和相应消息(通知)的数组。

    此示例适用于电子邮件通知:

    // Display the total Cat in email notifications
    add_action( 'woocommerce_email_after_order_table', 'add_cat_sum_to_emails', 9, 4 );
    function add_cat_sum_to_emails( $order, $sent_to_admin, $plain_text, $email ){
        $cat_sum = cat_sum( 'clothing', $order, true );
        echo '<p>Total cat: '.$cat_sum['total'].'</p>';
    }
    
    // Display the notice in email notifications
    add_action( 'woocommerce_email_order_details', 'add_custom_notice_to_emails', 4, 4 );
    function add_custom_notice_to_emails( $order, $sent_to_admin, $plain_text, $email ){
        $cat_sum = cat_sum( 'clothing', $order, true );
        echo '<p style="border:solid 1px #333; padding:12px;"> '.$cat_sum['notice'].'</p>';
    }
    

    代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

    一切都经过测试并且有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-17
      • 1970-01-01
      • 2018-09-13
      • 2020-10-26
      • 1970-01-01
      • 2018-06-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多