Skip to content
This repository has been archived by the owner on Mar 17, 2022. It is now read-only.

Commit

Permalink
Fixes #215 Coupon string translation, Fixes #217 updated BACS function
Browse files Browse the repository at this point in the history
* Fixes #215 add string translations for Coupons (includes WooCommerce
Extended Coupon Features if installed)
* addresses #168 with a utility function get_translated_variation to
help get translated products or variations
* Fixes #217 BACS bank_details() update for woocommerce3
  • Loading branch information
Jon007 committed Jul 19, 2017
1 parent 456e3cb commit 5ab1c13
Show file tree
Hide file tree
Showing 5 changed files with 260 additions and 100 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
### 1.0.2
* Fixes #215 add string translations for Coupons (includes WooCommerce Extended Coupon Features if installed)
* addresses #168 with a utility function get_translated_variation to help get translated products or variations
* Fixes #217 BACS bank_details() update for woocommerce3
* partially implements #208 WooCommerce 3.1 CSV Import/Export by adding support for synchronising Product Meta and Product Attributes to translated products
* Fixes #213 copy children for Grouped Product
* Fixes #207 suppresses login customization to allow "Pay for Order" links to work when customer is not logged in
Expand Down
5 changes: 4 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,11 @@ Just make sure to setup your permalinks , and every thing will be fine , I promi
== Changelog ==

== 1.0.2 ==
* partially implements #208 WooCommerce 3.1 CSV Import/Export by adding support for synchronising Product Meta and Product Attributes to translated products
* Fixes #215 add string translations for Coupons (includes WooCommerce Extended Coupon Features if installed)
* addresses #168 with a utility function get_translated_variation to help get translated products or variations
* Fixes #217 BACS bank_details() update for woocommerce3
* Fixes #213 copy children for Grouped Product
* partially implements #208 WooCommerce 3.1 CSV Import/Export by adding support for synchronising Product Meta and Product Attributes to translated products
* Fixes #207 suppresses login customization to allow "Pay for Order" links to work when customer is not logged in
(after login continue to payment page instead of my account home)
* Fixes #212 update deleteRelatedVariation for woocommerce3 warnings
Expand Down
164 changes: 125 additions & 39 deletions src/Hyyan/WPI/Coupon.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,134 @@ public function __construct()
{
if ('on' === Settings::getOption('coupons', Features::getID(), 'on')) {
add_action('woocommerce_coupon_loaded', array($this, 'couponLoaded'));

add_action('wp_loaded', array($this, 'registerCouponStringsForTranslation'));

//apply label filter with higher priority than woocommerce-auto-added-coupons
add_filter('woocommerce_cart_totals_coupon_label',
array($this, 'translateLabel'), 20, 2);
add_filter('woocommerce_coupon_get_description',
array($this, 'translateDescription'), 10, 2);

/* additional fields for WooCommerce Extended Coupon Features */
add_filter('woocommerce_coupon_get__wjecf_enqueue_message',
array($this, 'translateMessage'), 10, 2);
add_filter('woocommerce_coupon_get__wjecf_select_free_product_message',
array($this, 'translateMessage'), 10, 2);
add_filter('woocommerce_coupon_get__wjecf_free_product_ids',
array($this, 'getFreeProductsInLanguage'), 10, 2);
}
}

/**
* filter product ids
*
* @param string $product_ids list
* @param WC_Coupon $coupon current coupon
*
* @return array filtered result
*/
public function getFreeProductsInLanguage($productIds, $coupon)
{
if (is_admin()){return $productIds;}
$productLang = pll_current_language();
$productIds = explode(',', $productIds);
$mappedIds = array();
foreach ($productIds as $productId) {
$mappedIds[] = Utilities::get_translated_variation($productId, $productLang);
}
return $mappedIds;
}

/**
* translate coupon code.
*
* @param string $value
* @param \WC_Coupon $coupon
*
* @return string
*/
public function translateLabel($value, $coupon)
{
return sprintf( esc_html__( 'Coupon: %s', 'woocommerce' ),
pll__( \get_post( $coupon->get_id() )->post_title ) );
}
/**
* translate coupon description.
*
* @param string $value
* @param \WC_Coupon $coupon
*
* @return string
*/
public function translateDescription($value, $coupon)
{
return pll__($value);
}
/**
* translate coupon message.
*
* @param string $value
* @param \WC_Coupon $coupon
*
* @return string
*/
public function translateMessage($value, $coupon)
{
return pll__($value);
}

