在Woocommerce pdf发票中显示自定义结帐字段值

时间:2018-03-13 06:45:08

标签: php wordpress pdf woocommerce checkout

我想在Woocommerce中为我的订单添加一些自定义字段。 我之前已经成功地根据this official documentation在结帐时输入了一些用户输入的新字段。

我想要实现的是根据我工作的其他产品检查逻辑将设置字符串值传递给自定义字段。基本上如果某个类别的产品:

  • 在购物车中,显示消息一:(shipping_instructions_delivery_field_update_order_meta
  • 否则显示消息二(shipping_instructions_pickup_field_update_order_meta)。

我的代码:

// adds order note at checkout page for the RRNC to pickup at home
add_action( 'woocommerce_before_checkout_form', 'specific_checkout_content', 12 );
function specific_checkout_content() {
    // set your special category name, slug or ID here:
    $special_cat = 'randwick-rugby-netball-club';
    $bool = false;
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        $item = $cart_item['data'];
        if ( has_term( $special_cat, 'product_cat', $item->id ) )
            $bool = true;
    }
    // If the special category is detected in one items of the cart
    // It displays the message
    if ($bool)
    {
      echo '<div class="checkout-instructions"><p><strong>PLEASE PICK UP ORDER FROM THE CLUB.</strong></p></div>';
      add_filter( 'woocommerce_cart_needs_shipping_address', '__return_false');     //Removes the ship to different address for club store items
      add_filter( 'woocommerce_enable_order_notes_field', 'remove_wc_order_notes' );    //Removes the order notes
      add_action( 'woocommerce_checkout_update_order_meta', 'shipping_instructions_pickup_field_update_order_meta' );   //Adds the pick up note for the pdf slip
      add_action( 'woocommerce_checkout_process', 'my_custom_checkout_field_process_pickup' );
    }
  else
  {
     echo '<p><strong>Orders are dispatched within 2 business days and shipping times are estimated at between 3-7 business days depending on your location within Australia.</strong></p>';
    add_action( 'woocommerce_before_order_notes', 'delivery_instructions_field', 10 );  //Adds the Delivery Instructions fields to the checkout
    add_action( 'woocommerce_checkout_update_order_meta', 'shipping_instructions_delivery_field_update_order_meta' );   //Adds the delivery note for the pdf slip
    add_action( 'woocommerce_checkout_process', 'my_custom_checkout_field_process_delivery' );
  }
}

function my_custom_checkout_field_process_delivery( ) {
                global $woocommerce;
                // Check if set, if its not set add an error.
                if ( !$_POST[ 'delivery_instructions' ] )
                                wc_add_notice( __( 'Please select from the delivery options.' ), 'error' );
                                 /*$woocommerce->add_error( __( 'Please select from the delivery options.' ) );*/
                if ( !$_POST[ 'sport_instructions' ] )
                                wc_add_notice( __( 'Please select the sport of the order' ), 'error' );
                                /*$woocommerce->add_error( __( 'Please select the sport of the order' ) );*/
}

function my_custom_checkout_field_process_pickup( ) {
                global $woocommerce;
                // Check if set, if its not set add an error.
                if ( !$_POST[ 'sport_instructions' ] )
                                wc_add_notice( __( 'Please select the sport of the order' ), 'error' );
                                /*$woocommerce->add_error( __( 'Please select the sport of the order' ) );*/
}

// remove Order Notes from checkout field in Woocommerce
function remove_wc_order_notes() {
    return false;
}


function shipping_instructions_delivery_field_update_order_meta( $order_id ) {
            update_post_meta( $order_id, 'shipping_instructions', 'IF YOU HAVE NOT RECEIVED YOUR ORDER WITHIN 7 DAYS, PLEASE CONTACT US TO FOLLOW UP' );
}

function shipping_instructions_pickup_field_update_order_meta( $order_id ) {
            update_post_meta( $order_id, 'shipping_instructions', 'PLEASE PICK UP FROM THE CLUB SHOP' );
}

//Admin side
/**
 * Update the order meta with field value
 **/
add_action( 'woocommerce_checkout_update_order_meta', 'delivery_instructions_field_update_order_meta' );
function delivery_instructions_field_update_order_meta( $order_id ) {
                if ( $_POST[ 'delivery_instructions' ] )
                                update_post_meta( $order_id, 'delivery_instructions', esc_attr( $_POST[ 'delivery_instructions' ] ) );
}

add_action('woocommerce_admin_order_data_after_billing_address', 'my_custom_shipping_fields_display_admin_order_meta', 10, 1);

