【问题标题】:Can I make a WooCommerce shortcode from variable template part?我可以从可变模板部分制作 WooCommerce 短代码吗?
【发布时间】:2020-12-27 18:14:42
【问题描述】:

我想创建一个简码,在我想要的 Woocommerce 产品页面上显示模板部分 mytheme/woocommerce/single-product/add-to-cart/variable.php。我的目标是完全从 WP Barkery 的 Long Description 设置我的页面,而不是从模板文件中调用此模板部分。

我做了:

function shortcode_add_to_cart_variations(){
    wc_get_template_part( 'single-product/add-to-cart/variable' );
}
add_shortcode('add-to-cart-variations', 'shortcode_add_to_cart_variations');

模板部分被调用,但没有显示它的变体,因为$available_variations = NULL : https://github.com/woocommerce/woocommerce/blob/3.5.0/templates/single-product/add-to-cart/variable.php#L26

如何制作我的简码并在我的产品页面上显示我想要的变体(例如在我的 Revolution Slider 中)?

【问题讨论】:

    标签: templates woocommerce shortcode


    【解决方案1】:

    我自己找到了答案。

    我只需要使用核心的woocommerce功能woocommerce_variable_add_to_cart()

    详情在这里:http://hookr.io/functions/woocommerce_variable_add_to_cart/

    在我的functions.php(子主题)中添加这个:

        function shortcode_add_to_cart_variations(){
            woocommerce_variable_add_to_cart();
        }
        add_shortcode('add-to-cart-variations', 'shortcode_add_to_cart_variations');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-31
      • 1970-01-01
      • 2021-09-09
      • 2019-10-29
      • 1970-01-01
      • 1970-01-01
      • 2013-07-09
      • 1970-01-01
      相关资源
      最近更新 更多