/**
* Register coupon titles adn descriptions in Polylang's Strings translations table.
*/
public function registerCouponStringsForTranslation()
{
if (function_exists('pll_register_string')) {
$coupons = $this->getCoupons();

foreach ($coupons as $coupon) {
//$code = wc_format_coupon_code($coupon->post_title);
pll_register_string($coupon->post_name, $coupon->post_title,
__('Woocommerce Coupon Names', 'woo-poly-integration'));
pll_register_string($coupon->post_name . '_description', $coupon->post_excerpt,
__('Woocommerce Coupon Names', 'woo-poly-integration'), true);

$coupon_message = get_post_meta($coupon->ID, '_wjecf_enqueue_message', true);
if ($coupon_message) {
pll_register_string($coupon->post_name . '_message', $coupon_message,
__('Woocommerce Coupon Names', 'woo-poly-integration'), true);
}
$freeproduct_message = get_post_meta($coupon->ID, '_wjecf_select_free_product_message', true);
if ($freeproduct_message) {
pll_register_string($coupon->post_name . '_freeproductmessage', $coupon_message,
__('Woocommerce Coupon Names', 'woo-poly-integration'), true);
}

}
}
}

/**
* Helper function - Gets the coupons enabled in the shop.
*
* @return array $coupons Coupons settings including post_type, post_excerpt and post_title
*/
private function getCoupons(){
global $woocommerce;

$args = array(
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'asc',
'post_type' => 'shop_coupon',
'post_status' => 'publish',
);

$coupons = get_posts( $args );
return $coupons;
}


/**
* Extend the coupon to include porducts translations.
*
Expand Down Expand Up @@ -79,45 +204,6 @@ public function couponLoaded(\WC_Coupon $coupon)
return $coupon;
}

/**
* Extend the coupon to include porducts translations.
*
* @param \WC_Coupon $coupon
*
* @return \WC_Coupon
*/
public function couponLoadedOld(\WC_Coupon $coupon)
{
$productIDS = array();
$excludeProductIDS = array();
$productCategoriesIDS = array();
$excludeProductCategoriesIDS = array();
foreach ($coupon->product_ids as $id) {
foreach ($this->getProductPostTranslationIDS($id) as $_id) {
$productIDS[] = $_id;
}
}
foreach ($coupon->exclude_product_ids as $id) {
foreach ($this->getProductPostTranslationIDS($id) as $_id) {
$excludeProductIDS[] = $_id;
}
}
foreach ($coupon->product_categories as $id) {
foreach ($this->getProductTermTranslationIDS($id) as $_id) {
$productCategoriesIDS[] = $_id;
}
}
foreach ($coupon->exclude_product_categories as $id) {
foreach ($this->getProductTermTranslationIDS($id) as $_id) {
$excludeProductCategoriesIDS[] = $_id;
}
}
$coupon->product_ids = $productIDS;
$coupon->exclude_product_ids = $excludeProductIDS;
$coupon->product_categories = $productCategoriesIDS;
$coupon->exclude_product_categories = $excludeProductCategoriesIDS;
return $coupon;
}

