在woocommece产品变体选择上显示自定义字段

时间:2019-09-24 06:25:41

标签: php jquery woocommerce variations

基于我的研究和有限的编码知识,我制定了以下内容,希望对我的查询提供指导。

从产品页面的下拉列表中选择时,我希望获得变化的自定义字段(在页面上的链接内)。而且它应该适用于自定义字段ID和任何链接前缀

例如www.amazon.in/dp/[custom_field] 我希望根据所选的变化来更新自定义字段ID [custom_field]的值。

我正在通过可视化作曲家插件使用自定义产品模板,其中我已插入带有上述链接的文本块。

仅当我设置了[custom_field]值并通过短代码在链接中调用它时,即使选择了变体,它也仅显示父产品的[custom_field]值。

我有以下解决方案来更改[custom_field]的值,但它仅将我限制为前缀www.amazon.in/dp/[custom_field]链接,也将我限制为单个自定义字段ID,即custom_field

所以我正在寻求解决前缀和自定义字段ID限制的问题。

目前,我已经通过设置了自定义字段 https://businessbloomer.com/woocommerce-add-custom-field-product-variation/

添加到主题/ childtheme / woocommerce /单一产品/添加到购物车/variation.php中的代码是

<script type="text/javascript">  
    var var_content = '{{{ data.variation.custom_field }}}';
    var_content = var_content.replace('<div class="woocommerce_custom_field">Custom Field: <span>', "");
    var_content = var_content.replace("</span></div>", "");

    var c_link = 'https://www.amazon.in/dp/'+ var_content + '/?tag=visiaro-21&th=1';
    jQuery(".variation_custom_link").attr("href",c_link);

    jQuery( ".variations_form" ).on( "woocommerce_variation_select_change", function () { 
    var var_val = jQuery('input[name=variation_id]').val();
    if(var_val === ""){
        var default_single_product_cf = jQuery("#default_single_product_cf").text();

        var c_link = 'https://www.amazon.in/dp/'+ default_single_product_cf + '/?tag=visiaro-21&th=1';
        jQuery(".variation_custom_link").attr("href",c_link);
    }
} );

wp-content / themes / childtheme / functions.php中的代码:

  function product_custom_field_in_single_page_function() {
    global $product;
    return get_post_meta( $product->get_id(), 'custom_field', true );
  }
  add_shortcode( 'product_custom_field_single_product', 'product_custom_field_in_single_page_function');

在产品自定义模板(可视化作曲家)中,我还将“ variation_custom_link”放在了Link CSS类文本框中。

screenshot

我如何实现调用任何id的自定义字段值,并且前缀不限于亚马逊?

我还有什么更好的方法可以实现吗?

0 个答案:

没有答案
相关问题