草庐IT

php - 根据 woocommerce 类别更改同一产品的默认变体值

coder 2024-05-02 原文

我正在研究一种根据他所属的类别显示同一产品的默认变体值的方法。 例如,我出售一张带有蓝色和红色选项的卡片。 当用户进入类别 ONE 时,我希望默认值为蓝色。 如果他属于第二类,则该值为红色。

我找到了一个钩子(Hook) woocommerce_product_default_attributes ,但我不知道如何使用它。

注意:即使您的产品属于两个类别,woocommerce 似乎也只能识别每个产品的一个类别。


示例 (编辑):

我有一个产品 P .
产品 P 分为两类:Cat 1 & Cat 2 .
此外,产品 P 有两个变量: Blue & Red

当用户来到Cat 1 ,我希望默认值为 Blue 。 如果他来 Cat 2 ,该值将为 Red .

@LoicTheAztech 的答案代码 (下) 有效,但是:

When I go to Cat 1 or Cat 2, I can see that for Woocommerce, the product is only in Cat 1, even if we can access by both category.

所以在一切之前,我需要解决 woocommerce 问题。

最佳答案

从 Woocommerce 3 开始,您可以使用 woocommerce_product_get_default_attributes 过滤器钩子(Hook)……所以您的 2 个产品类别的代码将如下所示:

add_filter( 'woocommerce_product_get_default_attributes', 'filtering_product_get_default_attributes', 10, 2 );
function filtering_product_get_default_attributes( $default_attributes, $product ){
    // We EXIT if it's not a variable product
    if( ! $product->is_type('variable') ) return $default_attributes;

    ## --- YOUR SETTINGS (below) --- ##

    // The desired product attribute taxonomy (always start with "pa_")
    $taxonomy = 'pa_color';

    $category1 = 'one' // The 1st product category (can be an ID, a slug or a name)
    $value1 = 'blue' // The corresponding desired attribute slug value for $category1

    $category2 = 'two' // The 2nd product category (can be an ID, a slug or a name)
    $value2 = 'red' // The corresponding desired attribute slug value for $category2

    ## --- The code --- ##

    // Get the default attribute used for variations for the defined taxonomy
    $default_attribute = $product->get_variation_default_attribute( $taxonomy );

    // We EXIT if define Product Attribute is not set for variation usage
    if( empty( $default_attribute ) ) return $default_attributes;

    // For product category slug 'one' => attribute slug value "blue"
    if( has_term( 'one', 'product_cat', $product->get_id() ) )
        $default_attributes[$taxonomy] = 'blue';

    // For product category slug 'two' => attribute slug value "red"
    elseif( has_term( 'two', 'product_cat', $product->get_id() ) )
        $default_attributes[$taxonomy] = 'red';

    else return $default_attributes; // If no product categories match we exit

    return $default_attributes; // Always return the values in a filter hook
}

代码进入事件子主题(或事件主题)的 function.php 文件。尚未测试,但应该可以。

解释:

Since Woocommerce 3 and new introduced CRUDS setter methods, when a method is using get_prop() WC_Data method, a dynamic hook based on the object type and the method name can be used (essentially for frontend: "view" context)

参见 this line $value = apply_filters( $this->get_hook_prefix() . $prop, $value, $this );

this line 返回 'woocommerce_' 。 $this->object_type 。 '_get_';

所以过滤器 Hook 是动态生成的(其中$this->object_type 等于'product') :

'woocommerce_' . $this->object_type . '_get_' . 'default_attributes'

有 2 个参数:

  • $attributes (替换$value的属性值)
  • $product (替换$this的类实例对象)...

查看 Woocommerce Github closed and solved issue

关于php - 根据 woocommerce 类别更改同一产品的默认变体值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46073328/