/**
* Get array of product translations IDS.
Expand Down
131 changes: 72 additions & 59 deletions src/Hyyan/WPI/Gateways/GatewayBACS.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,70 +59,83 @@ public function email_instructions($order, $sent_to_admin, $plain_text = false)

/**
* Get bank details and place into a list format.
* Updated from wooCommerce 3.1
*
* Note: Since this is declared as a private function in WC_Gateway_BACS, it needs
* to be copied here 1:1
*
* @param int $order_id
*/
private function bank_details($order_id = '')
{
if (empty($this->account_details)) {
return;
}
private function bank_details( $order_id = '' ) {

if ( empty( $this->account_details ) ) {
return;
}

// Get order and store in $order
$order = wc_get_order( $order_id );

// Get the order country and country $locale
$country = $order->get_billing_country();
$locale = $this->get_country_locale();

// Get sortcode label in the $locale array and use appropriate one
$sortcode = isset( $locale[ $country ]['sortcode']['label'] ) ? $locale[ $country ]['sortcode']['label'] : __( 'Sort code', 'woocommerce' );

$bacs_accounts = apply_filters( 'woocommerce_bacs_accounts', $this->account_details );

if ( ! empty( $bacs_accounts ) ) {
$account_html = '';
$has_details = false;

foreach ( $bacs_accounts as $bacs_account ) {
$bacs_account = (object) $bacs_account;

if ( $bacs_account->account_name ) {
$account_html .= '<h3 class="wc-bacs-bank-details-account-name">' . wp_kses_post( wp_unslash( $bacs_account->account_name ) ) . ':</h3>' . PHP_EOL;
}

$account_html .= '<ul class="wc-bacs-bank-details order_details bacs_details">' . PHP_EOL;

// BACS account fields shown on the thanks page and in emails
$account_fields = apply_filters( 'woocommerce_bacs_account_fields', array(
'bank_name' => array(
'label' => __( 'Bank', 'woocommerce' ),
'value' => $bacs_account->bank_name,
),
'account_number' => array(
'label' => __( 'Account number', 'woocommerce' ),
'value' => $bacs_account->account_number,
),
'sort_code' => array(
'label' => $sortcode,
'value' => $bacs_account->sort_code,
),
'iban' => array(
'label' => __( 'IBAN', 'woocommerce' ),
'value' => $bacs_account->iban,
),
'bic' => array(
'label' => __( 'BIC', 'woocommerce' ),
'value' => $bacs_account->bic,
),
), $order_id );

foreach ( $account_fields as $field_key => $field ) {
if ( ! empty( $field['value'] ) ) {
$account_html .= '<li class="' . esc_attr( $field_key ) . '">' . wp_kses_post( $field['label'] ) . ': <strong>' . wp_kses_post( wptexturize( $field['value'] ) ) . '</strong></li>' . PHP_EOL;
$has_details = true;
}
}

$account_html .= '</ul>';
}

if ( $has_details ) {
echo '<section class="woocommerce-bacs-bank-details"><h2 class="wc-bacs-bank-details-heading">' . __( 'Our bank details', 'woocommerce' ) . '</h2>' . PHP_EOL . $account_html . '</section>';
}
}

}

// Get order and store in $order
$order = wc_get_order($order_id);

// Get the order country and country $locale
$country = Utilities::get_billing_country($order);
$locale = $this->get_country_locale();

// Get sortcode label in the $locale array and use appropriate one
$sortcode = isset($locale[ $country ]['sortcode']['label']) ? $locale[ $country ]['sortcode']['label'] : __('Sort Code', 'woocommerce');

$bacs_accounts = apply_filters('woocommerce_bacs_accounts', $this->account_details);

if (!empty($bacs_accounts)) {
echo '<h2>'.__('Our Bank Details', 'woocommerce').'</h2>'.PHP_EOL;

foreach ($bacs_accounts as $bacs_account) {
$bacs_account = (object) $bacs_account;

if ($bacs_account->account_name || $bacs_account->bank_name) {
echo '<h3>'.wp_unslash(implode(' - ', array_filter(array($bacs_account->account_name, $bacs_account->bank_name)))).'</h3>'.PHP_EOL;
}

echo '<ul class="order_details bacs_details">'.PHP_EOL;

// BACS account fields shown on the thanks page and in emails
$account_fields = apply_filters('woocommerce_bacs_account_fields', array(
'account_number' => array(
'label' => __('Account Number', 'woocommerce'),
'value' => $bacs_account->account_number,
),
'sort_code' => array(
'label' => $sortcode,
'value' => $bacs_account->sort_code,
),
'iban' => array(
'label' => __('IBAN', 'woocommerce'),
'value' => $bacs_account->iban,
),
'bic' => array(
'label' => __('BIC', 'woocommerce'),
'value' => $bacs_account->bic,
),
), $order_id);

foreach ($account_fields as $field_key => $field) {
if (!empty($field['value'])) {
echo '<li class="'.esc_attr($field_key).'">'.esc_attr($field['label']).': <strong>'.wptexturize($field['value']).'</strong></li>'.PHP_EOL;
}
}

echo '</ul>';
}
}
}
}
Loading

0 comments on commit 5ab1c13

Please sign in to comment.