Skip to content

Instantly share code, notes, and snippets.

@fumikito
Created December 21, 2020 06:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fumikito/fb36cc6bb55a3758f57bcd2c9c1a6690 to your computer and use it in GitHub Desktop.
Save fumikito/fb36cc6bb55a3758f57bcd2c9c1a6690 to your computer and use it in GitHub Desktop.
注文の請求先と配送先が異なる時、Yamato B2 Exporter for WooCommerceプラグインの出力するCSVを変更します。
<?php
/*
Plugin Name: Yamato B2 Exporter Change Shipping From
Plugin URI: https://kunoichiwp.com/product/plugin/yamato-b2-exporter
Description: 注文の請求先と配送先が異なる時、Yamato B2 Exporter for WooCommerceプラグインの出力するCSVを変更します。
Version: 1.0.0
Author: Hametuha INC.
Author URI: https://kunoichiwp.com/product/plugin/yamato-b2-exporter
License: GPLv3 or later
*/
// Avoid direct access.
defined( 'ABSPATH' ) || die();
/**
* 注文情報をカスタマイズする
*
* @param array $row CSVの各行
* @param WC_Order $order 注文情報
* @return array フィルターされた行。
*/
add_filter( 'yb2_each_row', function( $row, WC_Order $order ) {
if ( yb2cf_detect_gift( $order ) ) {
$states = WC()->countries->get_states( $order->get_billing_country() );
$address1 = $states[ $order->get_billing_state() ] . $order->get_billing_city() . $order->get_billing_address_1();
$address2 = $order->get_billing_address_2();
$row[ 19 ] = $order->get_billing_phone(); // 依頼主電話番号
$row[ 21 ] = $order->get_billing_postcode(); // 依頼主郵便番号
$row[ 22 ] = $address1; // 依頼主住所
$row[ 23 ] = $address2; // 依頼主住所(アパート、マンション)
$row[ 24 ] = $order->get_formatted_billing_full_name(); // 依頼主名
}
return $row;
}, 10, 2 );
/**
* 注文がギフトかどうかを判別する。
*
* @param WC_Order $order 注文オブジェクト。
* @return bool この注文が贈り物かどうか。
*/
function yb2cf_detect_gift( $order ) {
if ( ! $order->get_formatted_shipping_full_name() || ! $order->get_formatted_billing_full_name() ) {
// 住所が1つしか入力されていないので、カスタマイズは不要。
return false;
} elseif ( $order->get_formatted_shipping_full_name() !== $order->get_formatted_billing_full_name() ) {
// アドレスが異なるので、「ご依頼主住所」を請求先にする
return true;
} else {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment