【问题标题】:How to use BigCommerce utils.api.productAttributes.optionChange如何使用 BigCommerce utils.api.productAttributes.optionChange
【发布时间】:2022-08-05 03:23:56
【问题描述】:

如何在 React 功能组件上使用 utils.api.productAttributes.optionChange 以及当您无权访问 $form.serialize() 时

我试图像这样手动创建一个formData,但它不起作用。

  const addOption = () => {
    const formData = new FormData();
    formData.append(\"action\", \"add\");
    formData.append(\"product_id\", \"459\");
    formData.append(\"optionValue\", \"12213\");
    formData.append(\"attribute\", \"25331\");

    console.log(formData);

    utils.api.productAttributes.optionChange(
      459,
      formData,
      (err, response) => {
        const attributesData = response.data || {};

        console.log(\"err: \", err);
        console.log(\"res: \", response);

        // I just copied this from the documentation but where do I get these methods?
        this.updateProductAttributes(attributesData); 
        this.updateView(attributesData); 
      }
    );
  };  

我得到这些errors 虽然响应状态代码是 200,而我的 formData 只是空的。

    标签: javascript reactjs bigcommerce


    【解决方案1】:

    文档中没有解释,但您实际上并不需要序列化的表单数据,您只需要属性 id 和值。尝试这个:

    const addOption = () => {
    
      utils.api.productAttributes.optionChange(
        459,
        'attribute%5B25331%5D=12213',
        (err, response) => {
          const attributesData = response.data || {};
    
          console.log("err: ", err);
          console.log("res: ", response);
    
          // I just copied this from the documentation but where do I get these methods?
          this.updateProductAttributes(attributesData); 
          this.updateView(attributesData); 
        }
      );
    };
    

    【讨论】:

      【解决方案2】:

      马特发布的代码有效吗?我正在尝试从结帐页面做类似的事情,但它不起作用

      【讨论】:

        猜你喜欢
        • 2013-02-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多