【问题标题】:Output product custom field in woocommerce email header在 woocommerce 电子邮件标题中输出产品自定义字段
【发布时间】:2019-08-02 17:18:19
【问题描述】:

我正在尝试在订单电子邮件标题中输出产品的自定义字段。这个钩子是woocommerce_email_header ($email_heading, $email)。我尝试了下面的代码,但它不适用于woocommerce_email_header,我在结帐时遇到内部服务器错误。

add_action('woocommerce_email_header', 'wcv_ingredients_email_logo', 10, 4);
function wcv_ingredients_email_logo( $order,  $sent_to_admin,  $plain_text, $email_heading, $email ){
    foreach($order->get_items() as $item_values){
        // Get the product ID for simple products (not variable ones)
        $product_id     = $item_values['product_id']; //get the product ID
        $image_id       = get_post_meta( $product_id, 'store_email_logo', true ); //get the image ID associated to the product
        $image_src      = wp_get_attachment_image_src( $image_id, 'full' )[0]; //get the src of the image - you can use 'full', 'large', 'medium', or 'thumbnail' here,
        $image          = '<img src="'.$image_src.'">'; //create the img element
        echo $image . '<br>'; //echo the image
    }
}

【问题讨论】:

    标签: php wordpress woocommerce hook-woocommerce


    【解决方案1】:

    只需将您的代码替换为以下代码 sn-p -

    add_action('woocommerce_email_header', 'wcv_ingredients_email_logo', 10, 2);
    function wcv_ingredients_email_logo( $email_heading, $email ){
        if($email->object){
            foreach($email->object->get_items() as $item_values){
                // Get the product ID for simple products (not variable ones)
                $product        = $item_values->get_product();
                $image_id       = get_post_meta( $product->get_id(), 'store_email_logo', true ); //get the image ID associated to the product
                $image_src      = wp_get_attachment_image_src( $image_id, 'full' )[0]; //get the src of the image - you can use 'full', 'large', 'medium', or 'thumbnail' here,
                $image          = '<img src="'.$image_src.'">'; //create the img element
                echo $image . '<br>'; //echo the image
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-24
      • 2020-12-07
      • 1970-01-01
      • 2018-01-07
      • 2018-08-29
      • 2018-08-30
      • 1970-01-01
      相关资源
      最近更新 更多