Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress 在细枝模板中调用woocommerce函数两次_Wordpress_Woocommerce_Twig_Timber - Fatal编程技术网

Wordpress 在细枝模板中调用woocommerce函数两次

Wordpress 在细枝模板中调用woocommerce函数两次,wordpress,woocommerce,twig,timber,Wordpress,Woocommerce,Twig,Timber,在我的php文件中,我可以编写以下内容: $product = wc_get_product( $context['post']->ID ); $context['sale_price'] = $product->get_variation_sale_price( 'min', false ); 并且{{sale\u price}}在我的小树枝文件中输出销售价格 但是我需要在循环产品上执行此操作。在我的小树枝文件中,我可以运行wc\u get\u product()函数{%set

在我的php文件中,我可以编写以下内容:

$product = wc_get_product( $context['post']->ID );
$context['sale_price'] = $product->get_variation_sale_price( 'min', false );
并且
{{sale\u price}}
在我的小树枝文件中输出销售价格

但是我需要在循环产品上执行此操作。在我的小树枝文件中,我可以运行
wc\u get\u product()
函数
{%set product=fn('wc\u get\u product',post.id)%}

但是我怎么才能做这样的事情:
product->get\u variation\u sale\u price('min',false)

所以本质上是在前一个函数的输出上运行另一个函数


谢谢

好吧,我想起来了

在我的小树枝文件中,我调用一个函数
{%set pricefield=function(['StarterSite','my_function'],post.ID)%}
,当我从下面的函数返回一个数组时,我有以下两位输出:
{%if pricefield.sale='true%%}sale{%endif%}
{pricefield.price}

在我的函数文件中,我有以下内容:

function my_function($i) {
        $product = wc_get_product($i);
        if ( $product->is_type( 'variable' ) ) :

            $product_variations = $product->get_available_variations();

            #2 Get one variation id of a product
            $variation_product_id = $product_variations [0]['variation_id'];

            #3 Create the product object
            $variation_product = new WC_Product_Variation( $variation_product_id );

            #4 Use the variation product object to get the variation prices
            if ($variation_product->sale_price) :
                return array('sale' => 'true', 'price' => '<del>$' . $variation_product->regular_price . '</del> &nbsp;$' . $product->get_variation_sale_price( 'min', false ));
            else : return array('sale' => 'false', 'price' =>  '$' . $variation_product->regular_price);
            endif;
        else:
            if ($product->sale_price) :
                return array('sale' => 'true', 'price' => '<del>$' . $product->regular_price . '</del> &nbsp;$' . $product->sale_price);
            else : return array('sale' => 'false', 'price' =>  '$' . $product->regular_price);
            endif;
        endif;
    }

    /** This is where you can add your own functions to twig.
     *
     * @param string $twig get extension.
     */
    public function add_to_twig( $twig ) {
        $twig->addExtension( new Twig_Extension_StringLoader() );
        $twig->addFilter( new Twig_SimpleFilter( 'myfoo', array( $this, 'myfoo' ) ) );
        $twig->addFunction( new Timber\Twig_Function( 'my_function', 'my_function' ) );
        return $twig;
    }
函数我的函数($i){
$product=wc\U get\U product($i);
如果($product->是_类型('variable')):
$product_VARIANTIONS=$product->获取可用的_VARIANTIONS();
#2获取产品的一个变体id
$variation_product_id=$product_variations[0]['variation_id'];
#3创建产品对象
$variation\u product=新WC\u product\u variation($variation\u product\u id);
#4使用变体产品对象获取变体价格
如果($variation\u product->sale\u price):
返回数组('sale'=>'true','price'=>'$。$variation\u product->regular\u price'.$。$product->get\u variation\u sale\u price('min',false));
else:返回数组('sale'=>'false','price'=>'$。$variation\u product->regular\u price);
endif;
其他:
如果($product->销售价格):
返回数组('sale'=>'true','price'=>'$'.$product->常规价格'$'.$product->销售价格);
else:返回数组('sale'=>'false','price'=>'$。$product->常规价格);
endif;
endif;
}
/**在这里,您可以将自己的函数添加到twig。
*
*@param string$twig获取扩展名。
*/
公共功能添加到小枝($twig){
$twig->addExtension(新的twig_扩展_StringLoader());
$twig->addFilter(新的twig\u SimpleFilter('myfoo',array($this,'myfoo'));
$twig->addFunction(新木材\ twig_函数('my_函数','my_函数');
返回$twig;
}
最后,我做了更多的工作,因为我必须处理可变价格的产品以及这些产品的折扣,以及有折扣但没有可变价格的产品等等,但最终这意味着细枝模板中没有功能,这是最好的结果


希望我能标记所有我签出的堆栈溢出帖子,但找不到它们。如果这能帮助其他人,那就太好了

我认为模板引擎的全部目的是将逻辑和数据操作排除在视图或模板之外。您应该在创建模板之前执行此类操作。只需循环使用模板中的值即可。是的@ngearing我真的需要帮助创建一个自定义函数,我想我可以传入我将在循环中使用的产品id。您不知道如何编写一个函数来执行以下操作:
$product=wc\u get\u product($context['post']->ID);echo$product->get\u variation\u sale\u price('min',false)