function my_custom_shipping_fields_display_admin_order_meta($order) {
echo '<p><strong>' . __('Shipping Note') . ':</strong><br> ' . get_post_meta($order->id, 'shipping_instructions', true) . '</p>';
}

$shipping_instructions = get_post_meta($wpo_wcpdf->export->order->id,'shipping_instructions',true);
if (isset($shipping_instructions)) {
    echo $shipping_instructions;
    }


/**
 * Add the delivery instructions field to the checkout
 **/

function delivery_instructions_field( $checkout ) {
                 echo '<div id="delivery_instructions_field"><h2>' . __('Delivery Instructions') . '</h2>';
                woocommerce_form_field_radio( 'delivery_instructions', array(
                                 'type' => 'select',
                                'class' => array(
                                                 'delivery_instructions form-row-wide'
                                ),
                                'label' => __( '' ),
                                'placeholder' => __( '' ),
                                'required' => true,
                                'options' => array(
                                  'Signature Required' => '<b> Signature Required</b><br/>A signature is required for the goods to be left at the intended address. If there is no one to sign for your delivery, a card will be left and it is then your responsibility to collect the items from the nearest depot or arrange for the courier to return. This will be at your expense<br/><br/>',
                                  'Leave Unattended' => '<b> Authority to leave unattended</b><br/>Courier will leave your goods at the intended address. You can give directions in the “Order Notes” below for the best place to leave your delivery',

                                )
                ), $checkout->get_value( 'delivery_instructions' ) );
                echo '</div>';
}


add_action('woocommerce_admin_order_data_after_billing_address', 'my_custom_billing_fields_display_admin_order_meta', 10, 1);

function my_custom_billing_fields_display_admin_order_meta($order) {
echo '<p><strong>' . __('Delivery Instructions') . ':</strong><br> ' . get_post_meta($order->id, 'delivery_instructions', true) . '</p>';
}

$delivery_instructions = get_post_meta($wpo_wcpdf->export->order->id,'_delivery_instructions',true);
if (isset($delivery_instructions)) {
    echo $delivery_instructions;
    }

/**
 * Add the field to the checkout to get the sport of what the customer orders
 **/
add_action( 'woocommerce_after_order_notes', 'sport_instructions_field', 20 );
function sport_instructions_field( $checkout ) {
                 echo '<div id="sport_instructions_field"><h2>' . __('Which sport is this order in relation to?') . '</h2>';
                woocommerce_form_field_radio( 'sport_instructions', array(
                                 'type' => 'select',
                                'class' => array(
                                                 'sport_instructions form-row-wide'
                                ),
                                'label' => __( '' ),
                                'placeholder' => __( '' ),
                                'required' => true,
                                'options' => array(
                                  'Rugby Union' => '<b>Rugby Union</b><br/>',
                                  'Rugby League' => '<b>Rugby League</b><br/>',
                                   'Netball' => '<b>Netball</b><br/>',
                                   'Football' => '<b>Football</b><br/>',
                                   'AFL' => '<b>AFL</b><br/>',
                                   'Touch Tag' => '<b>Touch & Tag</b>',

                                )
                ), $checkout->get_value( 'sport_instructions' ) );
                echo '</div>';
}

/**
 * Update the order meta with field value
 **/
add_action( 'woocommerce_checkout_update_order_meta', 'sport_instructions_field_update_order_meta' );
function sport_instructions_field_update_order_meta( $order_id ) {
                if ( $_POST[ 'sport_instructions' ] )
                                update_post_meta( $order_id, 'sport_instructions', esc_attr( $_POST[ 'sport_instructions' ] ) );
}

add_action('woocommerce_admin_order_data_after_billing_address', 'my_custom_sports_fields_display_admin_order_meta', 20, 1);

function my_custom_sports_fields_display_admin_order_meta($order) {
echo '<p><strong>' . __('Sport in relation to') . ':</strong><br> ' . get_post_meta($order->id, 'sport_instructions', true) . '</p>';
}

$sport_instructions = get_post_meta($wpo_wcpdf->export->order->id,'_sport_instructions',true);
if (isset($sport_instructions)) {
    echo $sport_instructions;
}

我认为最后带有字符串的update_post_meta()应该设置它,但它似乎没有做任何事情。或许我没有适当的功能:
function my_custom_checkout_field($checkout)

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

试试这段代码。 _shipping_instructions将其替换为shipping_instructions

$shipping_instructions = get_post_meta($wpo_wcpdf->export->order >id,'shipping_instructions',true);
if (isset($shipping_instructions)) {
    echo $shipping_instructions;
}