有关php - 根据 woocommerce 类别更改同一产品的默认变体值的更多相关文章

  1. ruby-on-rails - Ruby on Rails 迁移,将表更改为 MyISAM - 2

    如何正确创建Rails迁移,以便将表更改为MySQL中的MyISAM?目前是InnoDB。运行原始执行语句会更改表,但它不会更新db/schema.rb,因此当在测试环境中重新创建表时,它会返回到InnoDB并且我的全文搜索失败。我如何着手更改/添加迁移,以便将现有表修改为MyISAM并更新schema.rb,以便我的数据库和相应的测试数据库得到相应更新? 最佳答案 我没有找到执行此操作的好方法。您可以像有人建议的那样更改您的schema.rb,然后运行:rakedb:schema:load,但是,这将覆盖您的数据。我的做法是(假设

  2. ruby - Highline 询问方法不会使用同一行 - 2

    设置:狂欢ruby1.9.2高线(1.6.13)描述:我已经相当习惯在其他一些项目中使用highline,但已经有几个月没有使用它了。现在,在Ruby1.9.2上全新安装时,它似乎不允许在同一行回答提示。所以以前我会看到类似的东西:require"highline/import"ask"Whatisyourfavoritecolor?"并得到:Whatisyourfavoritecolor?|现在我看到类似的东西:Whatisyourfavoritecolor?|竖线(|)符号是我的终端光标。知道为什么会发生这种变化吗? 最佳答案

  3. ruby - 默认情况下使选项为 false - 2

    这是在Ruby中设置默认值的常用方法:classQuietByDefaultdefinitialize(opts={})@verbose=opts[:verbose]endend这是一个容易落入的陷阱:classVerboseNoMatterWhatdefinitialize(opts={})@verbose=opts[:verbose]||trueendend正确的做法是:classVerboseByDefaultdefinitialize(opts={})@verbose=opts.include?(:verbose)?opts[:verbose]:trueendend编写Verb

  4. ruby-on-rails - 项目升级后 Pow 不会更改 ruby​​ 版本 - 2

    我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby​​版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby​​版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘

  5. ruby - Capistrano 3 在任务中更改 ssh_options - 2

    我尝试使用不同的ssh_options在同一阶段运行capistranov.3任务。我的production.rb说:set:stage,:productionset:user,'deploy'set:ssh_options,{user:'deploy'}通过此配置,capistrano与用户deploy连接,这对于其余的任务是正确的。但是我需要将它连接到服务器中配置良好的an_other_user以完成一项特定任务。然后我的食谱说:...taskswithoriginaluser...task:my_task_with_an_other_userdoset:user,'an_othe

  6. ruby-on-rails - date_field_tag,如何设置默认日期? [ rails 上的 ruby ] - 2

    我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问

  7. ruby-on-rails - 在默认方法参数中使用 .reverse_merge 或 .merge - 2

    两者都可以defsetup(options={})options.reverse_merge:size=>25,:velocity=>10end和defsetup(options={}){:size=>25,:velocity=>10}.merge(options)end在方法的参数中分配默认值。问题是:哪个更好?您更愿意使用哪一个?在性能、代码可读性或其他方面有什么不同吗?编辑:我无意中添加了bang(!)...并不是要询问nobang方法与bang方法之间的区别 最佳答案 我倾向于使用reverse_merge方法:option

  8. ruby - 如何根据特征实现 FactoryGirl 的条件行为 - 2

    我有一个用户工厂。我希望默认情况下确认用户。但是鉴于unconfirmed特征,我不希望它们被确认。虽然我有一个基于实现细节而不是抽象的工作实现,但我想知道如何正确地做到这一点。factory:userdoafter(:create)do|user,evaluator|#unwantedimplementationdetailshereunlessFactoryGirl.factories[:user].defined_traits.map(&:name).include?(:unconfirmed)user.confirm!endendtrait:unconfirmeddoenden

  9. ruby - 更改 ActiveRecord 中对象的类 - 2

    假设我有一个FireNinja我的数据库中的对象,使用单表继承存储。后来才知道他真的是WaterNinja.将他更改为不同的子类的最干净的方法是什么?更好的是,我很想创建一个新的WaterNinja对象并替换旧的FireNinja在数据库中,保留ID。编辑我知道如何创建新的WaterNinja来self现有FireNinja的对象,我也知道我可以删除旧的并保存新的。我想做的是改变现有项目的类别。我是通过创建一个新对象并执行一些ActiveRecord魔法来替换行,还是通过对对象本身做一些疯狂的事情,或者甚至通过删除它并使用相同的ID重新插入来做到这一点,这是问题的一部分。

  10. ruby - Rails 关联 - 同一个类的多个 has_one 关系 - 2

    我的问题的一个例子是体育游戏。一场体育比赛有两支球队,一支主队和一支客队。我的事件记录模型如下:classTeam"Team"has_one:away_team,:class_name=>"Team"end我希望能够通过游戏访问一个团队,例如:Game.find(1).home_team但我收到一个单元化常量错误:Game::team。谁能告诉我我做错了什么?谢谢, 最佳答案 如果Gamehas_one:team那么Rails假设您的teams表有一个game_id列。不过,您想要的是games表有一个team_id列,在这种情况下

随机推荐