Last active
September 14, 2022 11:30
-
-
Save danielbitzer/ff93ec8f1693027a843a1602c1c12423 to your computer and use it in GitHub Desktop.
[AutomateWoo] Display text dynamically based on the number of items in an order. More info at https://automatewoo.com/docs/variables/custom-variables/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Add the custom variable to the list | |
*/ | |
add_filter( 'automatewoo/variables', 'my_automatewoo_variables' ); | |
/** | |
* @param $variables array | |
* @return array | |
*/ | |
function my_automatewoo_variables( $variables ) { | |
// variable's string form is set here, it will be order.pluralize | |
$variables['order']['pluralize'] = dirname(__FILE__) . '/variable-order-pluralize.php'; | |
return $variables; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ( ! defined( 'ABSPATH' ) ) exit; | |
/** | |
* @class AW_Variable_Order_Pluralize | |
*/ | |
class My_AutomateWoo_Variable_Order_Pluralize extends AutomateWoo\Variable { | |
/** @var bool - whether to allow setting a fallback value for this variable */ | |
public $use_fallback = false; | |
public function load_admin_details() { | |
$this->description = __( "Display text dynamically based on the number of items in an order.", 'automatewoo'); | |
// setting parameters fields is optional | |
$this->add_parameter_text_field( 'single', __( 'Used when there is one item purchased.', 'your-text-domain' ) ); | |
$this->add_parameter_text_field( 'plural', __( 'Used when there are more than one item purchased.', 'your-text-domain' ) ); | |
} | |
/** | |
* @param $order WC_Order | |
* @param $parameters array | |
* @return string | |
*/ | |
public function get_value( $order, $parameters ) { | |
$single = isset( $parameters['single'] ) ? $parameters['single'] : ''; | |
$plural = isset( $parameters['plural'] ) ? $parameters['plural'] : ''; | |
return $order->get_item_count() == 1 ? $single : $plural; | |
} | |
} | |
return new My_AutomateWoo_Variable_Order_Pluralize(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment