【问题标题】:Customizing order completed email notification: Displaying product custom field自定义订单完成电子邮件通知:显示产品自定义字段
【发布时间】:2017-03-12 04:45:36
【问题描述】:

我们正在将 WooCommerce Memberships 插件用于销售健身指导视频的 WooCommerce 网站。

我们为客户销售的每个视频创建了一个 WooCommerce 产品,还创建了一个匹配该产品的 WooCommerce 会员资格,该会员资格在产品销售时激活。

每个视频都在单独的 WordPress 页面上,根据匹配的会员计划进行限制。

我的问题是:
如何向客户发送与他们购买的产品相关的电子邮件通知,其中包含指向包含视频的 Wordpress 页面的 URL?

我知道我们可以override WooCommerce templateemails/customer-completed-order.php,但我不知道如何根据购买的 WooCommerce 产品输出自定义字符串“包含视频页面的 URL”。

您能否根据购买的 WooCommerce 产品帮助输出自定义字符串(包含视频页面的 URL)?

谢谢

【问题讨论】:

    标签: php wordpress woocommerce orders email-notifications


    【解决方案1】:

    您可以在不编辑 WooCommerce 模板的情况下使用专用的 WooCommerce 操作挂钩分两步实现它(如果步骤 1 尚未完成)

    • 在管理产品页面常规设置元框中创建/保存自定义字段。

    • 在您的电子邮件“完成”订单电子邮件通知中呈现指向相关视频页面的链接。

    这是这个功能和测试代码:

    # 1) Creating/Saving a custom field in the admin product pages general setting metabox.
    
    // Inserting product general settings custom field (set the related video page ID)
    add_action( 'woocommerce_product_options_general_product_data', 'product_general_settings_custom_field_create' );
    function product_general_settings_custom_field_create() {
        echo '<div class="options_group">';
    
        woocommerce_wp_text_input( array(
            'type'              => 'text',
            'id'                => 'video_page_id', // we save the related page ID
            'label'             => __( 'Video page ID', 'woocommerce' ),
            'placeholder'       => '',
            'description'       => __( 'Insert page ID', 'woocommerce' ),
        ) );
    
        echo '</div>';
    }
    
    // Saving the custom field value when submitted (saving the related video page ID)
    add_action( 'woocommerce_process_product_meta', 'product_general_settings_custom_field_save' );
    function product_general_settings_custom_field_save( $post_id ){
        $wc_field = $_POST['video_page_id'];
        if( !empty( $wc_field ) )
            update_post_meta( $post_id, 'video_page_id', esc_attr( $wc_field ) );
    }
    
    # 2) Rendering the related video page link in your email "completed" order email notification.
    
    // Displaying in completed order email notification the related video page permalink
    add_action('woocommerce_order_item_meta_end', 'displaying_a_custom_link_in_completed_order_email_notification', 10, 4);
    function displaying_a_custom_link_in_completed_order_email_notification($item_id, $item, $order, $html){
        // For completed orders status only
        if ( $order->has_status('completed') ){
             // Get the custom field value for the order item
            $page_id = get_post_meta( $item['product_id'], 'video_page_id', true);
            $page_id = '324';
            // Get the page Url (permalink)
            $page_permalink = get_permalink( $page_id );
            // Get the page title (optional)
            $page_title = get_the_title( $page_id );
            // Displaying the page link
            echo '<br><small>' . __( 'Watch your video: ', 'woocommerce' ). '<a href="'.$page_permalink.'">' . $page_title . '</a></small>';
        }
    }
    

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

    【讨论】:

      【解决方案2】:
      function render_product_description($item_id, $item, $order){
          $_product = $order->get_product_from_item( $item );
          echo "<a href='.$_product->post->video_url.'>" . $_product->post->video_url. "</a>"; 
      
      }
      
      add_action('woocommerce_order_item_meta_end', 'render_product_description',10,3);
      

      请在您的主题的functions.php中尝试此代码sn-p并进行必要的修改。

      【讨论】:

      • 谢谢 Mujeebu。我不认为 WooCommerce 产品具有 video_url 值。我们是否需要将video_url 作为自定义字段添加到产品中?
      • 是的,您可以将其添加为自定义字段并从上述订单行项目详细信息中获取
      猜你喜欢
      • 2020-12-07
      • 1970-01-01
      • 2020-09-09
      • 2021-01-29
      • 1970-01-01
      • 2018-08-29
      • 2018-08-30
      • 2021-03-26
      • 1970-01-01
      相关资源
      最近更新 更多