【问题标题】:Wordpress shortcode linked to another DB链接到另一个数据库的 Wordpress 简码
【发布时间】:2016-03-02 08:54:23
【问题描述】:

首先,这是我第一次尝试短代码。 值得一提的是,我也在我的网站上使用 woocommerce。

让我们开始吧:

我知道要在 wordpress 中添加短代码,你需要在 functions.php 文件中写入类似于以下代码的内容:(这只是一个示例)

function myshortcode() {   
    return "This is a shortcode example!";
}
add_shortcode( 'mycode', 'myshortcode' );

如果我将[mycode] 添加到 wordpress 页面编辑器中,预览会正确显示我的文本。

但是如果我需要在短代码中使用变量(在我的例子中是 woocommerce 订单号)怎么办?

假设我需要比较 woocommerce_order_numbermy_custom_uid(插入另一个数据库,外部 wordpress)。

我通常使用像下面这样的 db 请求,通常它工作正常(和以前一样,这只是一个例子):

select 'my_custom_uid' from 'my_custom_database' where 'woocommerce_order_number' = '1234'

问题是我不知道 woocommerce_order_number(它每次都会更改!),因为这个短代码需要进入一个 html 电子邮件正文,我需要在客户下订单后发送给他们。

如何获取客户 woocommerce 订单(每次都会更改的变量),以便我能够将其用于我的简码以将其链接到我的 custom_uid?

如果问题不够清楚,请随时要求澄清! 非常感谢

【问题讨论】:

    标签: wordpress woocommerce shortcode


    【解决方案1】:

    我看不出使用简码的理由。如果你想在电子邮件中添加一些东西,你应该使用电子邮件中的一个钩子。例如:

    function kia_display_email_order_meta( $order, $sent_to_admin, $plain_text ) {
        $some_field = get_post_meta( $order->id, '_some_field', true ),
        $another_field = get_post_meta( $order->id, '_another_field', true ),
        if( $plain_text ){
            echo 'The value for some field is ' . $some_field . ' while the value of another field is ' . $another_field;
        } else {
            echo '<p>The value for <strong>some field</strong> is ' . $some_field. ' while the value of <strong>another field</strong> is ' . $another_field . '</p>';
        }
    }
    add_action('woocommerce_email_customer_details', 'kia_display_email_order_meta', 30, 3 );
    

    注意$order 对象是kia_display_email_order_meta 函数可用的第一个参数。因此,您可以通过$order-&gt;id 获取 ID。这应该在客户地址详细信息之后添加数据,但如果 woocommerce_email_customer_details 不合适,还有其​​他可用的挂钩。

    【讨论】:

    • 我想使用简码的原因是因为我使用了一个名为 woocommerce 后续电子邮件的插件,它允许您在一段时间后发送电子邮件。我显然只能使用简码放入电子邮件模板
    • 如果电子邮件遵循 WooCommerce 电子邮件的模式,那么它们应该仍然具有您可以使用的钩子。
    【解决方案2】:

    我终于设法解决了这个问题,如果有人感兴趣,我会这样做:

    <?PHP
    
    add_action('fue_before_variable_replacements', 'register_variable_replacements', 11, 4);
    add_action('fue_email_variables_list', 'email_variables_list');
    
    /**
     * This gets called to replace the variable in the email with an actual value
     * @param $var - Modify this: array key is the variable name, value is the replacement
     */
    
    
    function register_variable_replacements($var,  $email_data, $queue_item, $email){
        global $wpdb;
        // Look up UID from order number
        $orderNumber = $var->get_variables()['order_number'];
        $sql = " //Your sql statement here...//";
        $results = $wpdb->get_results($sql);
        $UID = $results[0]->meta_value;
        $variables = array(
            'uid' => $UID
        );
        $var->register($variables);
    }
    
    function email_variables_list($email)
    {
        global $woocommerce;
        ?>
        <li class="var hideable var_subscriptions">
            <strong>{uid}</strong>
            <img class="help_tip" title="<?php _e('Order UID', 'follow_up_emails'); ?>" src="<?php echo $woocommerce->plugin_url(); ?>/assets/images/help.png" width="16" height="16"/>
        </li>
        <?php
    }
    

    现在在您的后续电子邮件插件中,您可以使用 {uid} 作为变量,这将在每封电子邮件中替换为正确的值。

    虽然最好的方法是使用短代码,但后来我发现了这个过滤器,我认为这是处理这个问题的最佳方法。此代码也非常灵活,您可以添加任意数量的变量。

    希望这可以帮助某人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-21
      • 2014-10-04
      • 1970-01-01
      • 2023-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多